🪡
【terraform】setproduct使い方
概要
terrafromの関数setproductの使い方備忘録
要約
複数のリストを1つのリストにまとめることができる
例
- リスト1つ目
["192.168.1.0/24","192.168.2.0/24"]
- リスト2つ目
["ap-northeast-1a","ap-northeast-1c"]
-
subnets
というリストを上記2つのリストから作成[["192.168.1.0/24","ap-northeast-1a"],["192.168.2.0/24","ap-northeast-1c"]]
code
locals {
ip_ranges = ["192.168.1.0/24","192.168.2.0/24"]
az_kinds = ["ap-northeast-1a","ap-northeast-1c"]
subnets = [
for i in setproduct(local.ip_ranges, local.az_kinds) : {
ip_range = i[0]
az = i[1]
}
]
}
resource "aws_subnet" "this" {
for_each = {
for j in local.subnets : "${j.ip_range} "{j.az}" => p
}
vpc_id = var.vpc_id
cidr_block = each.value.ip_range
availability_zone = each.value.az
}
留意点
localsでsetproductを使用する際に、未使用のリソースが含まれていると弾かれる!!
静的な値を使用するか、2回に分けて作れと指摘されます
エラーメッセージ
The "for_each" map includes keys derived from resource attributes that cannot be determined until
apply, and so Terraform cannot determine the full set of keys that will identify the instances of
this resource.
When working with unknown values in for_each, it's better to define the map keys statically in your
configuration and place apply-time results only in the map values.
Alternatively, you could use the -target planning option to first apply only the resources that the
for_each value depends on, and then apply a second time to fully converge.
Discussion