修复Terraform中的“不建议仅使用插值表达式”警告

时间:2019-11-25 19:04:33

标签: terraform

我已升级到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.

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

9 个答案:

答案 0 :(得分:4)

  Warning: Interpolation-only expressions are deprecated

  on main.tf line 3, in provider "aws":
   3:   region  = "${var.region}"

我也收到了以上警告,这是由于更改了以terraform声明变量的语法所致。 参见下面的示例-:

旧语法-region =“ $ {var.region}”#您将获得仅插值警告

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

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

答案 1 :(得分:4)

更新插值如下:

subscription_id = "${var.subscription_id}"

subscription_id = var.subscription_id

答案 2 :(得分:3)

您先升级了代码吗?

Terraform 0.11与0.12不兼容,因此您必须先对其进行升级。

terraform init
terraform 0.12upgrade

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

请同时浏览terraform文档

https://www.terraform.io/upgrade-guides/0-12.html

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

可以使用马丁·阿特金斯(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:

答案 5 :(得分:1)

Terraform v14 fmt合并了此内容。现在,您可以获取rc二进制文件,只需运行terraform fmt

答案 6 :(得分:0)

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

正则表达式:

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

替换为:

\1\2 

答案 7 :(得分:0)

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

尝试不使用插值使用,例如:arn = aws_sns_topic.default.arn

答案 8 :(得分:-2)

或者您可以使用简单的sed:

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