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 5 years have passed since last update.

Terraform sliceの奇妙なふるまい

Last updated at Posted at 2018-12-06

Terraformの効率化に欠かせないmap変数の一部を取り出すのに必須のslice関数ですが、なかなか他では見られないユニークな仕様なのではまらないようにメモ程度ですが投稿します。

関数の定義

slice(リスト、開始、終了)

動作

リストから指定範囲を切り出して返します

パラメータ

リスト:元になるlist
開始:0から始まるリストの切り出し開始インデックス
終了:切り出しの終了位置

どこが奇妙?

終了:切り出しの終了位置

この終了位置の一つ前のインデックスまでを切り出します。(ヲイ)

実例

普通に一つ切り出す
variable "list_01" {
  default = ["One", "Two", "Three", "Four"]
}

output "slice_01" {
  value = "${slice(var.list_01,0,1)}"
}
結果
slice_01 = [
    One
]

真ん中の二つを切り出す
variable "list_01" {
  default = ["One", "Two", "Three", "Four"]
}

output "slice_02" {
  value = "${slice(var.list_01,1,3)}"
}
結果
slice_02 = [
    Two,
    Three
]

最後の一つを切り出す
variable "list_01" {
  default = ["One", "Two", "Three", "Four"]
}

output "slice_03" {
  value = "${slice(var.list_01,3,4)}"
}
結果
slice_03 = [
    Four
]

感想

きっと、よかれと思って実装したんだと思う............

きっと、そうだよ........................

でもね、存在しないインデックスを指定するのって、すげー気持ち悪いんだけど......

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?