2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

aws-sdk-goでcredentialを指定するには

Posted at

結論

Credentialsを指定して、静的な値を入れる。

You can hard-code credentials in your application by passing the access keys to a configuration instance, as shown in the following snippet.

go
aws.Config{
	Region: aws.String(cfg.Region),
	Credentials: credentials.NewStaticCredentials(
		"AWS_ACCESS_KEY_ID",
		"AWS_SECRET_ACCESS_KEY",
		"TOKEN",)
}

経緯

アプリケーションをデプロイする際に、dockerコンテナの環境変数と本番環境の環境変数が被っていた。
この環境変数は、ElasticSearchを使うにあたって必要だったので命名を変更。
AWS_ACCESS_KEY_ID => ES_A_K_ID
AWS_SECRET_ACCESS_KEY => ES_S_A_K

エラー

NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors

aws-sdk-goが、秘匿情報が読めてないことが発覚。
いままで何の気なしに使っていたので、どうしたもんかと調べる:tea:

修正前

aws-sdkの仕様で、 環境変数(AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY)を自動で読み出していた。
コンテナの環境変数に存在しないので、空の値。

func ConnectDB(cfg *config.Server) (RepoInterface, error) {
	session, err := session.NewSession()
	if err != nil {
		return nil, err
	}
	db := dynamo.New(session, &aws.Config{Region: aws.String(cfg.Region)})
        # 中略
	return &repository{db: db}, nil
}

修正後

まとめの通りにCredentialを入れ込んであげる。

func ConnectDB(cfg *config.Server) (RepositoryInterface, error) {
	session, err := session.NewSession()
	if err != nil {
		return nil, err
	}
	db := dynamo.New(session, &aws.Config{
		Region: aws.String(cfg.Region),
		Credentials: credentials.NewStaticCredentials(
			cfg.ESAKID,
			cfg.ESSAK,
			"",
		),
	})
        # 中略
	return &repository{db: db}, nil
}

REF

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?