Manage multiple Terraform versions with tfenv

3 min read

As terraform evolves has been major changes that forces you to update your terraform code to use it with the latest version. If you have a large codebase it can be very challenging to keep up with the versions since it can be very time consuming. So, instead of this you can specify on your side the required terraform version like so:

terraform {
  required_version = "=0.11.14"
}

If you try to plan/apply this code you would get an error message like this:

Error: Unsupported Terraform Core version

  on main.tf line 3, in terraform:
   3:   required_version = "0.11.14"

This configuration does not support Terraform version 0.13.5. To proceed,
either choose another supported Terraform version or update this version
constraint. Version constraints are normally set for good reason, so updating
the constraint may lead to other errors or unexpected behavior.

To make it easier to switch between terraform versions we can use tfenv

tfenv is on github. It is a set of bash scripts that are going to do it's magic for us. To install it we just need to follow the instructions thet we can find in the README file

Once we have it installed we can use tfenv list to show all the different terraform version we have currently installed:

$ tfenv list
* 0.13.5 (set by /home/jprats/.tfenv/version)
  0.12.29
  0.12.26
  0.11.14
  0.11.9
  0.11.2
  0.10.8

On the other hand, using tfenv list-remote we will be able to list all the terraform versions that are available:

$ tfenv list-remote
0.15.0-alpha20210107
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.14.0-rc1
0.14.0-beta2
0.14.0-beta1
0.14.0-alpha20201007
0.14.0-alpha20200923
0.14.0-alpha20200910
0.13.6
0.13.5
0.13.4
0.13.3
0.13.2
0.13.1
0.13.0
0.13.0-rc1
0.13.0-beta3
0.13.0-beta2
0.13.0-beta1
0.12.30
0.12.29
0.12.28
(...)

For example, let's assume we are currently using 0.13.5

$ terraform --version
Terraform v0.13.5

Your version of Terraform is out of date! The latest version
is 0.14.3. You can update by downloading from https://www.terraform.io/downloads.html

Switching to 0.11.14 it is as easy as:

$ tfenv use 0.11.14
Switching default version to v0.11.14
Switching completed

Repeating terraform --version we will be able to see that we have switched to another terraform version:

$ terraform --version
Terraform v0.11.14

Your version of Terraform is out of date! The latest version
is 0.14.4. You can update by downloading from www.terraform.io/downloads.html

If you don't have the version we are requesting available is going to complain:

$ tfenv use 0.14.4
No installed versions of terraform matched '0.14.4'

But installing a new versions is just as easy as switching version. You just need to use tfenv install as follows:

$ tfenv install 0.14.4
Installing Terraform v0.14.4
Downloading release tarball from https://releases.hashicorp.com/terraform/0.14.4/terraform_0.14.4_linux_amd64.zip
############################################################################################################################################################################################################ 100,0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.14.4/terraform_0.14.4_SHA256SUMS
No keybase install found, skipping OpenPGP signature verification
Archive:  tfenv_download.xlKP0f/terraform_0.14.4_linux_amd64.zip
  inflating: /home/jprats/.tfenv/versions/0.14.4/terraform  
Installation of terraform v0.14.4 successful. To make this your default version, run 'tfenv use 0.14.4'

Then, if we repeat the tfenv use, this time, is going to succeed

$ tfenv use 0.14.4
Switching default version to v0.14.4
Switching completed

Posted on 01/02/2021

Categories