Upgrading Terraform from version 0.11.x to 0.12.12 with both a local State File or Remote State

Linux Oct 23, 2019

As at the time of this post, the latest version of Terraform is the 0.12.12. The upgrade can be easily achieved with the following steps

Download the latest release of Terraform the link https://releases.hashicorp.com/terraform/

It can be installed from the linux command line by simply writing the command below

wget https://releases.hashicorp.com/terraform/0.12.12/terraform_0.12.12_linux_amd64.zip -O /tmp/terraform_0.12.12_linux_amd64.zip; sudo unzip /tmp/terraform_0.12.12_linux_amd64.zip -d /usr/local/bin/
terraform version
Terraform v0.12.12

If your remote state lies in a backend simply intialize terraform once again with your backend

terraform init -backend-config=credentials.auto.tfvars

while locally can be simply typing

terraform init

Terraform added a tool which simply upgrades all your terraform tf or .auto.tfvars file. This can be then initialized and rewrites your state files by simply typing the command below

terraform 0.12upgrade
This command will rewrite the configuration files in the given directory so
that they use the new syntax features from Terraform v0.12, and will identify
any constructs that may need to be adjusted for correct operation with
Terraform v0.12.

We recommend using this command in a clean version control work tree, so that
you can easily see the proposed changes as a diff against the latest commit.
If you have uncommited changes already present, we recommend aborting this
command and dealing with them before running this command again.

Would you like to upgrade the module in the current directory?
  Only 'yes' will be accepted to confirm.

  Enter a value: yes

-----------------------------------------------------------------------------

Upgrade complete!

The configuration files were upgraded successfully. Use your version control
system to review the proposed changes, make any necessary adjustments, and
then commit.

There you are good to go and continue working with the latest Terraform version at the time of this post.

OBSERVATION

Terraform not only rewrites all your files but as well creates a new file called versions.tf. The content shows your current terraform version.

[xxxx@ terraform]$ cat versions.tf
terraform {
  required_version = ">= 0.12"
}

Also observed in the 0.12 upgrade. Terraform inserts a TODO if its not sure of how to rewrite a part of a template. In my case was

# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibility in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
  records = [var.local_dns_ip[count.index]]

in my values, i had a list in list. so terraform wasn't sure how to interprete this. here is an example shown below

local_dns_ip = [["192.168.200.199"], ["192.168.200.189"], ["192.168.200.169", "92.168.200.179"]

the variable representation was

variable "local_dns_ip" {
  description = "just an example"
  type        = "list"
}

terraform 0.12upgrade rewrote my vars as shown below with the above error message

variable "local_dns_ip" {
  description = "just an example"
  type        = list(string)
}

The solution was to simply to edit the vars and the records line as shown below

#version 0.11.14
records = [var.local_dns_ip[count.index]]

#version 0.12.12
records = var.local_dns_ip[count.index]

#version 0.11.14
variable "local_dns_ip" {
  description = "just an example"
  type        = list(string)
}  

#version 0.12.12
variable "local_dns_ip" {
  description = "just an example"
  type        = list
}
Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
#