DEV Community

Cover image for Tfenv to handle different versions of Terraform
jdxlabs
jdxlabs

Posted on • Updated on

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

I have been using this tool regularly for several years. It really simplifies Terraform versioning on a daily basis, as a simple and effective tool, so I recommend it.

There are other alternatives appreciated by the community you can also consider, like Tfswitch or Asdf which allows you to manage several languages.

Top comments (0)