Using TFENV to Manage Multi-Version Terraform

Ayu W
3 min readMar 20, 2020

For you who used Terraform to maintain your infrastructure (cloud based of course), you may experience some annoying changes. As we all know, HashiCorp is actively enhance Terraform until their latest major upgrade last year (shifting from v0.11 into v0.12).

While I’m writing this, I checked that the latest version is 0.12.24 and it could be pretty annoying when we run terraform init, plan, apply but always getting an error in syntax.

But, don’t you cry no more! In this article, I will guide you to the ultimate light.

Prerequisites

This tutorial assumes you use terminal in MacOS to code and maintain your terraform configuration. You also install terraform using brew at your OS.

Confirming Your Terraform Version

Before you do something, first make sure Terraform is installed and check its version.

Cynthias-MacBook-Pro:playbook ayucynthia$ terraform -versionTerraform v0.12.9Your version of Terraform is out of date! The latest versionis 0.12.23. You can update by downloading from www.terraform.io/downloads.html

Well, my terraform version is far from update. But, I still use several tfstate with that version. So, how do I manage another terraform configuration with different version?

Setting Up Terraform Environment

To manage several terraform versions, we have two options:

  1. Manage different terraform version using container. But, hello? Are you sure you want to go that far? It will be troublesome if you had to maintain another stack in your laptop. This approach will need another level of knowledge, such as maintaining your own docker images, associate shell script which contains terraform executable, and blah-blah-blah.
  2. So, I will skip the first and go forward to the second way. Maybe this is the best approach for now. The idea is to manage terraform environment using tfenv. It’s a terraform version manager inspired by rbenv (ruby environment).

TFENV does a few magic. We could install and uninstall specific terraform version. We could list available version from remote and local, then finally we could be a terraform-shifter. Neat, right?

Installing Terraform Env

You could visit this link to get a detailed wiki of terraform env. In this article, I will summarize the step-by-step of getting tfenv on your MacOS (64 bit).

  1. Install tfenv using brew install tfenv . Just leave the old installation as is.
  2. Install different terraform version using tfenv install <version> . For example: tfenv install 0.12.6 . Wait until the installation process is finished.
  3. Shift into your needed version using tfenv use <version> . For example: tfenv use 0.12.6 .
  4. To list all the available remote version (from terraform download page), use tfenv list-remote . The result will look like this:
$ tfenv list-remote0.12.00.12.0-rc10.12.0-beta20.12.0-beta10.12.00.11.14...

5. To list all the available local version (installed in your system), use tfenv list . The result will look like this:

Cynthias-MacBook-Pro:~ ayucynthia$ tfenv list* 0.12.6 (set by /usr/local/Cellar/tfenv/1.0.1/version)0.12.50.12.4

It’s awesome, right! With this little invention, we could maintain our terraform state without worries.

Happy Terraforming!

--

--