Switching Terraform versions using tfenv

table of contents
Hi, I'm Teraoka, an infrastructure engineer.
This time, I'll show you how to switch Terraform versions using tfenv.
1. What is tfenv?
tfenvis a tool that allows you to switch the version of Terraform you are running via the command line interface (CLI).
As you may have noticed from the similar name, it
rbenvwas inspired by
It is currently in alpha, but Terraform0.12will be released soon, and the HCL format will change significantly, so
it might be convenient to set up tfenv now so you can switch versions at any time.
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)
After completing these steps and running Terraform,
you can use the version installed with tfenv.
$ terraform --version Terraform v0.11.11
I'd like to try version 0.12 as well, not just 0.11.
Let's switch 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 seems to have switched.
Of course, it's possible to revert 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
Additionally, rbenv has a similar function, but it
also allows you to change the version for specific directories only.
Place a file named ".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)
`tfenv list` shows version 0.11.11, but
the directory containing `.terraform-version` is loading version 0.12.0.
If you go back one directory and check the version there, it will of course load 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 it will be very useful in situations like that.
I'm really looking forward to the official release of Terraform 0.12.
I encourage everyone to try using tfenv!
0
