gpt4 book ai didi

Terraform 组合变量和字符串

转载 作者:行者123 更新时间:2023-12-04 11:10:28 25 4
gpt4 key购买 nike

我试图在 Terraform 中做一个相当简单的任务,但它不起作用:

变量:

hosted_zone       = "example.com"
domain = "my.${var.hosted_zone}"

route_53_record:
resource "aws_route53_record" "regional" {
zone_id = "${data.aws_route53_zone.selected.zone_id}"
name = "${var.domain}"
type = "A"
ttl = "300"
records = ["4.4.4.4"]
}

当我跑 terraform plan我得到这个:
+ aws_route53_record.regional
id: <computed>
allow_overwrite: "true"
fqdn: <computed>
name: "my.${var.hosted_zone}"
records.#: "1"
records.3178571330: "4.4.4.4"
ttl: "300"
type: "A"
zone_id: "REDACTED"

域应该是 my.example.com .如何加入变量 hosts_zoned 和一个字符串以形成域?

最佳答案

您不能在 tfvars 文件中使用插值。
相反,您可以像这样直接在 Terraform 中加入它:
terraform.tfvars

hosted_zone = "example.com"
domain = "my"
主文件
resource "aws_route53_record" "regional" {
zone_id = data.aws_route53_zone.selected.zone_id
name = "${var.domain}.${var.hosted_zone}"
type = "A"
ttl = "300"
records = ["4.4.4.4"]
}
或者,如果您总是需要将这些东西组合在一起,您可以使用本地:
locals {
domain = "${var.domain}.${var.hosted_zone}"
}

resource "aws_route53_record" "regional" {
zone_id = data.aws_route53_zone.selected.zone_id
name = local.domain
type = "A"
ttl = "300"
records = ["4.4.4.4"]
}

关于Terraform 组合变量和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54752049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com