【发布时间】:2021-02-13 03:07:18
【问题描述】:
我正在使用 Terraform v0.13.5。如果我为在 AWS 中注册的域创建单个 AWS 证书资源,我还可以使用以下方法成功创建 Route53 DNS 验证记录:
resource "aws_acm_certificate" "api" {
domain_name = "api.example.com"
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "api_validation" {
for_each = {
for dvo in aws_acm_certificate.api.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = data.aws_route53_zone.example.zone_id # already exists
}
但是,我想使用带有映射变量的 for_each 创建多个证书。我使用以下方法定义了aws_acm_certificate 资源:
variable "sub_domains" {
type = map
default = {
"api" = "api"
"api_test" = "api.test"
}
}
resource "aws_acm_certificate" "certs" {
for_each = var.sub_domains
domain_name = "${each.value}.example.com"
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
我不知道如何引用定义验证记录时创建的动态证书。 以下 sn-p 现在不起作用:
resource "aws_route53_record" "api_validation" {
for_each = {
for dvo in aws_acm_certificate.certs.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = data.aws_route53_zone.myzone.zone_id # already exists
}
Terraform 报错如下:
Because aws_acm_certificate.certs has "for_each" set, its attributes must be
accessed on specific instances.
那么如何为每个动态创建的证书获取domain_validation_options?
【问题讨论】:
标签: amazon-web-services foreach terraform aws-certificate-manager