terraformでrandom_passwordで作ったパスワードを参照する

またあとで自分で調べそうなのでメモ。

terraformにはrandom_passwordという便利なリソースがあって、ランダムパスワードを生成してRDSの起動時などに使うことができる。

https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password

このパスワードを一時的に参照したいときは、backendのS3のファイルを直接参照するなどすればもちろん見れるわけだけど、CLIだけで済ませたい。

outputとnonsensitiveの組み合わせでいける。

output "master_password" {
  value = nonsensitive(random_password.master.result)
}

これでterraform planすることで表示される。

Changes to Outputs:
  + master_password = "xxxxxxxxxxxx"

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

terraform applyはせずに消す。

terraform stateに含まれることには違いないので、初期構築時にだけ使い、あとでパスワードは必ず変更する。