2 min read

How Do I Upgrade Terraform Providers?

Terraform Providers have regular releases and keeping their versions up-to-date in Configurations files is important.
💡
Tools used:
- Docker
- Terraform
- An AWS EC2 Ubuntu Instance

Install Terraform

The official instructions for installing Terraform are here. I used Ubuntu-based EC2 for this "How do I", instructions for which are here.

Install Docker

The official instructions for installing Docker are here.

Step 1: Make a simple Configuration File.

terraform {
  required_version = ">= 1.0.0"
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "5.23.0"
    }
  } 
}

Save the file as main.tf

Step 2: Execute terraform init.

terraform init

The initialization will commence, and show the following output on the screen

Notice version 5.23.0 of the Google Provider is being installed.

As part of initialization, a .terraform.lock.hcl file is also generated. This file stores a hash for each distinct Provider-Version pairing.

Contents of .terraform.lock.hcl file

Step 3: Change the Provider version.

In main.tf, make a change (as shown below):

terraform {
  required_version = ">= 1.0.0"
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "5.27.0"
    }
  }
}

Change version from 5.23.0 to 5.27.0.

Step 4: Execute terraform init again.

A different Provider version warrants a new initialization of the terraform environment. However, this time, you should get an error instead of the happy green success message.

The .terraform.lock.hcl file has a hash for 5.23.0 but the Configuration file wants 5.27.0

The .hcl file has the hash for 5.23.0 and is therefore confused when it finds 5.27.0 in the Configuration file.

Step 5: Execute terraform init with -upgrade flag.

terraform init -upgrade
Notice version 5.27.0 is being downloaded.

I write to remember, and if, in the process, I can help someone learn about Containers, Orchestration (Docker Compose, Kubernetes), GitOps, DevSecOps, VR/AR, Architecture, and Data Management, that is just icing on the cake.