Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform cannot convert targeted plan to json output #23297

Open
cr-zlilaharon opened this issue Nov 6, 2019 · 12 comments
Open

Terraform cannot convert targeted plan to json output #23297

cr-zlilaharon opened this issue Nov 6, 2019 · 12 comments
Labels
bug cli v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@cr-zlilaharon
Copy link

cr-zlilaharon commented Nov 6, 2019

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.

@hashibot hashibot added bug cli v0.12 Issues (primarily bugs) reported against v0.12 releases labels Nov 6, 2019
@danieldreier
Copy link
Contributor

@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 a `terraform show` with `-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.

@GJKrupa
Copy link

GJKrupa commented Feb 3, 2020

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

@mpereira
Copy link

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

@bflad
Copy link
Contributor

bflad commented May 14, 2020

@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.

@mpereira
Copy link

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

@blazart
Copy link

blazart commented May 29, 2020

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

@apparentlymart
Copy link
Contributor

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.

@cr-liadsh
Copy link

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

@kichitan
Copy link

In my case.

I ran my plan while using this on my main.tf

provider "aws" {
version = "~> 2.7"

then I removed version = "> 2.7" from my main and save it, then when I destroy I got the error, so I re-added the version = "> 2.7" to my main.tf ran the destroy and it worked.

@pjroth
Copy link

pjroth commented May 6, 2021

I observed this same error today using 0.13.5 when using the hashicorp/tfe provider. Note this was executed in Terraform Cloud.

Plan: 0 to add, 0 to change, 5 to destroy.
Failed running command "cd '/terraform/prod/_global/terraform-cloud' && envdir /env terraform show -json /terraform/terraform.tfplan" (exit 1)
Output: Failed to marshal plan to json: error marshaling prior state: unsupported attribute "external_id"

I updated to 0.14.11 and this bug went away. My plan succeeded with 0.15.2 as well.

@theothertom
Copy link

When I hit this issue, it was related to attempting to get the JSON form of a plan that had been generated by a different version of a provider. Specifically, this was due to copying a plan saved by a CI server to my workstation which is admittedly a bit of an edge-case.

Slightly confusingly, generating a non-JSON form of this plan worked correctly.

@Zodzie
Copy link

Zodzie commented Jul 21, 2023

after running into this same issue
Failed to marshal state to json: unsupported attribute ... ERRO[0077]

after clearing cache and lock file the show -json command worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cli v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

No branches or pull requests