hashicorp/terraform · error

error marshaling prior state

Error message

error marshaling prior state: %s

What it means

Top-level propagation error from Marshal: jsonstate.Marshal failed while rendering the prior state file embedded in the plan JSON. The wrapped error comes from the state marshaller and usually indicates a schema or decode problem with prior-state resources.

Solutions

  1. Read the wrapped error for the failing state resource.
  2. Ensure all providers for resources present in state are initialized.
  3. Run `terraform refresh`/`state upgrade` if the state format is from an older version.
  4. Regenerate the plan after aligning versions.
Defensive patterns

Strategy: validation

Validate before calling

// Ensure every resource present in prior state has a schema before marshaling.
func stateSchemasOk(sf *statefile.File, schemas *terraform.Schemas) error {
    if sf == nil { return nil }
    for _, m := range sf.State.Modules {
        for _, r := range m.Resources {
            for _, i := range r.Instances {
                _ = i
                // look up ResourceTypeConfig by provider/mode/type; error if Body == nil
            }
        }
    }
    return nil
}

Prevention

When it happens

Trigger: A resource in prior state references a provider schema missing at render time, or its value cannot be decoded/marshaled against the current schema.

Common situations: State written by an older Terraform/provider being rendered by a newer one with incompatible schemas; provider removed from config but still referenced in state.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/02b73e467421cc09. Report an issue: GitHub.

Appendix: source

Thrown at internal/command/jsonplan/plan.go:337

			return nil, fmt.Errorf("error in marshaling deferred action invocations: %s", err)
		}
	}

	// output.OutputChanges
	if output.OutputChanges, err = MarshalOutputChanges(p.Changes); err != nil {
		return nil, fmt.Errorf("error in marshaling output changes: %s", err)
	}

	// output.Checks
	if p.Checks != nil && p.Checks.ConfigResults.Len() > 0 {
		output.Checks = jsonchecks.MarshalCheckStates(p.Checks)
	}

	// output.PriorState
	if sf != nil && !sf.State.Empty() {
		output.PriorState, err = jsonstate.Marshal(sf, schemas)
		if err != nil {
			return nil, fmt.Errorf("error marshaling prior state: %s", err)
		}
	}

	// output.Config
	output.Config, err = jsonconfig.Marshal(config, schemas)
	if err != nil {
		return nil, fmt.Errorf("error marshaling config: %s", err)
	}

	// output.Changes.ActionInvocations
	if p.Changes.ActionInvocations != nil {
		if output.ActionInvocations, err = MarshalActionInvocations(p.Changes.ActionInvocations, schemas); err != nil {
			return nil, fmt.Errorf("error marshaling action invocations: %s", err)
		}
	}

	return json.Marshal(output)
}

View on GitHub (pinned to d32a084675)