Upgrading your Terraform code from version 0.13 to the latest version is a strategic move for developers and DevOps professionals aiming to harness the most advanced features and enhancements of Terraform. This guide provides a detailed walkthrough on how to update your Terraform code, complete with code examples and a comparison of the differences between Terraform version 0.13 and the latest version.

Preparing for the Upgrade

Before diving into the upgrade process, it's crucial to ensure that your environment is ready. Begin by backing up your existing Terraform code and state files. Utilizing a version control system like Git is highly recommended to track changes and facilitate easy reversion if needed.

Step 1: Install the Latest Version of Terraform

To upgrade, you first need to install the latest version of Terraform. You can refer to our detailed tutorial on how to install the Terraform on Windows, Linux & MacOS.

Step 2: Initialize the Upgrade

Navigate to your project directory in the command line and execute the following command to start the upgrade process:

terraform init -upgrade

This command prepares your Terraform configuration files for the latest version by upgrading modules, providers, and itself.

Code Examples and Differences

Let’s explore some of the significant differences between Terraform 0.13 and the latest version through code examples:

Example 1: Provider Dependency Lock File

Terraform 0.13 and earlier versions do not automatically generate a dependency lock file.

The latest versions of Terraform automatically generate a .terraform.lock.hcl file when you run terraform init. This file ensures that Terraform applies the same set of provider dependencies throughout all your environments, enhancing consistency and reliability in deployments:

# This file is generated by Terraform to record the provider versions used.
# It should be version controlled to ensure consistent builds.

provider "registry.terraform.io/hashicorp/aws" {
  version     = "3.27.0"
  constraints = "~> 3.27"
  hashes = [
    "h1:...",
    "zh:...",
  ]
}

Example 2: Sensitive Input Variables

Terraform 0.13 does not have built-in mechanisms to mark variables as sensitive.

The latest versions of Terraform allow you to declare variables as sensitive, preventing their values from being exposed in the CLI output:

variable "user_password" {
  type      = string
  sensitive = true
}

output "password" {
  value     = var.user_password
  sensitive = true
}

Step 3: Review Changes and Test

After initializing the upgrade, review the changes proposed by Terraform. Check the output and commit these changes to your version control system.

Next, execute terraform plan to preview how these changes will affect your infrastructure. This step is crucial to ensure that the upgrade will not introduce any unexpected 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 configurations for the latest Terraform version.

Conclusion

Upgrading to the latest version of Terraform from 0.13 can significantly enhance your infrastructure management capabilities with features like provider dependency locking and sensitive data handling. 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.