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


11

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

这些消息有数百种。有没有一种自动的方法来修复它们?

Answers:


13

您是否先升级了代码?

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

terraform init
terraform 0.12upgrade

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


3
是的,我升级了代码。我仍然收到上面的错误。
凯文·伯克

为了明确起见,升级模块意味着在模块根目录中运行相同的2条命令,例如.terraform / modules / <module_name>
Kim Sant

4

可以使用Martin Atkins的terraform-clean语法代码(感谢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)进行插值。为此,我不得不退后一步,仔细地应用正则表达式替代:"\$\{([^}"]+)\}"由于在本文档中进行插值,因此不能盲目地应用它。
Trevor Robinson


0

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

正则表达式:

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

用。。。来代替:

\1\2 

-2

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

sed -i 's/\"\${/\"/g;s/}\"/\"/g' main.tf
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.