How to Properly Setup Terraform with Tfenv

  • by
Terraform Tfenv

This guide will help you get Terraform up and running in a manner that avoids all the mistakes I made. If you’re unfamiliar with Terraform, it’s an Infrastructure as Code (IaC) tool designed to manage your cloud resources using in code. This allows you to properly version control changes, roll things back, copy + paste (or use modules) for similar resources, etc. You’re here though, which means you’re either interested in trying Terraform out or setting it up on a new computer. Either way, let’s dive into it!

Prerequisite: Before jumping into Terraform it’s important that you have the AWS CLI installed and properly configured with your secret and access key. If you need some help with that check out our guide on setting up the AWS CLI.

Terraform Install: A Cautionary tale

Terraform is just a binary file from Hashicorp. If you want to take the easy road and just download it you can visit their download page here: https://www.terraform.io/downloads.html. However let me throw a little caution to the wind.

When you download the binary you are downloading a specific version. Let’s say I went and downloaded it right now – I would get Terraform version 1.0.7. I write some code and apply it with no problems. 2 weeks later my teammate comes along and downloads Terraform, because there have been upgrades they might get Terraform version 1.0.8. They do exactly as I did, write some code, apply it, no issues. Everything is working, what’s the big deal?

The big deal is that new versions of Terraform are not backwards compatible on applies. Because my teammate applied Terraform using version 1.0.8 I will receive an error if I try to apply it using version 1.0.7. This forces me to go download the latest version of Terraform, maybe its 1.0.9 now, delete my old Terraform binary, move this one to the old location, and apply the code. And after all this work all I’ve done is caused my teammates code to break next time they run it.

Downloading the file and moving the binary each time you need to step up a version is frustrating. To get around this strenuous process I recommend using a tool called tfenv.

Tfenv – The Terraform Version Problem Solver

Tfenv is a tool whose sole purpose is to allow easy management of Terraform versions. For example if you want to install Terraform version 1.0.7 you just run tfenv install 1.0.7. If version 1.0.7 isn’t compatible with your Terraform code you can just run tfenv use 1.0.2 (or whatever version you need). When it’s time to switch to 1.0.7 you can switch versions super easy since it’s already downloaded.

Setting up Tfenv

Tfenv on Mac

If you’re on a Mac the easiest way to install it is to use brew.

  1. Run a brew install of the tfenv package.
$ brew install tfenv

2. Verify tfenv was properly installed by checking it’s version.

$ tfenv --version
tfenv 2.2.2-84-g459d15b

If you have some beef with Brew and want to go through the slightly more manual route feel free to jump down to the Linux section and run those commands – they will work for either Operating System.

Tfenv on Linux

The installation process of tfenv should be the same on all Linux Operating Systems. Just follow the steps below.

  1. Clone the tfenv repository into the directory ~/.tfenv
$ git clone https://github.com/tfutils/tfenv.git ~/.tfenv

2. Create a symbolic link from this new directory (~/.tfenv) to /usr/local/bin. This will enable your terminal to find the tfenv binary when you run tfenv.

sudo ln -s ~/.tfenv/bin/* /usr/local/bin

3. Verify tfenv is working properly by checking it’s version.

$ tfenv --version
tfenv 2.2.2-84-g459d15b

How to use Tfenv with Terraform

You will generally use Tfenv for 3 different tasks – listing Terraform versions, installing certain Terraform versions, and using each of the versions you installed. Below we’ll go over how to do each of these.

  1. Listing Terraform versions

To list all Terraform versions available to you run tf-env list-remote. This will show you a complete list of all Terraform versions available for installation through tfenv.

$ tfenv list-remote
1.1.0-alpha20210922
1.1.0-alpha20210908
1.1.0-alpha20210811
1.1.0-alpha20210728
1.1.0-alpha20210714
1.1.0-alpha20210630
1.1.0-alpha20210616
1.0.7
1.0.6
1.0.5

You can also just list the versions that are already installed on your local computer by running tfenv list.

$ tfenv list
* 1.0.7 (set by /home/user/.tfenv/version)
  1.0.5

2. Installing Terraform Versions with Tfenv

Once you’ve listed the versions available for installation you can install them by running tfenv install <version>. For example

$ tfenv install 1.0.6
Installing Terraform v1.0.6
Downloading release tarball from https://releases.hashicorp.com/terraform/1.0.6/terraform_1.0.6_linux_amd64.zip
######################################################################### 100.0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.0.6/terraform_1.0.6_SHA256SUMS
No keybase install found, skipping OpenPGP signature verification
Archive:  /tmp/tfenv_download.OVg1Hf/terraform_1.0.6_linux_amd64.zip
  inflating: /home/user/.tfenv/versions/1.0.6/terraform  
Installation of terraform v1.0.6 successful. To make this your default version, run 'tfenv use 1.0.6'

3. Using Terraform Versions with Tfenv

To switch between versions run tfenv use <version>. Below you can see me switch back and forth between versions.

$ tfenv use 1.0.6
Switching default version to v1.0.6
Switching completed
$ tfenv use 1.0.7
Switching default version to v1.0.7
Switching completed

Conclusion

If you use Terraform I would highly recommend installing it using Tfenv. It’s a fantastic tool that will save you a lot of pain.

Leave a Reply

Your email address will not be published. Required fields are marked *