hashicorp/terraform · error
Error saving temporary state: %s
Error message
Error saving temporary state: %s
What it means
Returned by backendMigrateNonEmptyConfirm's saveHelper when writing the SOURCE state to a temp file fails (saveHelper at line 512-515, called for source at line 520). The statemgr.NewFilesystem(path).WriteState(source) call errored, so the comparison files for the confirmation prompt couldn't be produced.
Source
Thrown at internal/command/meta_backend_migrate.go:521
// 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)
}
// 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?",
Description: fmt.Sprintf(
strings.TrimSpace(inputBackendMigrateNonEmptyCloud),
opts.SourceType, sourcePath, destinationPath, appName),View on GitHub (pinned to c9def3e214)
Solutions
- Free disk space on the temp volume and retry.
- Set TMPDIR to a local writable disk (avoid network filesystems for temp).
- Disable or exempt terraform from AV scanners that lock new files.
- Verify write permissions on the temp dir for the running user.
- Retry; if persistent, capture the inner error for the exact write failure code.
Example fix
# before: write source state to /tmp fails (NFS hiccup) # Error saving temporary state: ... write: I/O error # after: use a local disk for TMPDIR export TMPDIR=/local/tmp terraform init
Defensive patterns
Strategy: validation
Validate before calling
# Verify temp dir is writable AND has space for at least the largest state.
maxstate=$(terraform state pull 2>/dev/null | wc -c)
avail=$(df -P "${TMPDIR:-/tmp}" | awk 'NR==2{print $4}')
[ "$((avail*1024))" -gt "$((maxstate*3))" ] || { echo 'not enough temp space for both states'; exit 1; }
terraform init Try / catch
# On temp write failure, switch TMPDIR and retry.
terraform init 2>/tmp/init.err || {
grep -q 'Error saving temporary state' /tmp/init.err || exit 1
export TMPDIR="$HOME/.tf-tmp"; mkdir -p "$TMPDIR"
terraform init
} Prevention
- Ensure temp disk can hold at least 2x the largest state plus headroom.
- Use a local disk for TMPDIR, not network filesystems.
- Exempt terraform from AV scanners that lock new files.
- Free disk before migrations involving large states.
When it happens
Trigger: After the temp dir was created successfully, writing the source .tfstate file fails: disk fills between mkdir and write, write permission on the temp subdir revoked, path too long, or WriteState encounters an internal serialization error. Wrapped at line 521.
Common situations: Disk nearly full — mkdir succeeds but write fails; antivirus/scanner locking newly created files on Windows; NFS/ network filesystem write hiccup; SELinux relabeling denying the write.
Related errors
- Error creating temporary directory: %s
- Error copying state from the previous %q backend to the newl
- Can't ask approval for state migration when interactive inpu
- Migrating state from HCP Terraform or Terraform Enterprise t
- error deleting workspace %s: %w
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/5937a9b6b0dc76e9.
Report an issue: GitHub.