Upgrading Multiple Terraform Configurations to Version 0.12

Jake Jones
FAUN — Developer Community 🐾
3 min readNov 3, 2020

--

My experience with Terraform upgrades from version 0.11 to 0.12 have been really frustrating, until now.

Photo by Tim Gouw on Unsplash

Upgrading Terraform from version 0.11 to 0.12 has been a pain in the butt every time I have done it. (and I have done it many times)

Every time that I go through the process I learn a little something more that makes it a little easier.

Before You Begin

Before you even get started there is something you need to fix with module naming. If you have any modules that start with a number for the name the module must be renamed before continuing with your upgrade.

Terraform 0.12 does not allow a module name to start with a number. For example:

module "1_name_here" {
someconfiguration
}

You would need to change it to

module "one_name_here" {
someconfiguration
}

Second, if you are still for some reason using atlas as the backend updating this will avoid some headache. Use the new remote backend.

terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "YOUR ORG"
workspaces {
name = "YOUR WORKSPACE"
}
}
}

Third, update anything that uses the count argument to use number instead.

Switch to Terraform 0.12

Next you will want to switch to Terraform version 0.12. If you are using OSX there is a really easy tool you can use to do this called TFSwitch. You can read more about the tool here.

Now you can run your terraform init.

Upgrading Your Code

It is finally time to upgrade your configuration files. For this, you can always do it manually if enjoy torture… I would suggest that you use this command provided by Hashicorp instead:

find . -name '*.tf' -printf "%h\n" | uniq | xargs -n1 terraform 0.12upgrade -yes

There is one change I would make though add in what is in bold below:

find . -path ./.terraform -prune -o -name '*.tf' -printf "%h\n" | uniq | xargs -n1 terraform 0.12upgrade -yes

The above command will exclude the upgrade from being run in the .terraform folder which will save you a lot of time. Thanks to my good friend Thomas Gallagher for helping out with this. (Check him out on LinkedIn)

Oh, and if you are using OSX you might need to run this command first to install find utils:

brew install findutils

Then use gfind instead of find:

gfind . -path ./.terraform -prune -o -name '*.tf' -printf "%h\n" | uniq | xargs -n1 terraform 0.12upgrade -yes

Now that you have run that command your syntax should be good to go as long as any parent modules that you are referencing are ready to be used in Terraform 0.12. Go ahead and run another terraform init then terraform apply and fix any errors you run into.

Conclusion

I have run into a lot of different time-sucking issues with upgrading to Terraform 0.12. Hopefully sharing the above saves you a lot of time and headache.

If you are interested in learning more about Terraform I have a Free Terraform Course for getting started and a course to help you study for your HashiCorp Certified: Terraform Associate.

I also highly suggest checking out Terraform Up & Running by Yevgeniy Brikman.

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--