Fix “Interpolation-only expressions are deprecated

2020-06-08 19:13发布

I upgraded to Terraform v0.12.16 and now I am getting a lot of messages that look like this:

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.

There are hundreds of these messages. Is there an automated way to fix them?

标签: terraform
5条回答
来,给爷笑一个
2楼-- · 2020-06-08 19:14

Did you upgrade the code first?

Terraform 0.11 isn't compatible with 0.12, so you have to upgrade it first.

terraform init
terraform 0.12upgrade

If your Terraform code is calling other terraform modules, please make sure you have upgraded these terraform modules to 0.12 as well.

查看更多
仙女界的扛把子
3楼-- · 2020-06-08 19:14

I used notepad++ to remove that interpolation syntax.

regex:

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

replace_with:

\1\2 
查看更多
欢心
4楼-- · 2020-06-08 19:23

Martin Atkins's terraform-clean-syntax code can be used (thanks Kevin Burke for clue)

I have shamelessly used it and packaged within docker container so it can be easily run on non linux_amd64 machines, e.g. MacOS:

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

TL&DR (WARNING - this will update your tf files in place):

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:
查看更多
Lonely孤独者°
5楼-- · 2020-06-08 19:26

This tool will automatically strip the beginning and ending quotes and braces for you, which fixes the warnings: https://github.com/apparentlymart/terraform-clean-syntax

go get github.com/apparentlymart/terraform-clean-syntax
terraform-clean-syntax .
查看更多
时光不老,我们不散
6楼-- · 2020-06-08 19:35

Or you can use a simple sed:

sed -i 's/\"\${/\"/g;s/}\"/\"/g' main.tf
查看更多
登录 后发表回答