45

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?

10 Answers 10

34
  Warning: Interpolation-only expressions are deprecated

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

I also got the above warning, which is due to the changed syntax for declaring variables in terraform. See the example below -:

Old syntax- region = "${var.region}" # you will get Interpolation-only warning

New Synatx- region = var.region # no warning

Check the syntax and correct it using any code editor.

1
  • 2
    At current time this warning will be automatically fixed after terraform fmt
    – jtprogru
    Jan 9 at 19:07
24

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.

2
  • 3
    Yes, I upgraded the code. I was still getting the errors above. Dec 3, 2019 at 23:30
  • jus to clarify, upgrading the modules means running this same 2 commands in the modules root like .terraform/modules/<module_name>
    – Kim Sant
    Feb 26, 2020 at 15:29
13

Update interploations as below:

subscription_id = "${var.subscription_id}"

to

subscription_id = var.subscription_id
11

Terraform v14 fmt incorporated this. You can now get the rc binary and simply run terraform fmt.

6

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:
2
  • 1
    This tool was helpful, but note that it doesn't uninterpolate values within arrays (e.g. subnet_ids). For that, I had to fall back to carefully applying a regex substitution: "\$\{([^}"]+)\}" It can't be applied blindly due to interpolation in here-documents. Jan 16, 2020 at 18:14
  • For fish shell, docker run --rm -v $(pwd):/code -t pniemiec/terraform-clean-syntax-docker. Nov 26, 2020 at 1:59
5

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 .
1
  • getting: panic: didn't find any token of type TokenOBrack
    – Spanky
    Dec 4, 2019 at 18:38
2

Or you can use a simple sed:

sed -i 's/\"\${/\"/g;s/}\"/\"/g' main.tf
2
  • Worked really well. I used *.tf instead of main.tf, and it updated all the files Aug 4, 2020 at 6:14
  • 1
    Actually, you're replacing strings with ". Instead, you want it replaced with nothing: sed -i 's/\"\${//g;s/}\"//g' *tf. Your result turns private_key = "${file("ssh_key")}" into private_key = "file("ssh_key")", which is invalid. You want the result to be private_key = file("ssh_key") Nov 18, 2020 at 15:46
2

The solution in already in the log output: ... remove the "${ sequence from the start and the }" ...

1

I used notepad++ to remove that interpolation syntax.

regex:

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

replace_with:

\1\2 
1
  • 3
    In VScode I used "\$\{([^$]*)\}" and replaced with $1
    – b3n
    May 14, 2021 at 11:17
1

You used => arn "${aws_sns_topic.default.arn}"

Try to use without interpolation, like: arn = aws_sns_topic.default.arn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.