I want to define a list of maps inside a variable so I can use for_each on multiple resources with conditionals based on the key values.
For example I have this locals.tf file where I define the list of maps
locals {
networking = [
{
name = "first"
domain = "first.${local.dns_name}"
port = 8080
group = "eighties"
},
{
name = "second"
domain = "second.${local.dns_name}"
port = 8081
group = "eighties"
},
{
name = "third"
domain = "third.${local.dns_name}"
port = 9090
group = "nineties"
},
{
name = "fourth"
port = 9091
group = "nineties"
}
]
}
In my other file, I can loop through the list of maps with for_each and for arguments:
resource "google_dns_record_set" "dns_records" {
for_each = {
for k in local.networking: k.domain => k
if k.domain != null
}
name = each.value.domain
type = "A"
ttl = 300
managed_zone = var.managed_zone
project = var.dns_project
rrdatas = [google_compute_global_address.default-forwarding-address.address]
}
Given this setup, I have 2 different situations that I might discuss:
Is it possible to skip the resource creations if domain does not exist in the fourth map? Or simply skip the errors.
Error: Unsupported attribute on dns-record.tf line 4, in resource "google_dns_record_set" "dns_records": 4: if k.domain.= null This object does not have an attribute named "domain".
k.group == "eighties"
so I can target a specific group, I receive this duplication error. Two different items produced the key "eighties" in this 'for' expression. If duplicates are expected, use the ellipsis (...) after the value expression to enable grouping by key.
Are this errors manageable with the current setup or should I drop the list of maps idea?
You can check if the domain
exist as follows:
{
for k in local.networking: k.domain => k
if contains(keys(k), "domain")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.