Upgrading to Terraform 1.5#

Pinning Terraform versions is a good practice to ensure that your infrastructure-as-code (IaC) is always deployed with a known version of Terraform. This is especially important when using Terraform Cloud, as the version of Terraform used to plan and apply changes is not always the same as the version used to develop the IaC. This can lead to unexpected errors and behavior, but this also requires that you keep your Terraform version up-to-date in the different configuration files.

Updating Terraform within devcontainer#

In post Run Terraform within GitHub Codespaces Terraform was installed in the devcontainer using features. To upgrade Terraform, simply update the version number in the devcontainer.json file as shown below.

Example devcontainer file .devcontainer/devcontainer.json with Terraform version set#
{
  "name": "Python 3",
  "image": "mcr.microsoft.com/devcontainers/python:0-3.9",
  "features": {
    "ghcr.io/devcontainers/features/github-cli:1": {},
    "ghcr.io/devcontainers/features/terraform:1": {
      "version": "1.5.2"
    },
    "ghcr.io/devcontainers-contrib/features/flake8:2": {},
    "ghcr.io/devcontainers-contrib/features/pipenv:2": {},
    "ghcr.io/devcontainers-contrib/features/yamllint:2": {}
  }
}

Rebuilding the devcontainer will install the new version of Terraform and it is now available for use within the devcontainer.

Upgrading Terraform within GitHub Actions#

In post Run Terraform with GitHub Actions the Terraform GitHub Action was used to run Terraform within GitHub Actions. To upgrade Terraform, simply update the version number in the .github/workflows/terraform.yml file as shown below.

Example workflow file .github/workflows/terraform.yml#
---
name: Terraform

on:
  push:
  pull_request:

env:
  TERRAFORM_VERSION: '1.5.2'

jobs:
  terraform:
    name: Terraform
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set up Terraform
        uses: hashicorp/setup-terraform@v2
        with:
          terraform_version: ${{ env.TERRAFORM_VERSION }}
          cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

Warning

The version set in the .github/workflows/terraform.yml file must match the version set in the terraform/main.tf file and within Terraform Cloud. If the versions do not match, the GitHub Action will fail.

Updating Terraform within Terraform Cloud#

The last step was described in post Require a specific Terraform version and matching this version with the version used in the devcontainer and GitHub Actions is important to ensure that the same version of Terraform is used throughout the entire development lifecycle. To upgrade Terraform, simply update the version number in the terraform/main.tf file as shown below.

Example of terraform/main.tf with a specific version of Terraform#
terraform {
  required_version = "~>1.5.2"
}

When the Terraform Cloud workspace is not updated to use the new version of Terraform, the following error will be displayed when running a plan.

Terraform Cloud error when using an unsupported version of Terraform#
Initializing Terraform Cloud...

│ Error: Unsupported Terraform Core version

│   on providers.tf line 2, in terraform:
│    2:   required_version = "~>1.5.2"

│ This configuration does not support Terraform version 1.4.6. To proceed,
│ either choose another supported Terraform version or update this version
│ constraint. Version constraints are normally set for good reason, so
│ updating the constraint may lead to other errors or unexpected behavior.

After the version is updated, the Terraform Cloud workspace will need to be updated to use the new version of Terraform. This can be done by clicking on the workspace name, then clicking on the General Settings tab, and then clicking on the Queue Plan button.