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

Kev*_*rke 28 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.
Run Code Online (Sandbox Code Playgroud)

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

BMW*_*BMW 24

你先升级了代码吗?

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

terraform init
terraform 0.12upgrade
Run Code Online (Sandbox Code Playgroud)

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

  • 是的,我升级了代码。我仍然收到上面的错误。 (3认同)

yet*_*200 21

  Warning: Interpolation-only expressions are deprecated

  on main.tf line 3, in provider "aws":
   3:   region  = "${var.region}"
Run Code Online (Sandbox Code Playgroud)

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

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

新语法- region = var.region # 没有警告

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

  • 目前,此警告将在 `terraform fmt` 之后自动修复 (2认同)

Paw*_*iec 6

可以使用 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:
Run Code Online (Sandbox Code Playgroud)


sma*_*kov 6

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


Kev*_*rke 5

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

go get github.com/apparentlymart/terraform-clean-syntax
terraform-clean-syntax .
Run Code Online (Sandbox Code Playgroud)


小智 5

更新插值如下:

subscription_id = "${var.subscription_id}"
Run Code Online (Sandbox Code Playgroud)

subscription_id = var.subscription_id
Run Code Online (Sandbox Code Playgroud)