網頁

2023/9/5

Golang 取得AWS Pricing ServiceCode

Go以AWS提供的SDK aws-sdk-go-v2來取得Pricing ServiceCode。


ServiceCode是pricing api用來查詢AWS服務價格資料時的代碼參數。


事前要求

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

參考「Golang 建立AWS Pricing API client」建立Pricing API client。


取得Pricing ServiceCode

呼叫pricing.Client.DescribeServices傳入參數pricing.DescribeServicesInput取得所有服務的ServiceCode。

main.go

package main

import (
    "context"
    "fmt"

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

func main() {
    ctx := context.TODO()
    client := NewPricingClient(ctx)
    out, err := client.DescribeServices(ctx, &pricing.DescribeServicesInput{})
    if err != nil {
        panic(err)
    }

    for _, s := range out.Services {
        fmt.Println(*s.ServiceCode)
    }
}

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

    return pricing.NewFromConfig(cfg) // Create an Pricing service client
}


測試

執行Go應用程式輸出以下結果(這只是第一頁的結果)。

A4B
AWSAmplify
AWSAppFabric
AWSAppRunner
AWSAppSync
AWSApplicationMigrationSvc
AWSBackup
AWSBillingConductor
AWSBudgets
AWSCertificateManager
AWSCleanRooms
AWSCloudFormation
AWSCloudMap
AWSCloudTrail
AWSCloudWAN
AWSCodeArtifact
AWSCodeCommit
AWSCodeDeploy
AWSCodePipeline
AWSConfig
AWSCostExplorer
AWSDataExchange
AWSDataSync
AWSDataTransfer
AWSDatabaseMigrationSvc
AWSDeepRacer
AWSDeveloperSupport
AWSDeviceFarm
AWSDirectConnect
AWSDirectoryService
AWSELB
AWSElasticDisasterRecovery
AWSElementalMediaConvert
AWSElementalMediaLive
AWSElementalMediaPackage
AWSElementalMediaStore
AWSElementalMediaTailor
AWSEntityResolution
AWSEvents
AWSFIS
AWSFMS
AWSGlobalAccelerator
AWSGlueElasticViews
AWSGlue
AWSGreengrass
AWSGroundStation
AWSIoT1Click
AWSIoTAnalytics
AWSIoTEvents
AWSIoTFleetWise
AWSIoTSiteWise
AWSIoTThingsGraph
AWSIoT
AWSLakeFormation
AWSLambda
AWSM2
AWSMDC
AWSMediaConnect
AWSMigrationHubRefactorSpaces
AWSNetworkFirewall
AWSOutposts
AWSPrivate5G
AWSQueueService
AWSR53AppRecoveryController
AWSResilienceHub
AWSRoboMaker
AWSSecretsManager
AWSSecurityHub
AWSServiceCatalog
AWSShield
AWSStorageGatewayDeepArchive
AWSStorageGateway
AWSSupplyChain
AWSSupportBusiness
AWSSupportEnterprise
AWSSystemsManager
AWSTelcoNetworkBuilder
AWSTransfer
AWSWickr
AWSWisdom
AWSXRay
AlexaTopSites
AlexaWebInfoService
AmazonA2I
AmazonApiGateway
AmazonAppStream
AmazonAthena
AmazonBraket
AmazonChimeBusinessCalling
AmazonChimeCallMeAMCS
AmazonChimeCallMe
AmazonChimeDialInAMCS
AmazonChimeDialin
AmazonChimeFeatures
AmazonChimeServices
AmazonChimeVoiceConnector
AmazonChime
AmazonCloudDirectory
AmazonCloudFront
AmazonCloudSearch

沒有留言:

張貼留言