如何在飞行中修改地图



我需要对特定的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,它需要在前几次迭代中保持相同,然后在其余迭代中保持另一个。

此行:

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( 添加了这个变量:

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