網頁

2023/2/8

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」建立Direct Connect Connection


刪除Direct Connect Connection

呼叫directconnect.Client.DeleteConnection傳入參數directconnect.DeleteConnectionInput來刪除Direct Connect Connection。

directconnect.DeleteConnectionInput.ConnectionId填入要刪除的Direct Connect 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.DeleteConnectionInput{
        ConnectionId: &connectionId,
    }

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

    fmt.Println(output.ConnectionState) // deleted
}

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

deleted


沒有留言:

張貼留言