hashicorp/terraform · error

Error creating temporary directory: %s

Error message

Error creating temporary directory: %s

What it means

Returned by backendMigrateNonEmptyConfirm when os.MkdirTemp("", "terraform") at line 505 fails. This helper builds the confirmation prompt when both source and destination have non-empty state — it writes both states to temp files so the user can compare them. The temp directory creation failed.

Source

Thrown at internal/command/meta_backend_migrate.go:507

			Description: fmt.Sprintf(
				strings.TrimSpace(inputBackendMigrateEmpty),
				opts.SourceType, opts.DestinationType),
		}
	}

	return m.confirm(inputOpts)
}

func (m *Meta) backendMigrateNonEmptyConfirm(
	sourceState, destinationState statemgr.Full, opts *backendMigrateOpts) (bool, error) {
	// We need to grab both states so we can write them to a file
	source := sourceState.State()
	destination := destinationState.State()

	// 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)
	}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Free space or inodes on the volume backing the temp directory (often /tmp).
  2. Set TMPDIR to a writable location with space: 'export TMPDIR=/path/to/writable/dir'.
  3. Ensure the user running terraform has write permission on the temp dir.
  4. In containers, mount /tmp as read-write with adequate size.
  5. If disk pressure is chronic, clean up old terraform temp dirs (/tmp/terraform*).

Example fix

# before: /tmp full -> Error creating temporary directory: ... no space
# after: point TMPDIR at a volume with space
export TMPDIR=/var/tmp/opencode
terraform init
Defensive patterns

Strategy: validation

Validate before calling

# Ensure a writable temp dir with space before running init.
mkdir -p "${TMPDIR:-/tmp}" && : > "${TMPDIR:-/tmp}/.tfprobe" \
  || { export TMPDIR="$HOME/.tf-tmp"; mkdir -p "$TMPDIR"; }
df -h "${TMPDIR:-/tmp}" | awk 'NR==2{if($5+0>90){print "temp volume nearly full"; exit 1}}'
terraform init

Try / catch

# Set a known-writable TMPDIR and retry.
export TMPDIR="${TF_TMPDIR:-$HOME/.tf-tmp}"; mkdir -p "$TMPDIR"
terraform init || terraform init  # second attempt on the corrected TMPDIR

Prevention

When it happens

Trigger: The OS cannot create a temp directory under the system temp dir (os.TempDir): no space left on /tmp (ENOSPC), permission denied creating under TMPDIR, read-only /tmp in a hardened container, or the process ulimit on files/dirs is exhausted. Wrapped at line 507.

Common situations: CI runner with full /tmp; container with read-only /tmp mount; SELinux/AppArmor denying tempdir creation; low inodes; running as a user without write access to TMPDIR.

Related errors


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