【问题标题】:Fix "Interpolation-only expressions are deprecated" warning in Terraform修复 Terraform 中的“仅插值表达式已弃用”警告
【发布时间】:2020-03-21 03:22:27
【问题描述】:

我升级到 Terraform v0.12.16,现在我收到很多类似这样的消息:

Warning: Interpolation-only expressions are deprecated

  on ../modules/test-notifier/test_notifier.tf line 27, in resource "aws_sns_topic_policy" "default":
  27:   arn    = "${aws_sns_topic.default.arn}"

Terraform 0.11 and earlier required all non-constant expressions to be
provided via interpolation syntax, but this pattern is now deprecated. To
silence this warning, remove the "${ sequence from the start and the }"
sequence from the end of this expression, leaving just the inner expression.

Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.

有数百条这样的消息。是否有自动修复它们的方法?

【问题讨论】:

    标签: terraform


    【解决方案1】:
      Warning: Interpolation-only expressions are deprecated
    
      on main.tf line 3, in provider "aws":
       3:   region  = "${var.region}"
    

    我也收到了上述警告,这是由于在 terraform 中声明变量的语法发生了变化。 请参见下面的示例 - :

    旧语法- region = "${var.region}" # 你会得到 Interpolation-only 警告

    New Synatx- region = var.region # 无警告

    检查语法并使用任何代码编辑器进行更正。

    【讨论】:

      【解决方案2】:

      您是否先升级代码?

      Terraform 0.11 与 0.12 不兼容,因此您必须先升级它。

      terraform init
      terraform 0.12upgrade
      

      如果您的 Terraform 代码正在调用其他 terraform 模块,请确保您也已将这些 terraform 模块升级到 0.12。

      【讨论】:

      • 是的,我升级了代码。我仍然收到上述错误。
      • 澄清一下,升级模块意味着在模块根目录中运行相同的 2 个命令,例如 .terraform/modules/
      【解决方案3】:

      更新插值如下:

      subscription_id = "${var.subscription_id}"
      

      subscription_id = var.subscription_id
      

      【讨论】:

        【解决方案4】:

        Terraform v14fmt 合并了这个。您现在可以获取rc 二进制文件并简单地运行terraform fmt

        【讨论】:

          【解决方案5】:

          可以使用 Martin Atkins 的 terraform-clean-syntax 代码(感谢 Kevin Burke 提供线索)

          我无耻地使用它并将其打包在 docker 容器中,因此它可以轻松地在非 linux_amd64 机器上运行,例如macOS:

          https://github.com/NoLedgeTech/terraform-clean-syntax-docker

          TL&DR(警告 - 这将更新您的 tf 文件):

          docker pull pniemiec/terraform-clean-syntax-docker
          cd <DIRECTORY_WITH_TF_FILES>
          terraform init
          terraform plan    # This shows a lot of warnings
          docker run --rm -v $(pwd):/code -t pniemiec/terraform-clean-syntax-docker
          terraform plan    # This does not show a lot of warnings :sweat_smile:
          

          【讨论】:

          • 这个工具很有帮助,但请注意,它不会对数组中的值进行反插值(例如,subnet_ids)。为此,我不得不回退到仔细应用正则表达式替换:"\$\{([^}"]+)\}" 由于此处文档中的插值,它不能盲目应用。
          • 鱼壳,docker run --rm -v $(pwd):/code -t pniemiec/terraform-clean-syntax-docker.
          【解决方案6】:

          此工具会自动为您去除开头和结尾的引号和大括号,从而修复警告:https://github.com/apparentlymart/terraform-clean-syntax

          go get github.com/apparentlymart/terraform-clean-syntax
          terraform-clean-syntax .
          

          【讨论】:

          • 获取:panic: didn't find any token of type TokenOBrack
          【解决方案7】:

          或者你可以使用一个简单的sed:

          sed -i 's/\"\${/\"/g;s/}\"/\"/g' main.tf
          

          【讨论】:

          • 工作得非常好。我使用 *.tf 而不是 main.tf,它更新了所有文件
          • 实际上,您将字符串替换为"。相反,您希望将其替换为任何内容:sed -i 's/\"\${//g;s/}\"//g' *tf。您的结果将private_key = "${file("ssh_key")}" 变为private_key = "file("ssh_key")",这是无效的。您希望结果为private_key = file("ssh_key")
          【解决方案8】:

          我使用 notepad++ 删除了插值语法。

          正则表达式:

          ^(.*)\${(.*)}
          

          替换为:

          \1\2 
          

          【讨论】:

          • 在 VScode 中我使用 "\$\{([^$]*)\}" 并替换为 $1
          【解决方案9】:

          你使用了 => arn "${aws_sns_topic.default.arn}"

          尽量不使用插值,比如:arn = aws_sns_topic.default.arn

          【讨论】:

            【解决方案10】:

            已经在日志输出中的解决方案:... remove the "${ sequence from the start and the }" ...

            【讨论】:

              猜你喜欢
              • 2015-12-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-07-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多