1.この記事で達成したいこと sensitve=trueな値をterraform outputで確認したい 1-1.お悩みポイント そもそもsensitve=trueとしている理由は、terraform applyやterraform outputする際にはCLI(ターミナル)上では出力させたくないから なので、terraform applyやterraform outputする際には表示されないのが正しい! とはいえ、確認したいときはある。さて、どうするか、、??? $ terraform output aws_iam_smtp_password_v4 = <sensitive> sensitive=trueの技術的仕様はterraform.tfstateに記載されている値のうち、出力させないそれらを指定するというもの なので以下のように確認することはできるが、チョット煩わしい。シュッとやりたい。 $ cat terraform.tfstate | jq -r .outputs { "aws_iam_smtp_password_v4": { "value": "xxxxxxxxxxxxxxxxxxxxxx", "type": "string", "sensitive": true } } このような都合のいい、「ワガママ」な事情をよしなに汲み取ってterraform outputで確認するというのが本記事の目的 2.前提 Terraformのインストール方法を始めとする初期設定は終えているものとして話を進める 今回出力したくないセキュアな値は ses_smtp_password_v4 とする サンプルコードは以下のとおり $ cat main.tf resource "aws_iam_user" "dummy" { name = "dummy" path = "/dummy/" } resource "aws_iam_access_key" "dummy" { user = aws_iam_user.dummy.name } output "aws_iam_smtp_password_v4" { value = aws_iam_access_key.dummy.ses_smtp_password_v4 sensitive = true } 3.環境情報 Terraform実行環境 $ grep VERSION= /etc/os-release VERSION="20.04.3 LTS (Focal Fossa)" $ terraform version Terraform v1.1.0 on linux_amd64 + provider registry.terraform.io/hashicorp/aws v3.70.0 4.sensitive=trueとしてterraform applyするとマスクされることを確認 $ terraform apply 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_iam_access_key.dummy will be created + resource "aws_iam_access_key" "dummy" { + create_date = (known after apply) + encrypted_secret = (known after apply) + encrypted_ses_smtp_password_v4 = (known after apply) + id = (known after apply) + key_fingerprint = (known after apply) + secret = (sensitive value) + ses_smtp_password_v4 = (sensitive value) + status = "Active" + user = "dummy" } # aws_iam_user.dummy will be created + resource "aws_iam_user" "dummy" { + arn = (known after apply) + force_destroy = false + id = (known after apply) + name = "dummy" + path = "/dummy/" + tags_all = (known after apply) + unique_id = (known after apply) } Plan: 2 to add, 0 to change, 0 to destroy. Changes to Outputs: + aws_iam_smtp_password_v4 = (sensitive value) 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 略 Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Outputs: aws_iam_smtp_password_v4 = <sensitive> # <-- マスクされている!(これでいいんだけど実際ちゃんと入っているか確認したい!!) 4-1.terraform outputコマンドを実行すると・・・(再掲) sensitve=trueと指定しているのでマスクされている $ terraform output aws_iam_smtp_password_v4 = <sensitive> 5.sensitve=trueな値をシュッと確認する(cat terraform.tfstateしない) terraform outputコマンドに -json オプションを付与すればok $ terraform output -json { "aws_iam_smtp_password_v4": { "sensitive": true, "type": "string", "value": "xxxxxxxxxxxxxxxxxxxxxx" } } もちろんjqで加工することもできる $ terraform output -json | \ > jq -r '.aws_iam_smtp_password_v4 | { aws_iam_smtp_password_v4: .value }' { "aws_iam_smtp_password_v4": "xxxxxxxxxxxxxxxxxxxxxx" } もっとシンプルにterraform output -jsonの結果を絞る方法として、terraform output -json ${KEY} もある ${KEY}はoutputで指定しているもの $ terraform output -json aws_iam_smtp_password_v4 "xxxxxxxxxxxxxxxxxxxxxx $ grep output main.tf output "aws_iam_smtp_password_v4" { 6.参考 sensitive=trueな値を出力させる方法について Note: When using the -json or -raw command-line flag, any sensitive values in Terraform state will be displayed in plain text.
...