an odd fellow

仕事のメモ

terraform init が s3 の認証情報が違うと言ってコケるとき

備忘録

問題

terraform init で -backend-config を指定すると以下のように怒られた。

$ terraform init -backend-config="key=prd/gcp-terraform.tfstate"

Initializing the backend...

Error configuring the backend "s3": No valid credential sources found for AWS Provider.
  Please see https://terraform.io/docs/providers/aws/index.html for more information on
  providing credentials for the AWS Provider

Please update the configuration in your Terraform files to fix this error.
If you'd like to update the configuration interactively without storing
the values in your configuration, run "terraform init".

provier.tf で aws のアクセスキーとシークレットキーは以下のように指定してあり、-backend-config を指定するまでは動く事を確認している。

provider "aws" {
  access_key = "ACCESS_KEY"
  secret_key = "SECRET_KEY"
  region = "us-west-2"
}

解決策

自分は環境変数に吐くことにした。変数名はアクセスキーがAWS_ACCESS_KEY_ID、シークレットキーがAWS_SECRET_ACCESS_KEY。 (参照: AWS CLI の設定 - AWS Command Line Interface)

$ export AWS_ACCESS_KEY_ID = "ACCESS_KEY"
$ export AWS_SECRET_ACCESS_KEY = "SECRET_KEY"