top of page

Posts

How to Manage Terraform Version Using tfenv



What is tfenv?

The tfenv is a tool that’s for managing Terraform versions with CLI.

You may have noticed the similarity in the name.

The tfenv is made from the inspiration of rbenv.


tfenv installation

Clone from github


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

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


Install (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 executing this far, you can use the version you installed with tfenv.


$ terraform --version
Terraform v0.11.11

I’m going to switch the version.


Install (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 0.12 is loaded, so I'm going to run Terraform.


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

It seems the version is switched.

And of course you can switch back to version 0.11.


Switching versions


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

The rbenv has the same feature, but you can switch versions for the specified directory.

Put a file named “.terraform-version” under the directory that you are willing to change.


Switching versions (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)


If you check with tfenv list, the version is 0.11.11, but the directory that has .terraform-version has 0.12.0.

If you go to the current directory, and see the version, it’s 0.11.11.


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

Summary

With tfenv, switching versions can be easily done from the CLI.


The tfenv might be useful for


  • Testing operation and implementation for a new version

  • Use of plug-ins that are dependent on a specific version


Thank you!


This blog post is translated from a blog post written by Tomoki Takeuchi on our Japanese website Beyond Co..


Comments


bottom of page