英文:
Conditionally add object to array
问题
我尝试有条件地将一个对象添加到数组中
我有这段代码:
module "r53_records" {
for_each = var.s3_static_websites[terraform.workspace]
source = "terraform-aws-modules/route53/aws//modules/records"
version = "~> 2.0"
zone_name = each.value.zone
records = [
{
name = each.value.sub_domain
type = "CNAME"
records = [module.cdn[each.key].cloudfront_distribution_domain_name]
ttl = 300
},
// 仅在 each.value.create_wild_card == true 时添加此对象
{
name = "*"
type = "CNAME"
records = [module.cdn[each.key].cloudfront_distribution_domain_name]
ttl = 300
}
]
}
我看不到可以根据 foreach 中的值有条件地添加通配符 Route53 记录的方法。
英文:
I am trying to conditionally add an object to an array
I have this code:
module "r53_records" {
for_each = var.s3_static_websites[terraform.workspace]
source = "terraform-aws-modules/route53/aws//modules/records"
version = "~> 2.0"
zone_name = each.value.zone
records = [
{
name = each.value.sub_domain
type = "CNAME"
records = [module.cdn[each.key].cloudfront_distribution_domain_name]
ttl = 300
},
// Only add this object if each.value.create_wild_card == true
{
name = "*"
type = "CNAME"
records = [module.cdn[each.key].cloudfront_distribution_domain_name]
ttl = 300
}
]
}
I don't see a method I can use to onlyadd the wildcard route53 record based upon a value in the foreach
答案1
得分: 4
以下是代码部分的翻译:
Basically we need to dynamically construct the elements of the `records` module parameter value. We want one mandatory element with one optional element. Therefore, we need to approach this by `concat` one constant `list(object)` with an optional `list(object)` based on a ternary conditional with your specified logic:
records = concat(
[{
name = each.value.sub_domain
type = "CNAME"
records = [module.cdn[each.key].cloudfront_distribution_domain_name]
ttl = 300
}],
each.value.create_wild_card == true ?
[{
name = "*"
type = "CNAME"
records = [module.cdn[each.key].cloudfront_distribution_domain_name]
ttl = 300
}]
: []
)
英文:
Basically we need to dynamically construct the elements of the records
module parameter value. We want one mandatory element with one optional element. Therefore, we need to approach this by concat
one constant list(object)
with an optional list(object)
based on a ternary conditional with your specified logic:
records = concat(
[{
name = each.value.sub_domain
type = "CNAME"
records = [module.cdn[each.key].cloudfront_distribution_domain_name]
ttl = 300
}],
each.value.create_wild_card == true ?
[{
name = "*"
type = "CNAME"
records = [module.cdn[each.key].cloudfront_distribution_domain_name]
ttl = 300
}]
: []
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论