Refreshing old Terraform state

Written at evening time in English • Tags: , ,

If you find yourself with an old Terraform state file (say from v0.11) and you need to run terraform plan to check things out, you’ll need to make some adjustments:

  • Add a terraform block with required_providers to provide the source for the provider.
  • Remove the version statement from the provider block. (You will want to move it to the provider in the required_providers block.)
  • Adjust any other syntax changes that result in errors (terraform plan):
    • list()tolist([])
    • type = "string"type = string
    • Possibly many others…
  • Replace the provider in the existing ancient state file.
  • Refresh the providers (terraform init).
  • Plan away!

The provider change looks something like this:

--- a/accounts/dev/main.tf
+++ b/accounts/dev/main.tf
@@ -3,10 +3,19 @@

+terraform {
+    required_providers {
+       aws = {
+           source = "hashicorp/aws"
+           version = "~> 2.23"
+       }
+    }
+    required_version = ">= 1.0"
+}
+
 provider "aws" {
     region = "us-east-1"
-    version = "~> 2.23"
 }

Once you run out of errors from terraform plan you’ll get an error about the old provider in the state file, so you’ll need to run these commands:

terraform state replace-provider -- -/aws registry.terraform.io/hashicorp/aws
terraform init
terraform plan