hashicorp/terraform · error

error in marshaling resource drift: %s

Error message

error in marshaling resource drift: %s

What it means

Top-level wrapper: marshaling the resource_drift list (out-of-band changes found during refresh) via MarshalResourceChanges(p.DriftedResources, ...) failed. The inner error is typically a missing schema for one of the drifted resources.

Source

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

	// output.ResourceDrift
	if len(p.DriftedResources) > 0 {
		// In refresh-only mode, we render all resources marked as drifted,
		// including those which have moved without other changes. In other plan
		// modes, move-only changes will be included in the planned changes, so
		// we skip them here.
		var driftedResources []*plans.ResourceInstanceChangeSrc
		if p.UIMode == plans.RefreshOnlyMode {
			driftedResources = p.DriftedResources
		} else {
			for _, dr := range p.DriftedResources {
				if dr.Action != plans.NoOp {
					driftedResources = append(driftedResources, dr)
				}
			}
		}
		output.ResourceDrift, err = MarshalResourceChanges(driftedResources, schemas)
		if err != nil {
			return nil, fmt.Errorf("error in marshaling resource drift: %s", err)
		}
	}

	if err := output.marshalRelevantAttrs(p); err != nil {
		return nil, fmt.Errorf("error marshaling relevant attributes for external changes: %s", err)
	}

	// output.ResourceChanges
	if p.Changes != nil {
		output.ResourceChanges, err = MarshalResourceChanges(p.Changes.Resources, schemas)
		if err != nil {
			return nil, fmt.Errorf("error in marshaling resource changes: %s", err)
		}
	}

	if p.DeferredResources != nil {
		output.DeferredChanges, err = MarshalDeferredResourceChanges(p.DeferredResources, schemas)
		if err != nil {

View on GitHub (pinned to c9def3e214)

Solutions

  1. Inspect the inner error to find which drifted resource failed.
  2. Install the provider for the drifted resource with `terraform init`.
  3. Align provider versions between the run that detected drift and the render.
Defensive patterns

Strategy: try-catch

Try / catch

out, err := jsonplan.Marshal(config, plan, sf, schemas)
if err != nil && strings.Contains(err.Error(), "marshaling resource drift") {
    // a drifted resource failed; ensure its provider is installed
}
return err

Prevention

When it happens

Trigger: len(p.DriftedResources) > 0 and marshaling one drifted ResourceInstanceChangeSrc errors. In refresh-only mode all drifted resources are rendered; in other modes only non-NoOp drift is rendered.

Common situations: Drift detected on a resource whose provider is not installed at render time; refresh-only mode combined with a provider downgrade; state referencing a resource whose provider was removed.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/1fd19558046c639b. Report an issue: GitHub.