Switching Terraform versions using tfenv
table of contents
My name is Teraoka and I am an infrastructure engineer.
This time, I will show you how to switch the Terraform version using tfenv.
1.What is tfenv?
tfenv is a tool that allows you to switch the running version of Terraform using the CLI.
Some of you may have noticed because the names are similar, but
it is inspired by
rbenv It is currently in alpha status, but Terraform 0.12 will be released in the near future and the HCL format will change significantly, so
it may be convenient to be able to switch versions at any time with tfenv now.
2.Installing tfenv
Clone from github and pass 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)
If you run Terraform after completing this step,
you will be able to use the version installed with tfenv.
$ terraform --version Terraform v0.11.11
I would like to try using 0.12 instead of 0.11.
I'll try switching the version.
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 seems that 0.12 is loaded, so I will try running Terraform.
$ terraform --version Terraform v0.12.0-alpha4 (2c36829d3265661d8edbd5014de8090ea7e2a076)
It looks like it's been switched.
Of course, it is also possible to revert to 0.11.
Switch version
$ tfenv use 0.11.11 [INFO] Switching to v0.11.11 [INFO] Switching completed $ terraform --version Terraform v0.11.11
In addition, rbenv has a similar function, but
you can also change the version only for a specific directory.
Place a file called ".terraform-version" in the directory you want to change.
Version switching (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)
When I look at tfenv list, it is 0.11.11, but
0.12.0 is loaded in the directory where .terraform-version is located.
If you go back one directory and look at the version, of course 0.11.11 will be loaded.
$ cd ../ $ terraform version Terraform v0.11.11
4.Summary
Using tfenv, you can easily switch versions from the CLI.
- Trial implementation and operation test of new version
- Using plugins that depend on specific versions
I think it will be very useful in that scene.
I'm really looking forward to the official release of Terraform 0.12.
Everyone, please try using tfenv!