首页 文章

如何即时修改 Map ?

提问于
浏览
0

我需要在特定的HCL Map键/值对上迭代更多次,这需要基于特定变量的值 .

我想到了修改当前 Map 的想法 - 这样某些键/值将被迭代多次 .

如果我们有这张 Map - 我们称之为“map_domains”:

key_1 = value_1
key_2 = value_2

我们设置了这些变量:

variable "domains" {
  type = "list"

  default = [
    "key_1",
    "key_2",
  ]
}

variable "domain_alt_names" {
  type = "map"

  default = {
    key_1    = "value_1, value_2"
    key_2    = "value_3, value_4, value_5"
  }
}

我们如何将 Map “map_domains”修改为:

key_1 = value_1
key_1 = value_1
key_1 = value_1
key_2 = value_2
key_2 = value_2
key_2 = value_2
key_2 = value_2

我正在尝试通过DNS验证选项验证几个AWS ACM证书 - 并且每个域都有一些域备用名称,它们还需要在Route53中创建DNS记录,以便正确验证域证书 .

这是用于实现总体目标的代码 - 问题在于区域ID在前几次迭代中需要相同,然后在其余迭代中需要另一个 .

这一行:

zone_id = "${lookup(local.hosted_zone_ids_zipmap, element(keys(local.hosted_zone_ids_zipmap), count.index))}"

整个代码:

#
# EKS Worker Nodes Resources
#  * Issuing ACM certificates
#

resource "aws_route53_zone" "zones" {
  count = "${length(var.domains)}"

  name = "${element(var.domains, count.index)}"
}

locals {
  hosted_zone_ids_zipmap = "${zipmap(var.domains, aws_route53_zone.zones.*.zone_id)}"
}

resource "aws_acm_certificate" "cert" {
  count = "${length(var.domains)}"

  domain_name = "${element(keys(local.hosted_zone_ids_zipmap), count.index)}"

  subject_alternative_names = ["${
  lookup(var.domain_alt_names,
  "${element(var.domains, count.index)}")
  }"]

  validation_method = "DNS"

  tags {
    Domain = "${element(keys(local.hosted_zone_ids_zipmap), count.index)}"
  }
}

locals {
  dvo           = "${flatten(aws_acm_certificate.cert.*.domain_validation_options)}"
}

resource "aws_route53_record" "cert_validation" {
  count = "${length(var.domain_alt_names) + length(var.domains)}"

  zone_id = "${lookup(local.hosted_zone_ids_zipmap, element(keys(local.hosted_zone_ids_zipmap), count.index))}"
  name    = "${lookup(local.dvo[count.index], "resource_record_name")}"
  type    = "${lookup(local.dvo[count.index], "resource_record_type")}"
  records = ["${lookup(local.dvo[count.index], "resource_record_value")}"]
  ttl     = 60

  depends_on = ["aws_acm_certificate.cert"]
}

resource "aws_acm_certificate_validation" "cert" {
  count = "${length(var.domains)}"

  certificate_arn         = "${aws_acm_certificate.cert.*.arn[count.index]}"
  validation_record_fqdns = ["${aws_route53_record.cert_validation.*.fqdn[count.index]}"]

  depends_on = ["aws_acm_certificate.cert", "aws_route53_record.cert_validation"]
}

1 回答

  • 0

    我弄清楚了:

    (1)添加了这个变量:

    variable "domain_names_index" {
      // A flat map that will act as nested map
      //// for the subdomains and the alternative domain names
      //// so that the Hosted Zone ID can be calculated in a reverse order
      //// during the creation of the DNS Validation Route53 records
    
      type = "map"
    
      default = {
        tftestingdatorama.io  = "2"
        tftestingdatorama.org = "2"
        tftestingdlite.co     = "1"
        tftestingdlite.org    = "1"
      }
    }
    

    (2)然后我将代码更改为:

    resource "aws_route53_record" "cert_validation" {
      count = "${length(var.domain_alt_names) + length(var.domains)}"
    
      zone_id = "${
         lookup(local.hosted_zone_ids_zipmap,
         element(keys(local.hosted_zone_ids_zipmap),
         lookup(var.domain_names_index, "${lookup(local.dvo[count.index], "domain_name")
         }")))}"
    
      name    = "${lookup(local.dvo[count.index], "resource_record_name")}"
      type    = "${lookup(local.dvo[count.index], "resource_record_type")}"
      records = ["${lookup(local.dvo[count.index], "resource_record_value")}"]
      ttl     = 60
    
      depends_on = ["aws_acm_certificate.cert"]
    }
    

相关问题