4
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.

terraformAdvent Calendar 2021

Day 11

【Terraform】list, map, objectの結合を整理したい

Last updated at Posted at 2021-12-11

この記事はTerraform Advent Calendar 2021の11日目です。

これはなに

  • Terraformのlist, map, objectの結合メソッドがいくつかあるので整理したい
    • for_eachで複数リソース良さげに作ろうとした際、この辺で格闘した思い出

結合系のメソッド

試す

terraform consoleで対話モードに入って各メソッドの動作検証

concatflatten

基本的な使い方

> concat([0, 1, 2], ["a", "", "c"])
[
  0,
  1,
  2,
  "a",
  "",
  "c",
]

> flatten([[0, 1, 2], ["a", "", "c"]])
[
  0,
  1,
  2,
  "a",
  "",
  "c",
]

空のlistを結合するパターン

空のlistは結果に含まれない

> concat(["a", "b"], [], ["c"])
[
  "a",
  "b",
  "c",
]

> flatten([["a", "b"], [], ["c"]])
[
  "a",
  "b",
  "c",
]

多次元listを結合するパターン

concatは結果の要素にlistが含まれるが、flattenは含まれない

> concat(["a", "b"], [["ca", "cb"], [], ["da"]])
[
  "a",
  "b",
  [
    "ca",
    "cb",
  ],
  [],
  [
    "da",
  ],
]

> flatten([["a", "b"], [["ca", "cb"], [], ["da"]]])
[
  "a",
  "b",
  "ca",
  "cb",
  "da",
]

merge

基本的な使い方

結果はkeyでソートされる

> merge({"b"="bb"}, {"a"="aa", "c"="cc", ""="dd"}, {"0"=00, "1"=11})
{
  "" = "dd"
  "0" = 0
  "1" = 11
  "a" = "aa"
  "b" = "bb"
  "c" = "cc"
}

ダブルクォートで囲んでないkeyを持つmap、objectが含まれるパターン

結果はkeyがダブルクォートで囲まれる
valueはそのまま

> merge({a="aa", b="bb"}, {1=11, 2=22})
{
  "1" = 11
  "2" = 22
  "a" = "aa"
  "b" = "bb"
}

空のmap, objectを結合するパターン

空のは結果に含まれない

> merge({"a"="aa", "b"="bb"}, {}, {"c"="cc"})
{
  "a" = "aa"
  "b" = "bb"
  "c" = "cc"
}

重複keyの扱い

後続のkey, valueで上書きされていく(後続のものが優先される)

> merge({"a"="aa"}, {"a"="ab", "b"="bb"})
{
  "a" = "ab"
  "b" = "bb"
}

# keyにダブルクォートなしでも同じ
> merge({"a"="aa"}, {a="ab", "b"="bb"})
{
  "a" = "ab"
  "b" = "bb"
}

0の扱い

ダブルクォートで囲まないと不要な0は除去される

# 0が除去される
> merge({01=011}, {2=22, 3=33})
{
  "1" = 11
  "2" = 22
  "3" = 33
}

# 0が除去されるので後続のkey, valueが優先される
> merge({01=011}, {1=12}, {2=22, 3=33})
{
  "1" = 12
  "2" = 22
  "3" = 33
}

# 文字列扱いならば除去されない
> merge({"01"="011"}, {2=22, 3=33})
{
  "01" = "011"
  "2" = 22
  "3" = 33
}
4
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
4
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?