我需要在特定的 HCL 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_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 记录,以便正确验证域证书。
这是用于实现总体目标的代码 - 问题在于 Zone 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"]
}