Switching Terraform versions using tfenv

I'm Teraoka, an infrastructure engineer.
This time, I'll show you how to switch Terraform versions using tfenv.

1. What is tfenv?

tfenv is a tool that allows you to switch the version of Terraform you are running from the CLI.
As some of you may have noticed from the similar names, it
was inspired by
rbenv It is currently in alpha state, but Terraform 0.12 will be released soon and the HCL format will change significantly, so
it may be useful to be able to switch versions at any time with tfenv now.

2. Install tfenv

Clone from github and set the path

$ git clone https://github.com/tfutils/tfenv.git ~/.tfenv $ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile $ source ~/.bash_profile

3.How to use tfenv

Command List

$ tfenv tfenv 0.6.0-16-g4475b71 Usage: tfenv<command> [<options> ] Commands: install Install a specific version of Terraform use Switch a version to use uninstall Uninstall a specific version of Terraform list List all installed versions list-remote List all installable versions

Installation (0.11)

$ tfenv install 0.11.11 [INFO] Installing Terraform v0.11.11 [INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip ##################################################################### 100.0% [INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_SHA256SUMS tfenv: tfenv-install: [WARN] No keybase install found, skipping GPG signature verification Archive: tfenv_download.s1KQnc/terraform_0.11.11_linux_amd64.zip inflating: /root/.tfenv/versions/0.11.11/terraform [INFO] Installation of terraform v0.11.11 successful [INFO] Switching to v0.11.11 [INFO] Switching completed

List of installed versions

$ tfenv list * 0.11.11 (set by /root/.tfenv/version)

Once you have done this and run Terraform,
you will be able to use the version installed with tfenv.

$ terraform --version Terraform v0.11.11

I'd like to try using 0.12 instead of 0.11.
Let's try switching versions.

Installation (0.12)

$ tfenv install 0.12.0-alpha4 [INFO] Installing Terraform v0.12.0-alpha4 [INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.12.0-alpha4/terraform_0.12.0-alpha4_terraform_0.12.0-alpha4_linux_amd64.zip ##################################################################### 100.0% [INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.12.0-alpha4/terraform_0.12.0-alpha4_SHA256SUMS tfenv: tfenv-install: [WARN] No keybase install found, skipping GPG signature verification Archive: tfenv_download.gut3iL/terraform_0.12.0-alpha4_terraform_0.12.0-alpha4_linux_amd64.zip extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform-provider-aws_v1.40.0-6-gb23683732-dev_x4 extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform-provider-azurerm_v1.17.0-5-ga3b48ba3-dev_x4 extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform-provider-google_v1.19.1-3-g59efc8b9-dev_x4 extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform-provider-oci_v3.9.0-3-ga5859820-dev_x4 extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform-provider-null_v1.0.0-5-gf54ff98-dev_x4 extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform-provider-template_v1.0.0-5-g317c9c9-dev_x4 extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform-provider-random_v2.0.0-5-g612dff2-dev_x4 extracting: /root/.tfenv/versions/0.12.0-alpha4/terraform-provider-local_v1.1.0-5-ga2df742-dev_x4 [INFO] Installation of terraform v0.12.0-alpha4 successful [INFO] Switching to v0.12.0-alpha4 [INFO] Switching completed

List of installed versions

$ tfenv list * 0.12.0-alpha4 (set by /root/.tfenv/version) 0.11.11

It looks like 0.12 is loaded, so let's try running Terraform

$ terraform --version Terraform v0.12.0-alpha4 (2c36829d3265661d8edbd5014de8090ea7e2a076)

It looks like it has been switched.
Of course, you can switch back to 0.11.

Version Switching

$ tfenv use 0.11.11 [INFO] Switching to v0.11.11 [INFO] Switching completed $ terraform --version Terraform v0.11.11

rbenv also has a similar function, but
you can change the version only in a specific directory.
Place a file called ".terraform-version" in the directory you want to change.

Switch version (specific directory)

$ tfenv list 0.12.0-alpha4 * 0.11.11 (set by /root/.tfenv/version) $ pwd /opt/tfenv-version-test $ echo 0.12.0-alpha4 > .terraform-version $ terraform version Terraform v0.12.0-alpha4 (2c36829d3265661d8edbd5014de8090ea7e2a076)

Looking at tfenv list, it says 0.11.11, but
the directory with .terraform-version has 0.12.0 loaded.
If you go back one directory and check the version, you will see that it is 0.11.11.

$ cd ../ $ terraform version Terraform v0.11.11

4. Summary

Using tfenv, you can easily switch versions from the CLI

  • Testing and implementing new versions
  • Using version-specific plugins

I think tfenv will be extremely useful in situations like this.
I'm really looking forward to the official release of Terraform 0.12.
I hope you'll give tfenv a try!

If you found this article useful, please click [Like]!
0
Loading...
0 votes, average: 0.00 / 10
18,653
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yuki Teraoka

He joined Beyond in 2016 and is currently in his sixth year
as an infrastructure engineer working for an MSP, where he troubleshoots issues while also designing
and building infrastructure using public clouds such as AWS. Recently, he has been

using Hashicorp tools such as Terraform and Packer as part of the construction and operation automation
of container infrastructure such as Docker and Kubernetes, and he also plays the role of evangelist, speaking at external study groups and seminars.

・GitHub
https://github.com/nezumisannn

・Speaking history
https://github.com/nezumisannn/my-profile

・Presentation materials (SpeakerDeck)
https://speakerdeck.com/nezumisannn

・Certification:
AWS Certified Solutions Architect - Associate
Google Cloud Professional Cloud Architect