我正在尝试使用for_each在Terraform中创建多个别名DNS记录。指定别名的名称和区域ID时出现错误。我的问题是如何从定义为一组字符串的变量中读取?下面是我的代码块和定义的变量:
resource "aws_route53_record" "alias_records" {
for_each = var.alias_names
zone_id = aws_route53_zone.zone.zone_id
name = each.key
type = "A"
alias {
name = var.alias_dns_names[each.key]
zone_id = var.alias_zone_ids[each.key]
evaluate_target_health = false
}
}
variable "alias_names" {
type = set(string)
}
variable "alias_dns_names" {
type = set(string)
}
variable "alias_zone_ids" {
type = set(string)
}
Error: Invalid index
on alias.tf line 8, in resource "aws_route53_record" "alias_records":
8: name = var.alias_dns_names[each.key]
This value does not have any indices.
Error: Invalid index
on alias.tf line 9, in resource "aws_route53_record" "alias_records":
9: zone_id = var.alias_zone_ids[each.key]
This value does not have any indices.
alias_names = [
"alias1",
"alias2",
"alias3"
}
alias_dns_names = [
"alias_dns_1",
"alias_dns_2",
"alias_dns_3"
}
alias_zone_ids = [
"alias_zone_1",
"alias_zone_2",
"alias_zone_3"
}
答案 0 :(得分:0)
集合数据结构是无序的,其元素除其值外没有其他标识符,因此集合类型不适用于您的用例。
由于您似乎在这里描述了同一基础对象的三个不同方面,所以我认为从模块中公开该内容的最便捷方法是将其作为对象映射,如下所示:
variable "aliases" {
type = map(object({
zone_id = string
dns_name = string
}))
}
然后,您可以单独使用此变量来捕获每个别名记录所需的所有三个值:
aliases = {
alias1 = {
zone_id = "alias_zone_1"
dns_name = "alias_dns_1"
}
alias2 = {
zone_id = "alias_zone_2"
dns_name = "alias_dns_2"
}
alias3 = {
zone_id = "alias_zone_3"
dns_name = "alias_dns_3"
}
}
任何数据类型的映射都可以直接与资源for_each
一起使用,其中each.key
代表映射键,each.value
代表值。因此,您可以像这样更改资源块:
resource "aws_route53_record" "alias_records" {
for_each = var.aliases
zone_id = aws_route53_zone.zone.zone_id
name = each.key
type = "A"
alias {
zone_id = each.value.zone_id
name = each.value.dns_name
evaluate_target_health = false
}
}
由于地图的元素是对象,因此可以从each.value
访问它们,以获取与原始地图中的每个键相对应的zone_id
和dns_name
值。