網頁

2023/2/6

Golang 取得AWS Direct Connect Connection

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



事前要求

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

參考「Golang 建立AWS Direct Connect Connection」建立connection。


取得Diret Connect Connection

呼叫directconnect.Client.DescribeConnections傳入參數directconnect.DescribeConnectionsInput來取得Direct Connect Connection資料。

directconnect.DescribeConnectionsInput的屬性ConnectionId為connection的ID。



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)

    connectionId := "dxcon-fg5kq63s"
    params := &directconnect.DescribeConnectionsInput{
        ConnectionId: &connectionId,
    }

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

    connection := output.Connections[0]
    fmt.Println(*connection.ConnectionName)     // demo-connection-001
    fmt.Println(*connection.Bandwidth)          // 1Gbps
    fmt.Println(connection.ConnectionState)     // down
    fmt.Println(*connection.Location)           // CHT51
    fmt.Println(*connection.AwsLogicalDeviceId) // CHT51-2l5nybymui838
    fmt.Println(*connection.ProviderName)       // Chunghwa Telecom
}

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應用程式輸出以下結果。

demo-connection-001
1Gbps
down
CHT51
CHT51-2l5nybymui838
Chunghwa Telecom
<nil>


沒有留言:

張貼留言