hashicorp/terraform · error

Error saving temporary state

Error message

Error saving temporary state: %s

What it means

Returned by `backendMigrateNonEmptyConfirm` when writing the SOURCE state snapshot to the temp directory fails. The helper opens a filesystem state manager at the temp path and calls `WriteState`; failure prevents the confirmation diff from being shown.

Solutions

  1. Point `TMPDIR` at a larger writable volume and retry.
  2. Use `-force-copy` to bypass the diff confirmation for non-empty states.
  3. Free space on the temp filesystem.
  4. If the source state is suspiciously large, audit it for runaway resources before migrating.

Example fix

# before
terraform init   # temp write of source state failed
# after
TMPDIR=/data/tftmp terraform init -force-copy
Defensive patterns

Strategy: validation

Validate before calling

# Ensure temp volume has at least 2x the largest state size.
TMPDIR=${TMPDIR:-/tmp}; df -h "$TMPDIR"
# Optionally bypass the diff:
terraform init -force-copy

Try / catch

# On source-snapshot write failure, enlarge TMPDIR or skip the diff:
if ! terraform init >/tmp/init.log 2>&1; then
  grep -q 'Error saving temporary state' /tmp/init.log \
    && { echo 'Temp too small for source snapshot; set TMPDIR or use -force-copy' >&2; exit 2; }
  cat /tmp/init.log; exit 1
fi

Prevention

When it happens

Trigger: Fires when `saveHelper(opts.SourceType, sourcePath, source)` returns an error — the temp directory was created (697 passed) but writing `1-<SourceType>.tfstate` failed due to disk-full mid-write, permission flip, or a serialization error in the state.

Common situations: tmpfs filled by the source state size, concurrent cleanup racing with the write, an extremely large state that exceeds the temp volume, or filesystem encoding issues in `opts.SourceType`.

Related errors


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

Appendix: source

Thrown at internal/command/meta_backend_migrate.go:541

	// Save both to a temporary
	td, err := os.MkdirTemp("", "terraform")
	if err != nil {
		return false, fmt.Errorf("Error creating temporary directory: %s", err)
	}
	defer os.RemoveAll(td)

	// Helper to write the state
	saveHelper := func(n, path string, s *states.State) error {
		mgr := statemgr.NewFilesystem(path)
		return mgr.WriteState(s)
	}

	// Write the states
	sourcePath := filepath.Join(td, fmt.Sprintf("1-%s.tfstate", opts.SourceType))
	destinationPath := filepath.Join(td, fmt.Sprintf("2-%s.tfstate", opts.DestinationType))
	if err := saveHelper(opts.SourceType, sourcePath, source); err != nil {
		return false, fmt.Errorf("Error saving temporary state: %s", err)
	}
	if err := saveHelper(opts.DestinationType, destinationPath, destination); err != nil {
		return false, fmt.Errorf("Error saving temporary state: %s", err)
	}

	srcWord := backendHumanName(opts.Source)
	dstWord := backendHumanName(opts.Destination)

	// Ask for confirmation
	var inputOpts *terraform.InputOpts
	if opts.DestinationType == "cloud" {
		appName := "HCP Terraform"
		if cloudBackend, ok := opts.Destination.(*cloud.Cloud); ok {
			appName = cloudBackend.AppName()
		}
		inputOpts = &terraform.InputOpts{
			Id:    "backend-migrate-to-tfc",
			Query: "Do you want to copy existing state to HCP Terraform?",

View on GitHub (pinned to d32a084675)