Upgrade terraform to specific version
HomebrewTerraformHomebrew Problem Overview
Use case
I have installed Terraform v0.11.13 via homebrew and as recommended by terraform I want to ugprade to version v0.11.14 before doing the major upgrade to v0.12.0.
The problem
When I run brew upgrade terraform
or download the Mac package from the terraform website it would immediately update my terraform version to v0.12.0 I think.
So how can I upgrade to v0.11.14 instead?
Homebrew Solutions
Solution 1 - Homebrew
Especially when playing around with Terraform 0.12 betas, I learned to love tfenv.
After installation (via brew install tfenv
on MacOS), this allows you to easily discover, install and activate any Terraform version:
$ tfenv list-remote
0.12.0
0.12.0-rc1
0.12.0-beta2
0.12.0-beta1
0.12.0
0.11.14
...
$ tfenv install 0.11.14
[INFO] Installing Terraform v0.11.14
[INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_darwin_amd64.zip
...
[INFO] Installation of terraform v0.11.14 successful
[INFO] Switching to v0.11.14
[INFO] Switching completed
If you want to switch to a different version:
$ tfenv use 0.12.0
[INFO] Switching to v0.12.0
[INFO] Switching completed
Solution 2 - Homebrew
For anyone looking to do the same without using homebrew:
- Get source
$ wget https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_linux_amd64.zip
$ unzip terraform_0.11.14_linux_amd64.zip
- Give binary executable permissions and install (will overwrite current version)
$ chmod +x terraform
$ sudo mv terraform /usr/local/bin/
- Confirm version
$ terraform --version
Source: https://titosoft.github.io/kvm/terraform-and-kvm/#installing-terraform
Solution 3 - Homebrew
There is a nice alternative to tfenv that I've been using for few years now - tfswitch.
- Simple installation process with:
# MacOS
brew install warrensbox/tap/tfswitch
# Linux
curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh | bash
- Supplies alpha, beta and release candidates terraform versions.
- Supports multiple options for changing versions like:
- Manual with
tfswitch VERSION_NUMBER
. - Automatically detects and switch to version described in
version.tf
file. - Automatically switch to custom binaries under custom path (helpful for users or services with limited privileges).
- Automatically switch with
.tfswitchrc
,.terrafom-version
- requires minimum setup and supports bash, zsh and fish.
- Can be easily integrated with Jenkins and CircleCI - I haven't tested it out, these features were added 10 and 5 months ago respectively.
All in all it's a great and simple helper, would recommend this one.
Solution 4 - Homebrew
Apart from the conventional solutions, an easy implementation to the required problem would be installing tfswitch. It can change terraform versions with only one command. Installation and usage guide is provided in the given link.
Note:
You can not downgrade to previous versions after the script has been initialised (terraform init
).
Solution 5 - Homebrew
Easy and best way to do:-
# MacOS
brew install warrensbox/tap/tfswitch
# Linux
curl -L https://raw.githubusercontent.com/warrensbox/terraform-
switcher/release/install.sh | bash
then use Either one of command to change version-
1. tfswitch ==> you will see option to select version.
2. tfswitch 0.14.3 ==> this way
Solution 6 - Homebrew
I have used TF since 0.6 and actively use many different versions on my workstation. I do this with direnv [https://direnv.net/] since that was available for quite a while, and it works well for managing the TF binary version within a dir, plus it allows me to pass env vars per dir into TF with little hassle.
Sample .envrc:
export AWS_PROFILE=prod
export PATH=/usr/local/terraform/terraform-0.12.20:$PATH
export TF_VAR_dd_api_key=REDACTED
export TF_VAR_dd_app_key=REDACTED
When I cd into the dir containing this .envrc, not only does it put the correct TF binary in my PATH, it also sets my AWS profile and in this case some DataDog API keys.
When I want to upgrade TF, I ensure I have the desired binary placed into the correct location and edit the .envrc so it is in PATH. Ideally, the .envrc is not pushed to Github since other people could have different setups, and especially API keys should not go into the repo.
I know it's a little old fashioned but it works great for me.
Solution 7 - Homebrew
Go for tfswitch, it’s easy and efficient.