Problem
When upgrading to a newer major version of Terraform, a terraform plan or terraform apply command may fail with the following error:
Error: Unable to Read Previously Saved State for Upgrade ResourceState There was an error reading the saved resource state using the prior resource schema defined for version 0 upgrade. Please report this to the provider developer: flatmap states cannot be unmarshaled, only states written by Terraform 0.12 and higher can be unmarshaled
Cause
This error typically occurs when both the Terraform version and a provider version are upgraded in the same step. A major Terraform version upgrade (e.g., from 0.11 to 0.12) often changes the state file format. Concurrently, a provider upgrade can also introduce its own changes to how it stores information in the state file. Attempting both upgrades simultaneously can create a state format conflict that the new Terraform version cannot resolve, leading to the flatmap states cannot be unmarshaled error.
Solution
The recommended solution is to perform the upgrades in two separate stages: first, upgrade the Terraform version while keeping the provider version locked, and second, upgrade the provider version.
Procedure
-
Lock the provider version: In your configuration, add a
providerblock that explicitly locks the provider to the version that was used with your previous Terraform version. In this example, therandomprovider version2.3.1was used with Terraform 0.11.terraform { required_providers { random = { source = "hashicorp/random" version = "2.3.1" } } } resource "random_password" "password" { length = 16 special = true override_special = "!#$%&*()-_=+[]{}<>:?" } output "password" { value = "${random_password.password.id}" } -
Remove the local provider cache: Delete the
.terraformdirectory to ensure a clean initialization.$ rm -rf .terraform
-
Initialize with the new Terraform version: Run
terraform initusing your upgraded Terraform binary. This will download the locked provider version (2.3.1in this case).$ terraform init
-
Apply the configuration: Run
terraform applyto allow the new Terraform version to update the state file correctly with the old provider schema.$ terraform apply Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
-
Update the provider version constraint: Modify the
providerblock to allow for a newer version.terraform { required_providers { random = { source = "hashicorp/random" version = "~> 3.4" } } } -
Upgrade the provider: Run
terraform init -upgradeto download the latest provider version that matches your new constraint.$ terraform init -upgrade
-
Apply the final configuration: Run
terraform applyagain. The state is now fully upgraded to both the new Terraform version and the new provider version.$ terraform apply Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Additional Information
- For detailed instructions on upgrading Terraform, refer to the official Terraform upgrade guides.