Terraform结合变量和字符串

时间:2019-02-18 16:57:30

标签: terraform

我正在尝试在Terraform中完成一个相当简单的任务,但是它不起作用:

tfvars:

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。如何连接变量hosted_zoned和字符串以形成域?

1 个答案:

答案 0 :(得分:3)

您不能在tfvars文件中使用插值。

相反,您可以像这样直接将其加入Terraform中:

terraform.tfvars

hosted_zone       = "example.com"
domain            = "my"

main.tf

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 {
  local = "${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"]
}