Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install multiple or two versions of Terraform?

I have a lot of Terraform modules written in Terraform 0.11 using gcp-provider of Terraform and want to upgrade the same to Terraform 0.12.

For this purpose, I need to keep both the versions installed on my system and use the version according to the version the module is written in.

I will go one by one in every module and upgrade the module using terraform 0.12upgrade to be compatible with Terraform 0.12 as per this documentation.

How to safely keep two versions of Terraform in one System?

like image 242
Amit Yadav Avatar asked Feb 07 '20 12:02

Amit Yadav


3 Answers

I'd highly recommend the tfenv tool. It sanely and easily can be used to manage multiple terraform installations. It's familiar if you've ever used nvm (for nodejs) or rvm (for ruby).

You can even add a .terraform-version file to your modules and the tool will automatically switch terraform versions for you when you cd into a module.

like image 156
mjgpy3 Avatar answered Sep 28 '22 04:09

mjgpy3


Make your life easy and install tfswitch. It takes care of installing and switching between versions you need; and it works like a magic.

like image 20
sridhar krishnamurthy Avatar answered Sep 28 '22 06:09

sridhar krishnamurthy


I use Ubuntu 18.04 and I achieved this safely following the below steps. Similar steps can be followed to do the same on any Linux distro (making sure you are downloading the compatible binary. Confirm here)

NOTE Running the following commands as root or sudo user

Create directories to keep the Terraform binaries

$ mkdir -p /usr/local/tf
$ mkdir -p /usr/local/tf/11
$ mkdir -p /usr/local/tf/12

Download the binaries for both the versions

  1. Download and extract the binary for Terraform 0.11 in a separate directory:
    $ cd /usr/local/tf/11
    $ wget https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_linux_amd64.zip
    $ unzip terraform_0.11.14_linux_amd64.zip
    $ rm terraform_0.11.14_linux_amd64.zip
    
  2. Download and extract the binary for Terraform 0.12 in a separate directory:
    $ cd /usr/local/tf/12
    $ wget https://releases.hashicorp.com/terraform/0.12.20/terraform_0.12.20_linux_amd64.zip
    $ unzip terraform_0.12.20_linux_amd64.zip
    $ rm terraform_0.12.20_linux_amd64.zip
    
  3. Create symlinks for both the Terraform versions in /usr/bin/ directory:
    ln -s /usr/local/tf/11/terraform /usr/bin/terraform11
    ln -s /usr/local/tf/12/terraform /usr/bin/terraform12
    
    # Make both the symlinks executable
    chmod ugo+x /usr/bin/terraform*
    

Calling different versions

  • Now, command terraform11 invokes version 0.11 and terraform12 invokes version 0.12
  • Example:
    $ terraform11
    $ terraform12
    

NOTE

  • Keeping the binaries in separate directories helps to separate their plugins as well without disturbing each other.
like image 42
Amit Yadav Avatar answered Sep 28 '22 06:09

Amit Yadav