Andi Ashari

Tech Voyager & Digital Visionary

Managing Multiple Terraform Versions with Tfenv

Managing Multiple Terraform Versions with Tfenv

Have you ever found yourself juggling between different projects, each requiring its unique Terraform version? It's like trying to play a piano and a guitar simultaneously - a bit of a stretch, right? Enter tfenv, a Terraform version manager that's about to make your life a whole lot easier. In this semi-casual stroll through tech-ville, we'll explore how to install tfenv, why it's your new best friend, and how to use it to manage multiple Terraform versions like a pro. asd

Installing tfenv

Getting tfenv up and running is as straightforward as pie. You'll start by cloning the tfenv repository into your home directory. Open your terminal and run:

git clone --depth=1 ~/.tfenv 

This command fetches the latest tfenv goodness without dragging along the entire history of changes, keeping it light and fast.

Next up, let's make sure tfenv is always ready to jump into action. You have two options here:

Option 01: The PATH Way: Add tfenv to your shell profile to ensure it's recognized as a command. Append the following line to your .bash_profile.zshrc, or equivalent:

export PATH="$HOME/.tfenv/bin:$PATH" 

Option 02: The Symlink Method: If you prefer a more global approach, create symbolic links for tfenv binaries in /usr/local/bin:

ln -s ~/.tfenv/bin/* /usr/local/bin 

Either method will do the trick, so pick the one that sings to your soul. Full step-by-step on installing Tfenv can be found on the this official documentation

Setting the Stage with .terraform-version

Now that tfenv is installed, let's talk about managing those Terraform versions. tfenv uses a nifty little file named .terraform-version to control the version used in a project. Here's how to set it up:

  1. Create a directory for your project if you haven't already.
  2. Inside this directory, create a file named .terraform-version.
  3. Write the desired Terraform version inside this file, such as 1.7.2 for Project B or 1.0.1 for Project A.

Here's a quick example to create a directory for Project A and set it to use Terraform 1.0.1:

mkdir project-a 
echo "1.0.1" > project-a/.terraform-version 

And repeat for Project B with Terraform 1.7.2. This way, each project knows exactly which version of Terraform to use, no confusion, no mix-ups.

Testing It Out

To ensure everything is working as expected, navigate to each project directory in your terminal and run:

terraform --version 

This command should reflect the version specified in your .terraform-version file. If it does, you've successfully set up tfenv and are ready to manage multiple Terraform versions with ease.

Congratulations! You've mastered the art of managing multiple Terraform versions with tfenv. No more manual switching or version conflicts. Just smooth sailing through your infrastructure management tasks. With tfenv, you're now equipped to handle any project, regardless of its Terraform version, with confidence and efficiency. Happy coding!