環境ごとのリソース名の切り替え
- Terraformで環境ごとのリソース名の切り替えをする際は、Terraform 環境周りTips にあるように
workspace
サブコマンドを使えばよい - でも、その場合同じリソース名をprod/devで指定すると重複してしまうのではないかと危惧していた
構想
以下のようなリソースを作るべきかとちょっと迷った
resource "aws_iam_role" "hoge-role-${terraform.env}" { # <-- ${terraform.env}でリソース名切り替え
name = "hoge-role-${terraform.env}"
path = "/${terraform.env}/hoge/"
assume_role_policy = <<EOF
{
...
}
EOF
}
関連するStackOverflowやGitHubのトピック
- hashicorp/terraform - Can't use programmatic resource names #571
- StackOverflow - Referring to resources named with variables in Terraform
The names in Terraform are just logical for the purposes of referring to them. You want to parameterize the values inside the resource, and not the name itself, so this is by design.
Multiple environments are supported by using different variable and state files.
しかし、上記の記事にあるようにTerraformでリソース名に文字列補間できないのは仕様で、やる必要がないようだ。
同じリソース名を参照していたとしてもworkspaceが異なれば実際のARNは異なるのだろう(たぶん)