#DynamoDB #AWS #Go言語 #LocalStack ## 事象 たとえば[[AWS SDK for Go V2でテーブルを作成]]すると[[リージョン]]が空になる。 ## 状況 - [[AWSのconfigやcredentialを環境変数で設定]]している - カスタムエンドポイントを設定している - `config.LoadDefaultConfig`に`config.WithRegion`は設定してみた ## 原因 Custom resolverを作るときの`aws.Endpoint`に`SigningRegion`を指定していなかった。 ```go:NG awsEndpoint := "http://host.docker.internal:4566" customResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) { if awsEndpoint != "" { return aws.Endpoint{ PartitionID: "aws", URL: awsEndpoint, }, nil } return aws.Endpoint{}, &aws.EndpointNotFoundError{} }) ``` ```go:OK awsEndpoint := "http://host.docker.internal:4566" customResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) { if awsEndpoint != "" { return aws.Endpoint{ PartitionID: "aws", URL: awsEndpoint, SigningRegion: region, // 追加 }, nil } return aws.Endpoint{}, &aws.EndpointNotFoundError{} }) ``` ## 調査メモ ### config.LoadDefaultConfig ```go:[email protected]/config.go func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error) (cfg aws.Config, err error) { var options LoadOptions for _, optFn := range optFns { optFn(&options) } // assign Load Options to configs var cfgCpy = configs{options} cfgCpy, err = cfgCpy.AppendFromLoaders(ctx, defaultLoaders) if err != nil { return aws.Config{}, err } cfg, err = cfgCpy.ResolveAWSConfig(ctx, defaultAWSConfigResolvers) if err != nil { return aws.Config{}, err } return cfg, nil } ``` [[Functional Option Pattern]]を使っているが特に気になるところはない。 実際、`LoadDefaultConfig`の呼び出し元で`cfg.Region`の値を調べると、`ap-northeast-1`となっており反映されている。 ### dynamodb.NewFromConfig ```go:api_client.go // NewFromConfig returns a new client from the provided config. func NewFromConfig(cfg aws.Config, optFns ...func(*Options)) *Client { opts := Options{ Region: cfg.Region, HTTPClient: cfg.HTTPClient, Credentials: cfg.Credentials, APIOptions: cfg.APIOptions, Logger: cfg.Logger, ClientLogMode: cfg.ClientLogMode, } resolveAWSRetryerProvider(cfg, &opts) resolveAWSEndpointResolver(cfg, &opts) resolveEnableEndpointDiscoveryFromConfigSources(cfg, &opts) return New(opts, optFns...) } ``` ### addOperationCreateTableMiddlewares テーブル作成のオペレーション関数にログを仕込んでみたが、`ap-northeast-1`と表示されていた。 ```go:api_op_CreateTable.go func (c *Client) addOperationCreateTableMiddlewares(stack *middleware.Stack, options Options) (err error) { println("___") println(options.Region) println("^^^") ```