Tfenv to handle different versions of Terraform

jdxlabs

Jérôme Dx

Posted on January 15, 2024

Tfenv to handle different versions of Terraform

If you have different Terraform projects, despite the fact that you always want to have the latest, most up-to-date version, you may have to juggle between several versions.

That's why I wanted to introduce you to a little tool that could change your life : Tfenv.

Tfenv is a version manager for Terraform, allowing users to easily switch between different versions of the Terraform infrastructure code on their system.

You can set it up very easily :

brew install tfenv
Enter fullscreen mode Exit fullscreen mode

You can install the version you want

# Install the latest version
tfenv install latest

# or the version you want
tfenv install 1.6.6
Enter fullscreen mode Exit fullscreen mode

Then, you can use the version you want :

tfenv use 1.6.6

terraform version
# Terraform v1.6.6
# on darwin_amd64
Enter fullscreen mode Exit fullscreen mode

You can also list all the versions you have already installed :

tfenv list
Enter fullscreen mode Exit fullscreen mode

It may possibly be interesting to implement it in your bash scripts, if you want to automate it :

TF_VERSION="1.6.6"
if [[ "$OSTYPE" == "darwin"* ]] || [[ "$OSTYPE" == "linux-gnu" ]]; then
    tfenv install $TF_VERSION
    tfenv use $TF_VERSION
elif [[ $(terraform -v) != *"$TF_VERSION"* ]]; then
    echo "Terraform $TF_VERSION must be installed"
    exit 1
fi
Enter fullscreen mode Exit fullscreen mode

Tfenv is an efficient cross-platform tool, developed by Hashicorp, which aims to simplify the versioning of Terraform on a daily basis, in a simple and fast way.

Edit :
If some of your project use OpenTofu (or even Terragrunt and Atmos), You can also consider Tenv, which has the same goals, offers almost identical commands and will allow you to manage your other projects in parallel.

💖 💪 🙅 🚩
jdxlabs
Jérôme Dx

Posted on January 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related