Terraform v0.12中的多行字符串EOF shell样式“here doc”语法与v0.11不同,未被解释。

13
在 Octopus Deploy 中,我已经使用他们的“Apply a Terraform template”设置了一个 Terraform 应用步骤。
在我的 Terraform main.tf 文件中,我想使用连接,在 AWS 上运行 Amazon Linux EC2 实例上的远程执行。
    resource "aws_instance" "nginx" {
      ami           = "${var.aws_ami}"
      instance_type = "t2.nano"
      key_name      = "${var.key_name}"

      connection {
        type        = "ssh"
        user        = "ec2-user"
        private_key = "${var.aws_key_path}"
      }

      provisioner "remote-exec" {
        inline = [
          "sudo amazon-linux-extras install epel -y",
          "sudo yum update -y",
          "sudo amazon-linux-extras install nginx1.12 -y",
          "sudo systemctl enable nginx.service",
          "sudo systemctl start nginx.service",
          "sudo systemctl status nginx.service"
        ]
      }
    }

作为连接块的一部分,我们需要使用私钥PEM进行连接,以使用存储在AWS上的公钥进行身份验证。
我的私钥存储在Octopus Deploy项目中的变量中。
为了使我的私钥在Terraform中被正确解释为多行字符串,我必须使用“here doc”语法,使用起始EOF和结束EOF。
此语法说明可以在Terraform官方文档上找到。

https://www.terraform.io/docs/configuration-0-11/syntax.html

这是我的原始问题,我的变量语法出现问题,因为我没有正确处理多行PEM文件,我向Octopus Deploy支持提出了以下问题

https://help.octopus.com/t/terraform-apply-step-pem-variable-set-to-unix-lf-ucs-2-le-bom/23659

他们友好地指引我找到EOF语法所在的位置。
这在Terraform v0.11上运行得很好,但我们这边有很多代码都是用最新版本HCL2写的,即v0.12。
因此,我想强制Octopus Deploy使用v0.12二进制文件,而不是预装的Octopus Deploy v0.11。他们提供了一个内置的特殊变量,以便您可以使用不同的二进制文件。
但是当我使用这个二进制文件运行它时,脚本会出错,错误如下:
Error: Unterminated template string
No closing marker was found for the string. 
August 6th 2019 14:54:07 Error
Calamari.Integration.Processes.CommandLineException: The following command: "C:\Program Files\Octopus Deploy\Octopus\bin\terraform.exe" apply -no-color -auto-approve -var-file="octopus_vars.tfvars" 
August 6th 2019 14:54:07 Error
With the working directory of: C:\Octopus\Work\20190806135350-47862-353\staging 
August 6th 2019 14:54:07 Error
Failed with exit code: 1 
August 6th 2019 14:54:07 Error
Error: Unterminated template string 
August 6th 2019 14:54:07 Error
  on octopus_vars.tfvars line 34:

我已经查看了v0.12的官方文档。

https://www.terraform.io/docs/configuration/syntax.html#terraform-syntax

我不确定是否有任何东西可以帮助管理多行,这与他们在v0.11中所拥有的相同。
以下是从我的tfvars文件中成功运行的代码块。
aws_ami = "#{ami}"
key_name = "#{awsPublicKey}"
aws_private_key = <<-EOF
#{testPrivateKey}
-EOF

当我使用最新版本的Terraform v0.12.6运行此命令时,期望的结果是它能正常运行并在Octopus Deploy中运行我的Terraform Apply。
我希望Hashicorp的某个人有一种解决方法,因为我看到这应该已经通过https://github.com/hashicorp/terraform/pull/20281修复了。
但是我正在使用最新的二进制文件v0.12.6,在今天下载的。
有没有任何建议可以让它在v0.12中正常工作?谢谢。

Terraform 0.12在heredoc方面存在许多问题。 - Matt Schuchard
1个回答

19

“Flush heredoc” 的正确语法不包括在结束标记上加上破折号:

aws_key_path = <<-EOF
#{martinTestPrivateKey}
EOF

如果之前版本接受-EOF来结束heredoc,那么这是一个不幸的bug,已经在Terraform 0.12中修复,所以从现在开始,您必须使用文档中记录的语法,并将标记单独放在最后一行。


嗨,马丁 - 感谢您的快速回复。根据您提供的文档链接,我已将我的代码块更改为以下内容:aws_key_path = <<EOT #{martinTestPrivateKey} EOT。这似乎解决了问题,我的Octopus Deploy Terraform Apply步骤现在可以使用v0.12二进制语法正确运行。再次感谢 :) - Martin Woods

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接