Upgrading your Terraform code from version 0.12 to 0.13 might be a very daunting task for developers and DevOps professionals who want to take advantage of the latest features and improvements in Terraform. In this comprehensive guide, we will walk you through the process of updating your Terraform code from 0.12 to 0.13, provide code examples, and highlight the key differences between Terraform version 0.12 and 0.13.

Also Read: Upgrade Terraform code from 0.11 to 0.12 

Preparing for the Upgrade

Before you begin the upgrade process, it's crucial to prepare your environment to ensure a smooth transition. Start by backing up your existing Terraform code and state files. Using a version control system like Git can help you track changes and revert back if necessary.

Step 1: Install Terraform 0.13

To upgrade to Terraform 0.13, you first need to install the new version. You can download it from the official Terraform website or use a package manager like Homebrew or Chocolatey if you are on macOS or Windows, respectively.

Step 2: Initialize the Upgrade

Navigate to your project directory in the command line and run the following command to initialize the upgrade process:

# terraform 0.13upgrade

This command will identify any necessary changes required for your configuration files to be compatible with Terraform 0.13.

Code Examples and Differences

Let’s explore some of the key differences between Terraform 0.12 and 0.13 through code examples:

Example 1: Required Providers Syntax

Terraform 0.12:

provider "aws" {
  version = "~> 2.7"
  region  = "us-west-2"
}

Terraform 0.13 introduces a new way to declare providers, making it clearer which providers a module requires:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 2.7"
    }
  }
}

provider "aws" {
  region = "us-west-2"
}

Example 2: Module for_each and count

Terraform 0.12 does not support the use of for_each and count arguments within module blocks.

Terraform 0.13 introduces the ability to use for_each and count within modules, allowing for more dynamic configurations:hcl

module "servers" {
  source = "./modules/server"
  for_each = {
    "a" = "value1"
    "b" = "value2"
  }
}

Step 3: Review Changes and Test

After running the terraform 0.13upgrade command, review the changes proposed by Terraform. It's important to check the output and commit these changes to your version control system.

Next, run terraform plan to see how these changes will affect your infrastructure. This step is crucial to ensure that the upgrade will not introduce any unwanted changes.

Step 4: Apply the Upgrade

Once you have reviewed and are satisfied with the plan output, apply the changes by running:

terraform apply

This command will update your infrastructure according to the new Terraform 0.13 configurations.

Conclusion

Upgrading to Terraform 0.13 from 0.12 can significantly enhance your infrastructure management capabilities with features like improved provider declarations and the ability to use for_each and count in modules.

By following this guide, you can ensure a smooth upgrade process, leveraging the latest advancements in Terraform to maintain efficient, reliable, and scalable infrastructure.

Remember to test thoroughly and use version control to manage your Terraform configurations effectively.

We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,

DigitalOcean - 100$ free credit & Linode - 100$ free credit

Check some Exclusive Deals, HERE.

Also, check out DevOps Book You should read section.