🐕
TerraformをAWS SSO認証で利用する
この記事の目的
AWS SSO Login機能を利用してTerraform applyしたく、設定手順の備忘録
なぜ作ろうと思ったか?
いままでは ~/.aws/credentials
に認証情報を記載していたが、AWS SSO Loginを実施すれば
より手軽にTerraformでAWS構築ができるようになったっぽいのでAWS SSO Login利用環境に変えてみた。
その手順を忘れないように記載しておきます。
AWS SSO Profileの設定
SSO Profileの例は以下です。これを~/.aws/config
に記載します。
aws configure sso
コマンドで質問形式で作れるのですが、ここでは自分で書いてしまいます。
[profile hogehoge]
sso_account_id = 123456789012 # ご自身のAWS Account ID 12桁
region = ap-norheast-1 # 利用するRegion
output = json # 利用するアウトプットフォーマット
sso_start_url = https://x-1234567890.awsapps.com/start/# # 後述箇所でご自身のURLを把握
sso_region = ap-northeast-1
sso_role_name = AdministratorAccess # adminアクセスしたい
sso_start_url
は下記でわかります。
sso_role_name
はAWS access portalにある箇所から選びます。
sso loginして一時認証情報を取得する
続いて、このprofile hogehogeの一時認証情報を取得します。
aws sso login
というコマンドを使います。
# aws sso login --profile hogehoge
するとブラウザが立ち上がり認証画面になりますので承認して進めます。
コマンドプロンプトにはこんな感じが表示されます。
=> # aws sso login --profile hogehoge 14:49:06
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://x-1234567890.awsapps.com/start/#/device
Then enter the code:
xxxx-yyyy
Successfully logged into Start URL: https://x-1234567890.awsapps.com/start/#
terraform
今回はSQSを作ってみます。main.tf
をこんな感じにします。
provider "aws" {
region = "ap-northeast-1"
profile = "hogehoge"
}
resource "aws_sqs_queue" "my_queue" {
name = "test-queuq"
max_message_size = 2048
tags = {
"name" = "test-queue"
}
}
profile
の箇所にSSO認証を行ったprofile名を記載します。
これで、terraform init, plan, applyを実行します。
=> % terraform apply 15:22:12
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_sqs_queue.my_queue will be created
+ resource "aws_sqs_queue" "my_queue" {
+ arn = (known after apply)
+ content_based_deduplication = false
+ deduplication_scope = (known after apply)
+ delay_seconds = 0
+ fifo_queue = false
+ fifo_throughput_limit = (known after apply)
+ id = (known after apply)
+ kms_data_key_reuse_period_seconds = (known after apply)
+ max_message_size = 2048
+ message_retention_seconds = 345600
+ name = "test-queuq"
+ name_prefix = (known after apply)
+ policy = (known after apply)
+ receive_wait_time_seconds = 0
+ redrive_allow_policy = (known after apply)
+ redrive_policy = (known after apply)
+ sqs_managed_sse_enabled = (known after apply)
+ tags = {
+ "name" = "test-queue"
}
+ tags_all = {
+ "name" = "test-queue"
}
+ url = (known after apply)
+ visibility_timeout_seconds = 30
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
aws_sqs_queue.my_queue: Creating...
aws_sqs_queue.my_queue: Still creating... [10s elapsed]
aws_sqs_queue.my_queue: Still creating... [20s elapsed]
aws_sqs_queue.my_queue: Creation complete after 25s [id=https://sqs.ap-northeast-1.amazonaws.com/123456789012/test-queuq]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
できました!
Discussion