Terraform: Terraform cannot convert targeted plan to json output

Created on 6 Nov 2019  路  8Comments  路  Source: hashicorp/terraform

Terraform v0.12.13
HCL code not relevant

Error Output

Failed to marshal plan to json: error marshaling prior state: unsupported attribute "purpose"

Expected Behavior

terraform should have parsed the default plan file to json output.

Actual Behavior

Failed to marshal plan to json: error marshaling prior state: unsupported attribute "purpose"
No json output generated

Steps to Reproduce

  1. Run terraform plan with a -target flag
  2. Show the plan using terraform show with the -json flag
  3. Error raised

Additional Context

This is the full command I actually run:

terraform plan -var-file ../tfvars/compute.tfvars.json -target=module.network -out plan.bin terraform show -json plan.bin > plan.json

Also, when planning without a -target, this works as expected.

This is my first issue , please let me know if I need to add anything else.

bug cli v0.12

Most helpful comment

I'm seeing something similar but the error is
Failed to marshal plan to json: error marshaling prior state: unsupported attribute "placement_group_id"
I'm using v0.12.10 and AWS provider v2.45.0

All 8 comments

@cr-zlilaharon I have tried to reproduce this an I think I need a specific TF config in order to reproduce it. Here's what I've done, using terraform 0.12.17:

I used a simple terraform config with one resource:

provider "aws" {
  region     = "us-west-2"
}

resource "aws_instance" "foo" {
  ami = "ami-40d1f038" // Amazon linux HVM instance store
  instance_type               = "t2.micro"
}

Then I ran a plan with -target enabled

terraform plan -target=aws_instance.foo -out plan.bin

`` Then I ran aterraform showwith-json` as you described

terraform show -json plan.bin > plan.json

All those commands ran as expected and I wasn't able to reproduce the error you reported.

If you can put together a simple terraform config that reproduces this I'm happy to try and reproduce it again on my side.

I'm seeing something similar but the error is
Failed to marshal plan to json: error marshaling prior state: unsupported attribute "placement_group_id"
I'm using v0.12.10 and AWS provider v2.45.0

I'm seeing one or the other of the following when running terraform show -json

Failed to marshal plan to json: error marshaling prior state: unsupported attribute "tags"
Failed to marshal plan to json: error marshaling prior state: unsupported attribute "outpost_arn"

Using:

  • Terraform v0.12.24
  • terraform-provider-aws_v2.55.0_x4

@mpereira please note that the output_arn attributes were not added into the Terraform AWS Provider until very recently with version 2.60.0 -- is there potentially a provider version mismatch in your case? I'm not familiar with the command or its code, but figured I would mention it here in case it helps resolve your particular issue.

@bflad upgrading the Terraform AWS Provider to version 2.62.0 fixed the issue, thank you so much!

Hi. I had the same problem and I realized after execute a refresh terraform command It starts working.

terraform refresh -var-file prod.tfvars

I hope It could help you

Hi all,

Based on the more recent comments about this error it seems like this problem could be caused when an argument is removed from a resource type schema in a provider. My theory -- not yet proven -- is that by running with -target you all are excluding a resource whose resource type schema has changed, and thus preventing Terraform from upgrading its state during the refresh phase. That means that the prior state in the plan is still using the prior value that was produced with the old schema, and therefore the JSON plan renderer (which is schema-based) fails to convert it.

If this _is_ the cause, the solution is unfortunately likely to be a bit tricky. Terraform generally assumes that all existing resource instances will be updated to the latest schema as part of the refresh phase, and the -target argument is causing that assumption to be violated. Fixing that will either require changing that assumption or making -target no longer exclude objects from having their state schemas upgraded, both of which are a very big change relative to how -target works today.

A more tactical fix we could try is to have the terraform show -json code itself try to remove any attributes that are not part of the current schema before trying to convert to JSON. That should address the specific symptoms we've seen here, but would not be a complete solution because there are other similar issues it could not address, such as a new provider release changing the type of an existing attribute _without_ removing it: only the schema upgrade logic in the provider knows how to convert the value to the new type, and so ensuring that the schema upgrade still happens even when -target is used would be the only way to fix this fully.

Hi. I had the same problem and I realized after execute a refresh terraform command It starts working.

terraform refresh -var-file prod.tfvars

I hope It could help you

worked for me

Was this page helpful?
0 / 5 - 0 ratings