網頁

2023/2/6

Golang 建立AWS Direct Connect gateway

Go以AWS提供的SDK aws-sdk-go-v2來取得Direct Connect Connection的資料。



事前要求

參考「AWS 建立IAM管理使用者及credentials」設定供應用程式存取AWS需要的credentials。

參考「Golang 建立AWS Direct Connect API client」建立Direct Connect API client。


建立Diret Connect gateway

呼叫directconnect.Client.CreateDirectConnectGateway傳入參數directconnect.CreateDirectConnectGatewayInput來建立Direct Connect gateway。

main.go

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/directconnect"
)

func main() {
    ctx := context.TODO()
    client := NewDirectConnectClient(ctx)

    directConnectGatewayName := "demo-directconnect-gateway-001"
    amazonSideAsn := int64(64512)
    params := &directconnect.CreateDirectConnectGatewayInput{
        DirectConnectGatewayName: &directConnectGatewayName,
        AmazonSideAsn:            &amazonSideAsn,
    }

    output, err := client.CreateDirectConnectGateway(ctx, params)
    if err != nil {
        panic(err)
    }

    dcg := output.DirectConnectGateway

    fmt.Println(*dcg.DirectConnectGatewayId)   // e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af
    fmt.Println(dcg.DirectConnectGatewayState) // available
}

func NewDirectConnectClient(ctx context.Context) *directconnect.Client {
    cfg, err := config.LoadDefaultConfig(
        ctx,
        config.WithRegion("ap-northeast-1"),
    )
    if err != nil {
        panic(err)
    }

    return directconnect.NewFromConfig(cfg) // Create an Amazon Direct Connect service client
}

github


測試

執行Go應用程式輸出以下結果。

e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af
available




沒有留言:

張貼留言