Sed spell to ease moving to Terraform 0.13

December 28, 2020 – Samuli Seppänen

When moving from older versions of Terraform 0.12.x to latest 0.12.x (now: 0.12.29) you may notice the following warnings:

Warning: Interpolation-only expressions are deprecated                                                                                                                              

  on line 11, in resource "hcloud_server_network" "backup_example_org":                                                                                                 
  11:   server_id = "${}"        

These make sense, but may come as a surprise as earlier 0.12.x versions were perfectly fine with them.

You may have tons of these, so fixing them manually gets tiresome. The fix is not too difficult, but is quite unreadable due to escaping:

ls *.tf|xargs sed -i s/"\"\${\(.*\)}\""/"\1"/g

This basically uses regexp capture groups to get rid of "${}" and leave what's inside the curly braces intact. So, the output looks like this (courtesy of "git diff"):

 resource "hcloud_server_network" "backup_example_org" {
-  server_id = "${}"
-  network_id = "${}"
-  ip = "${var.backup_example_org_private_ip}"
+  server_id =
+  network_id =
+  ip = var.backup_example_org_private_ip

After this there's a good change you're back to

$ terraform validate
Success! The configuration is valid.

Want to talk to an expert?

If you want to reach us, just send us a message or book a free call!