AdSense

網頁

2024/6/18

Golang 列出AWS Account Regions啟用狀態

Go以AWS提供的SDK aws-sdk-go-v2列出Account的全部Regino啟用狀態。


事前要求

參考「Golang 建立AWS Account API client」建立Account API Client。


列出Region啟用狀態

呼叫account.Client.ListRegions,輸入參數account.ListRegionsInput取得Account的Regions的啟用狀態types.RegionOptStatus

package main

import (
    "context"
    "fmt"

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

func main() {
    ctx := context.TODO()
    client := NewAccountClient(ctx)
    output, err := client.ListRegions(ctx, &account.ListRegionsInput{
        AccountId: aws.String("[ACCOUNT_ID]"),
    })
    if err != nil {
        panic(err)
    }
    for _, r := range output.Regions {
        fmt.Println(r.RegionName)
        fmt.Println(r.RegionOptStatus)
    }
}

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

    return account.NewFromConfig(cfg) // Create an AWS Account client
}

沒有留言:

AdSense