網頁

2023/2/8

Golang 刪除AWS Direct Connect gateway

Go以AWS提供的SDK aws-sdk-go-v2來刪除Direct Connect gateway。



事前要求

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

參考「Golang 建立AWS Direct Connect gateway」建立Direct Connect gateway


刪除Direct Connect gateway

呼叫directconnect.Client.DeleteDirectConnectGateway傳入參數directconnect.DeleteDirectConnectGatewayInput來刪除Direct Connect gateway。

directconnect.DeleteDirectConnectGatewayInput.DirectConnectGatewayId填入要刪除的Direct Connect gateway的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)

    directConnectGatewayId := "e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af"
    params := &directconnect.DeleteDirectConnectGatewayInput{
        DirectConnectGatewayId: &directConnectGatewayId,
    }

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

    dcg := output.DirectConnectGateway
    fmt.Println(dcg.DirectConnectGatewayState) // deleting
}

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

deleting


沒有留言:

張貼留言