1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

terraformのoutputでList型とMap型で出力する方法

Posted at

やりたいこと

ループしているリソースをOutputで出力するときには、Outputもループさせて出力するのがいいと思う。
以下の場合は、Map型のリソースなので、Mapで出力してあげるほうがたぶんいい。
一応使い分けるられるように2パターン書いてます。

サンプル

locals {
  files = [
    {
      content = "aaa"
      name    = "hello1"
    },
    {
      content = "bbb"
      name    = "hello2"
    },
    {
      content = "ccc"
      name    = "hello3"
    },
  ]
}

resource "local_file" "hello" {
  for_each = { for i in local.files : i.name => i }
  content  = each.value.content
  filename = each.value.name
}

## list型で出力する例
output "list" {
  value = values(local_file.hello).*.content
}


## map型で出力する例
output "map" {
  value = {
    for k, v in local_file.hello : k => v.content
  }
}

出力

Outputs:

list = [
  "aaa",
  "bbb",
  "ccc",
]
map = {
  "hello1" = "aaa"
  "hello2" = "bbb"
  "hello3" = "ccc"
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?