简体   繁体   English

使用 for_each 在 Terraform 中创建多个别名记录

[英]Creating multiple Alias records in Terraform with for_each

I am trying to create multiple alias DNS records in terraform using for_each.我正在尝试使用 for_each 在 terraform 中创建多个别名 DNS 记录。 I am getting an error when specifying the name and zone ID of the Alias name.指定别名的名称和区域 ID 时出现错误。 My question is how do I read from a variable defined as a set of strings?我的问题是如何从定义为一组字符串的变量中读取? Below is my code block and variables defined:下面是我定义的代码块和变量:

    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"
    }

A set data structure is not ordered and its elements don't have any identifiers other than their values, so the set type is not appropriate for your use-case here.集合数据结构是无序的,并且它的元素除了它们的值之外没有任何标识符,因此集合类型不适合您的用例。

Since you seem to be describing three different aspects of the same underlying object here, I think the most convenient way to expose that from your module would be as a map of objects, like this:由于您似乎在这里描述了相同底层 object 的三个不同方面,我认为从您的模块中公开它的最方便的方法是作为对象的 map,如下所示:

variable "aliases" {
  type = map(object({
    zone_id  = string
    dns_name = string
  }))
}

You can then use this variable alone to capture all three of the values you need for each alias record:然后,您可以单独使用此变量来捕获每个别名记录所需的所有三个值:

  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"
    }
  }

A map of any data type can be used directly with resource for_each , with each.key representing the map key and each.value representing the value.任何数据类型的 map 都可以直接与资源for_each一起使用,其中each.key表示 map 键, each.value表示值。 So you can then change your resource block like this:因此,您可以像这样更改资源块:

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
  }
}

Because the elements of the map are objects, you can access them from each.value to get the zone_id and dns_name values that correspond with each key in the original map.因为 map 的元素是对象,所以可以从each.value访问它们,以获取与原始 map 中的每个键对应的zone_iddns_name值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM