🌐

aws loginでTerraformを使うための設定

に公開

環境

  • AWS CLI 2.32.17
  • Terraform 1.14.2
  • macOS 26.2

関連記事

この記事の内容でaws loginまたはterraform実行時に問題が発生した場合は、次の記事を参照してください。

https://zenn.dev/masatoshi_tada/articles/a0cdce3e8961ca

aws loginとは?

https://dev.classmethod.jp/articles/aws-cli-aws-login/

aws loginのための準備

1. IAMポリシーの追加

Terraform実行で利用しているIAMロールにポリシー追加が必要なので、忘れないようにしてください。

以下のようなポリシーを主導で追加するか、ManagedポリシーSignInLocalDevelopmentAccess(内容は下記と同じ)を追加すればOKです。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "signin:AuthorizeOAuth2Access",
                "signin:CreateOAuth2Token"
            ],
            "Resource": "arn:aws:signin:*:*:oauth2/public-client/*"
        }
    ]
}

公式ドキュメント👇️
https://docs.aws.amazon.com/signin/latest/userguide/command-line-sign-in.html#command-line-sign-in-local-development

2. ~/.aws/credentialのリネーム・削除

いきなり削除するとうまく行かなかったときに大変なので、しばらくはファイル名を変えるだけにしましょう。

mv ~/.aws/credentials ~/.aws/_credentials

aws loginを何日か使って、大丈夫そうなことが確認できたら削除しましょう。

rm ~/.aws/_credentials

Terraform利用に必要な設定

1. ~/.aws/configの編集

credential_process = aws configure export-credentialsをdefaultプロファイルに追加してください。

~/.aws/config
[default]
region = ap-northeast-1
output = json
# 👇️の1行を追加!
credential_process = aws configure export-credentials

[profile App1]
source_profile = default
...

[profile App2]
source_profile = default
...

default以外のプロファイルに書くと、terraform init時に下記のようなエラーになりました。

$ terraform init
Initializing the backend...

 Error: No valid credential sources found
 
 Please see https://developer.hashicorp.com/terraform/language/backend/s3
 for more information about providing credentials.
 
 Error: failed to refresh cached credentials, no EC2 IMDS role found, operation error ec2imds: GetMetadata, exceeded maximum number of attempts, 3, request send failed, Get "http://169.254.169.254/latest/meta-data/iam/security-credentials/": dial tcp 169.254.169.254:80: connect: host is
 down

2. AWSプロバイダー設定の編集

Terraformの.tfファイルにプロファイルを指定していると、terraform plan時に次のようなエラーになりました。

main.tf
# ❌️な例
provider "aws" {
  profile = "App1"
}
plan時のエラー
$ terraform plan

Planning failed. Terraform encountered an error while generating this plan.


 Error: No valid credential sources found
 
   with provider["registry.terraform.io/hashicorp/aws"],
   on main.tf line 16, in provider "aws":
   16: provider "aws" {
 
 Please see https://registry.terraform.io/providers/hashicorp/aws
 for more information about providing credentials.
 
 Error: failed to refresh cached credentials, operation error STS: AssumeRole, https response error StatusCode: 403, RequestID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, api error AccessDenied: User: arn:aws:sts::999999999999:assumed-role/SampleRole/tada is not authorized
 to perform: sts:AssumeRole on resource: arn:aws:iam::123456789012:role/SampleRole
 

なので、次のように書き換えましょう。

main.tf
# ⭕️な例
provider "aws" {
  region = "ap-northeast-1"
}

トラブルシューティング

~/.zshrcに環境変数AWS_PROFILEを記述していると、aws loginがうまく行かないことがありました。環境変数AWS_PROFILEを~/.zshrcから削除することで対応しました。

まとめ

以上の手順で、aws loginでTerraformが使えるようになります。

Discussion