Manage Multiple Terraform Versions With tfenv
When writing a Terraform config, you should specify the version of Terraform CLI you are using. This ensures that anyone using your terraform scripts is using the same version of the CLI tool. This is set in the terraform block, usually in providers.tf, like so:
terraform {
required_version = "1.1.5"
}
If you are writing many terraform configs, or your team is large, it’s really important to make sure everyone is using the same Terraform version. This is where tfenv comes in.
Installation and Usage
On MacOS:
brew install tfenv
You can then list out your terraform versions:
$ tfenv list
* 1.0.10
The version with the asterisk is the version of terraform that is currently activated. You can then install and use a new version like so:
tfenv install 1.1.5
tfenv use 1.1.5
The same version can be uninstalled simply with:
tfenv uninstall 1.1.5
You can install a version which matches certain criteria. From the README:
Install a specific version of Terraform. If no parameter is passed, the version to use is resolved automatically via .terraform-version files or TFENV_TERRAFORM_VERSION environment variable (TFENV_TERRAFORM_VERSION takes precedence), defaulting to ‘latest’ if none are found. If a parameter is passed, available options:
x.y.zSemver 2.0.0 string specifying the exact version to installlatestis a syntax to install latest versionlatest:<regex>is a syntax to install latest version matching regex (used by grep -e)min-requiredis a syntax to recursively scan your Terraform files to detect which version is minimally required. See required_version docs. Also see min-required section below.
$ tfenv install
$ tfenv install 0.7.0
$ tfenv install latest
$ tfenv install latest:^0.8
$ tfenv install latest-allowed
$ tfenv install min-required
Conclusion
It’s just that easy. You should use tfenv whenever you start a Terraform project of any size. Let me know if you have used tfenv before and if you like it. And remember, automate everything!