字符串的 Terraform 插值语法 [英] Terraform interpolation syntax for string
问题描述
我正在尝试使用 terraform 为多个 DNS 记录制作 SSL 证书,遵循此处的文档:https://www.terraform.io/docs/providers/aws/r/acm_certificate_validation.html
I'm trying to make an SSL cert with terraform for multiple DNS records, following the docs here: https://www.terraform.io/docs/providers/aws/r/acm_certificate_validation.html
对于 Route 53 的记录,给出了这个例子:
For the Route 53 records is gives this example:
resource "aws_route53_record" "cert_validation" {
name = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_name}"
type = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_type}"
zone_id = "${data.aws_route53_zone.zone.id}"
records = ["${aws_acm_certificate.cert.domain_validation_options.0.resource_record_value}"]
ttl = 60
}
名称和类型中的 0
指的是他们提供的单个 DNS 条目.如果我将几个 subject_alternative_names
添加到 aws_acm_certificate
并添加几个手动 aws_route53_record
并将 0
替换为 1
2
等,它按我想要的方式工作.
Where the 0
in the name and type refer to the single DNS entry they've provided. If I add several subject_alternative_names
to the aws_acm_certificate
and add several manual aws_route53_record
with the 0
replaced by 1
2
etc, it works the way I want.
我的问题是,我可以使用 Terraform 的 count
一次性完成此操作吗?我用 count = 5
尝试了这两件事:
My question is, can I do this in one go using Terraform's count
. I've tried these two things with count = 5
:
name = "${aws_acm_certificate.cert.domain_validation_options.*.resource_record_name[count.index]}"
这抱怨它得到的是一个字符串而不是一个列表
This complains that it's getting a string and not a list
name = "${aws_acm_certificate.cert.domain_validation_options.count.index.resource_record_name}"
这给了他们一个相同的名字,只是5".
This gives all of them the same name, and it's just "5".
设置:
resource "aws_route53_record" "cert_validation" {
count = 5
name = "${aws_acm_certificate.cert.domain_validation_options.*.resource_record_name[count.index]}"
type = "CNAME"
zone_id = "myzoneid"
records = ["${aws_acm_certificate.cert.domain_validation_options.*.resource_record_value[count.index]}"]
ttl = 60
}
错误:
* aws_route53_record.cert_validation: 5 error(s) occurred:
* aws_route53_record.cert_validation[4]: At column 95, line 1: invalid index operation into non-indexable type: TypeString in:
${aws_acm_certificate.cert.domain_validation_options.*.resource_record_value[count.index]}
* aws_route53_record.cert_validation[2]: At column 94, line 1: invalid index operation into non-indexable type: TypeString in:
${aws_acm_certificate.cert.domain_validation_options.*.resource_record_name[count.index]}
* aws_route53_record.cert_validation[3]: At column 94, line 1: invalid index operation into non-indexable type: TypeString in:
${aws_acm_certificate.cert.domain_validation_options.*.resource_record_name[count.index]}
* aws_route53_record.cert_validation[0]: At column 95, line 1: invalid index operation into non-indexable type: TypeString in:
${aws_acm_certificate.cert.domain_validation_options.*.resource_record_value[count.index]}
* aws_route53_record.cert_validation[1]: At column 94, line 1: invalid index operation into non-indexable type: TypeString in:
${aws_acm_certificate.cert.domain_validation_options.*.resource_record_name[count.index]}
推荐答案
经过多次反复试验,解决方案是这样的:
After a bunch more trial and error, the solution was this:
resource "aws_route53_record" "cert_validation" {
count = 5
name = "${lookup(aws_acm_certificate.cert.domain_validation_options[count.index], "resource_record_name")}"
type = "CNAME"
zone_id = "myzoneid"
records = ["${lookup(aws_acm_certificate.cert.domain_validation_options[count.index], "resource_record_value")}"]
ttl = 60
}
这篇关于字符串的 Terraform 插值语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!