Problem Executing Terraform apply/plan

I’m getting an error.

I tried to upgrade terraform version to 0.13.5 but I start getting error.
Error: cannot decode dynamic from flatmap.

I believe terraform apply command wasn’t executed with 0.12.29 version which caused the above error. However, I removed versions from 0.13.5 and I tried to execute terraform plan with 0.12.29 version but started getting another error.

state snapshot was created by Terraform v0.13.5, which is newer than current v0.12.29; upgrade to Terraform v0.13.5 or greater to work with this state

I’m not sure how to recover from this situation. Even, I tried to revert the remote state file to the previous but I still get the same error.
state snapshot was created by Terraform v0.13.5, which is newer than current v0.12.29; upgrade to Terraform v0.13.5 or greater to work with this state

Any help would be appreciated!

Most probably you have some .terraform* files and folders around. Did you run terraform init after changing to previous terraform version?

Yes, I did terraform init, and it executed successfully with this warning:
Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to PagerDuty/pagerduty. Please update your source in required_providers

Afterwards, I ran terraform plan, and I again got this error:
Error: state snapshot was created by Terraform v0.13.5, which is newer than current v0.12.29; upgrade to Terraform v0.13.5 or greater to work with this state

I understood that you have a remote backend and reverted the remote backend to the previous state. However you didn’t clean up your local state-files.

Attached my terraform state file which has a version of 0.11.13

I removed .terraform folder from the local stack and executed terraform init.
I got this error:
**Error refreshing state: state snapshot was created by Terraform v0.13.5, which is newer than current v0.12.29; upgrade to Terraform v0.13.5 or greater to work with this state
**

But as you can see, my state file which is attached has a version of 0.11.13

terraform.txt (92.2 KB)

Did you check for local copies of the state? Cleanup .terraform might not be sufficient or try to start of from a clean folder.

I deleted the folder and created a new one, where only terraform files were added. When I tried to run terraform init, I got this error:

Error refreshing state: state snapshot was created by Terraform v0.13.5, which is newer than current v0.12.29; upgrade to Terraform v0.13.5 or greater to work with this state

Presumably you are using a remote state method? Which one?

Wherever you are storing state it has been upgraded to the latest format which requires 0.13.5 as mentioned. You either need to revert your remote state (if possible, and mindful of any data you might lose) or upgrade your Terraform version.

I would suggest just using the 0.13.5 version.

Yep, my initial question was it. When I use terraform 0.13.5 version, I get this error:
Error: cannot decode dynamic from flatmap

The solution in forums was that I need to run terraform apply with version 0.12.29 which I did and got this error:
Error refreshing state: state snapshot was created by Terraform v0.13.5, which is newer than current v0.12.29; upgrade to Terraform v0.13.5 or greater to work with this state

Also, I used a previous state file which wasn’t executed with 0.13.5 version. Attached is the remote state file which is used to execute with 0.12.29 version. BTW, seems the file was last executed with 0.11.13 version.

Thanks for your responses.terraform.txt (92.2 KB)

Was that file obtained by running terraform state pull?

How did you revert to the previous state?

terraform state pull returns the below error:
Failed to refresh state: state snapshot was created by Terraform v0.13.5, which is newer than current v0.12.29; upgrade to Terraform v0.13.5 or greater to work with this state

I have AWS S3 versioning, and I override the current version with one of the previous versions.

If you are getting that error it doesn’t look like the revert has worked. How did you do that? Delete the latest version in S3 or create a new version containing older data?

Well, I tested it another way to make sure terraform loads the right file. I created another folder(prefix) in the S3 bucket and added previously attached file to that folder and referred to that file as a remote state so that AWS somehow won’t return another version of the file. But I still get the same error.

One thing which might help in troubleshooting, when I run terraform init, I get the below warning:
Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to PagerDuty/pagerduty. Please update your source in required_providers.

IMHO, this can be omitted, since you are running terraform 0.12.29, right now.
Could you share the console output after running terraform init and also list all files in your folder (e.g. ls -Lisa)?

Here is the logs output of terraform plan

Initializing modules...
- aws_account in ../../modules/aws_account
- ecs_service in ../../modules/ecs_service
- ecs_service.aws_account in ../../modules/aws_account
- ecs_service.tags in ../../modules/tags
- ecs_service.tags.aws_account in ../../modules/aws_account

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "template" (hashicorp/template) 2.2.0...
- Downloading plugin for provider "pagerduty" (terraform-providers/pagerduty) 1.8.0...
- Downloading plugin for provider "aws" (hashicorp/aws) 3.22.0...
- Downloading plugin for provider "null" (hashicorp/null) 3.0.0...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 3.22"
* provider.null: version = "~> 3.0"
* provider.pagerduty: version = "~> 1.8"
* provider.template: version = "~> 2.2"


Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has 
moved to PagerDuty/pagerduty. Please update your source in required_providers.


Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see 
any changes that are required for your infrastructure. All Terraform commands should 
now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

The command ls -Lisa shows that there are 48 files. Actually, we have a quite complex structure of modules and many resources are created within that stack. The out of that command is:

total 48
8674812825 0 .                          8674812829 8 backend.tf                 8674812832 8 task_definition.json
8674474246 0 ..                         8674812830 8 ecs_service.tf             8674812833 8 task_role.tf
8674820253 0 .terraform                 8674812831 8 outputs.tf                 8674812834 8 variables.tf

Hm, as @stuart-c mentioned terraform state pull should refer to the reverted state. So you could again check for the S3 backend definition, additional TF_WORKSPACE dependencies or generate more verbose output using TF_LOG=TRACE terraform init.

The output of TF_LOG=TRACE terraform init is attached here tf_init.txt (30.9 KB)

When I run terraform state pull this time it returned the state file which is attached here. It shows that the version is 0.12.29.tfpull.txt (75.8 KB)

But again, when I execute terraform plan, I get the attached stacktferror.txt (4.2 KB) with the below error:

Error: state snapshot was created by Terraform v0.13.5, which is newer than current 
v0.12.29; upgrade to Terraform v0.13.5 or greater to work with this state

Any further help on this or we should give up on this and might try somehow regenerate file from infrastructure?

Does your codebase also refer to another remote-state for input?
module.ecs_service.data.terraform_remote_state.iam: Refreshing state...

Could you check that one, too?

You might want to slightly anonymize traces before uploading :slight_smile: