この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
先日、以下のエントリのなかでaws sso loginコマンドを使ったAWS SSO(AWS Single Sign-On)のユーザーが引き受けたIAM Roleの権限でTerraformを実行する方法をご紹介しました。
この aws sso loginコマンドで作成される profileがいわゆるSSO Profileです。上記エントリのなかでAWS Provider version 3.26よりTerraformでもこのprofileのサポートを開始したと書きました。が、厳密にはこの説明では不十分な場合があるので、あらためてSSO ProfileのTerraformでの使い方についてまとめます。
先にまとめ
- SSO ProfileはAWS CLI Version2でのみ使用できます。まだVersion1の方はまずVersion2をインストールしてください。
- SSO Profileの作成にはaws configure ssoコマンドが便利です。
- 作ったSSO Profile名をTerraformのコード内でprofile引数に指定するか、AWS_PROFILE環境変数に格納してTerraformを実行します。
- S3 Backend、つまりStateファイルの格納場所としてS3バケットを使う場合は、Terraform本体のVersionを0.14.6以上にしましょう。
- AWS Providerを使って各種AWSリソースをプロビジョニングする場合は、AWS ProviderのVersionを 3.26以上にしましょう。
SSO Profileの作成方法
SSO Profileの例は以下です。これを~/.aws/config
に記載します。
[profile my-dev-profile]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = ap-northeast-1
sso_account_id = 123456789011
sso_role_name = readOnly
region = ap-northeast-1
output = json
もちろん手動でこの書式でprofileを書いても良いですが、aws configure sso
という便利コマンドを使うと質問回答形式で、かつ入力補完も付くのでより簡単に作成できます。
aws configure ssoコマンド実行例
$ aws configure sso
SSO start URL [None]: https://my-sso-portal.awsapps.com/start
SSO Region [None]: ap-northeast-1
Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:
https://device.sso.ap-northeast-1.amazonaws.com/
Then enter the code:
ZKTN-KTCZ
SSO Regionまで答えるとブラウザで自動でSSOポータルが開きます。 Profileを作りたいSSOユーザーでログインします。
ログイン完了すると以下画面になるので、Allowを押下します。 以下画面に遷移したらこのブラウザタブは閉じてOKです。 ターミナルに戻ります。ログインしたSSOユーザーがスイッチロールできるアカウント一覧が出てくるので、Profileを作りたいアカウントを選択します。 その後も、使いたいロール、デフォルトリージョン、デフォルトアウトプットフォーマット、profile名を順に訊かれます。
There are 3 AWS accounts available to you.
Using the account ID 123456789012
There are 2 roles available to you.
Using the role name "AdministratorAccess"
CLI default client Region [ap-northeast-1]:
CLI default output format :
CLI profile name [AdministratorAccess-123456789012]: test-sso-profile
To use this profile, specify the profile name using --profile, as shown:
aws s3 ls --profile test-sso-profile
ここまで完了すれば、~/.aws/config
末尾に前述の書式のProfileが追記されています。
最後に以下コマンドで作成したprofileを使用しましょう。
$ aws sso login --profile test-sso-profile
TerraformでSSO Profileを使う方法
S3 Backendのブロックと、AWS providerのブロックのprofile引数値に、先程作成したprofileの名前を指定します。
S3バックエンド設定例
terraform {
required_version = "= 0.14.9"
backend "s3" {
bucket = "kazue-hogehoge"
key = "fuga.tfstate"
region = "ap-northeast-1"
profile = "test-sso-profile"
}
}
AWS provider設定例
provider "aws" {
region = "ap-northeast-1"
profile = "test-sso-profile"
}
もしくはAWS_PROFILE
環境変数値に、前述のprofile名を格納してもOKです。
$ export AWS_PROFILE="test-sso-profile"
S3 Backendでエラーになる場合
Error: error configuring S3 Backend: no valid credential sources for S3 Backend found.
Please see https://www.terraform.io/docs/backends/types/s3.html
for more information about providing credentials.
Error: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
terraform init
時に上記の様なエラーになった場合は、TerraformのVersionを確認してください。S3 Backendの設定でSSO Profileが使えるのはVersion 0.14.6以上です。
- 関連PR: deps: Bump github.com/aws/aws-sdk-go@v1.37.0 by bflad · Pull Request #27620 · hashicorp/terraform
AWS Providerでエラーになる場合
Error: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.
Please see https://registry.terraform.io/providers/hashicorp/aws
for more information about providing credentials.
Error: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
terraform plan
もしくはterraform apply
時に上記エラーになった場合は、AWS ProviderのVersionを確認してください。AWS ProviderでSSO Profileが使えるのはVersion 3.26.0以降です。
まとめ再掲
- SSO ProfileはAWS CLI Version2でのみ使用できます。まだVersion1の方はまずVersion2をインストールしてください。
- SSO Profileの作成にはaws configure ssoコマンドが便利です。
- 作ったSSO Profile名をTerraformのコード内でprofile引数に指定するか、AWS_PROFILE環境変数に格納してTerraformを実行します。
- S3 Backend、つまりStateファイルの格納場所としてS3バケットを使う場合は、Terraform本体のVersionを0.14.6以上にしましょう。
- AWS Providerを使って各種AWSリソースをプロビジョニングする場合は、AWS ProviderのVersionを 3.26以上にしましょう。