Terraform v0.12 多行字符串 EOF shell 风格的“here doc”语法没有像 v0.11 之前那样被解释

2024-01-23

在 Octopus Deploy 中,我使用其应用 Terraform 模板设置了 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"
        ]
      }
    }

作为连接块的一部分,我们需要使用 SSH 密钥对进行连接,使用私钥 PEM 来使用存储在 AWS 上的公钥进行身份验证

我的私钥作为变量存储在八达通部署的项目中

为了在 Terraform 中将我的私钥正确解释为多行字符串,我必须使用“here doc”语法,并使用起始 EOF 和结束 EOF

此语法解释可以在 Terraform 官方文档中找到:

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

这是我最初的问题,因为我没有正确处理多行 PEM 文件,所以我的变量语法失败了,我向 Octopus 部署支持提出了下面的问题

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

他们好心地向我指出 EOF 语法的方向

这一切在 Terraform v0.11 上都运行良好,但我们这边有很多代码是在 v0.12 中的最新 HCL2 中编写的

因此,我想强制 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 https://www.terraform.io/docs/configuration/syntax.html#terraform-syntax

我不确定 v0.11 中是否有任何关于如何管理多行的内容有帮助

这是我的 tfvars 文件中在 v0.11 中成功运行的代码块

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 https://github.com/hashicorp/terraform/pull/20281

但在撰写本文时我正在使用今天下载的最新二进制文件 v0.12.6

有人对如何让它在 v0.12 中工作有什么建议吗?干杯


“flush Heredoc”的正确语法不包括最终标记上的破折号:

aws_key_path = <<-EOF
#{martinTestPrivateKey}
EOF

如果以前的版本接受-EOF要结束heredoc,不幸的是,这是一个错误,现已在 Terraform 0.12 中修复,因此继续前进,您必须使用文档中的语法 https://www.terraform.io/docs/configuration/expressions.html#string-literals,标记仅位于最后一行。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Terraform v0.12 多行字符串 EOF shell 风格的“here doc”语法没有像 v0.11 之前那样被解释 的相关文章

随机推荐