如何即时修改地图?

时间:2018-07-19 09:15:12

标签: terraform terraform-provider-aws hcl

我需要对特定HCL映射键/值对进行更多次迭代,这些键/值对必须基于特定变量的值。

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

如果有此地图-我们将其称为“ 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_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,在前几次迭代中,区域ID必须相同,而在随后的其他迭代中,区域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 :(得分: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"]
}