Upgrade Terraform (Linux)

Posted: 15 September, 2021 Category: recipes Tagged: terraform

Steps

  • Understand your ideal upgrade path by looking at the upgrade guides here: https://www.terraform.io/upgrade-guides/index.html
  • do which terraform to find the the path to terraform (likely /usr/bin/terraform or /usr/local/bin/terraform ). Whatever it is, jot down this installPath. You will need it in a bit.
  • Goto https://releases.hashicorp.com/terraform/ to see available versions. Choose the version you want based on the versions listed. You can click through on the hyperlinks to get the full base url for the download of interest.

    • emoji-bulb tip: to know what architecture to target, type uname -a in a terminal. For example I determined I should get the amd64 build, so I downloaded the amd64.zip files.
  • Download the correct file for the target terraform version, e.g.:

    curl -O https://releases.hashicorp.com/terraform/1.0.7/terraform_1.0.7_linux_amd64.zip
  • Backup the old version using the installPath you determined earlier, e.g. assuming it's /usr/local/bin/terraform:

    # replace `.old` with a more useful suffix, like the old version number that's being archived.
    sudo mv /usr/local/bin/terraform /usr/local/bin/terraform.old
  • unzip the new download to the original installPath, noted earlier. e.g.:

    sudo unzip terraform_1.0.7_linux_amd64.zip -d /usr/local/bin/
  • Check that you can invoke the new version!

    terraform version
  • run terraform init again to reinitialise. If you run into problems, don't forget to make edits to your tf files according to the upgrade guides and your chosen upgrade path (eg, some syntax has likely changed). To force upgrade the state / automate migration of state:

    terraform init -migrate-state

In my case, -migrate-state with my s3+dynamodb backend didn't work! kept complaining about locks. tried with init -reconfigure and still no joy. Nuked the dynamodb table entries. There was still an issue so I went into the S3 bucket and just nuked everything, came back and nuked the .terraform folder as well, then init was happy.

Imagine if this had been a real prod environment! emoji-see_no_evil


Ran into needing these steps because:

  • I was using a dockerized instance suggested as part of a course, but it was an older version and it was forcing me to use older syntax which I didn't want to stick to
  • It was BEYOND slow to keep firing up a docker run with --rm <tf-command-here> each time I wanted to use that version of tf, and I didn't want to get sidetracked with tfenv / tfswitch...
  • my own version of tf was also old: huge gap between when I first installed terraform to play with it, vs when I got around to using it in anger.