Terraformのfor_each文はよく使うけれど、for文はほとんど使ったことがないので、試しにマップ型をfor文に渡してifフィルターをしてみた。
locals { names ={ foo = "abc" bar = "def" baz = "ghi" } } output "sample" { value = local.names } output "sample1" { value = {for k, v in local.names : v => upper(k) } } output "sample2" { value = {for k, v in local.names : k => v } } output "sample3" { value = {for k, v in local.names : k => v if (k == "foo" || k == "baz")} }
表示結果
Outputs: sample = { "bar" = "def" "baz" = "ghi" "foo" = "abc" } sample1 = { "abc" = "FOO" "def" = "BAR" "ghi" = "BAZ" } sample2 = { "bar" = "def" "baz" = "ghi" "foo" = "abc" } sample3 = { "baz" = "ghi" "foo" = "abc" }