State encryption and migration rehearsal
State migration is an ownership change
A migration must preserve the relationship between configuration, state and actual infrastructure. Do not allow two engines or pipelines to manage one live state independently. Before changing the writer, identify every root module and remote-state consumer, establish a maintenance window, pause writes and capture protected backups of configuration, lockfiles, backend state and relevant metadata. Check the version-specific migration path and provider constraints; an engine change should not accidentally upgrade providers.
network root -> publishes subnet ID -> app rootnetwork root -> publishes subnet ID -> reporting rootEach root has its own state and independent reader permissions.Migration gate: validate all readers before changing writer format.The reader/writer map helps decide a safe sequence. Verify compatibility in the actual direction of each dependency. If the source root changes format, a downstream reader might fail even though its own state is unchanged. See https://opentofu.org/docs/intro/migration/multiple-configurations/.
What native encryption protects
OpenTofu can encrypt persisted state and saved plans using configured encryption methods and key providers. This protects the contents of stored artifacts against someone who obtains them without the necessary keys. It does not protect a compromised authorized runner, plaintext output, logs, deletion, or replay of an older artifact. Backend-side encryption is a separate control. Sensitive markings are not a substitute for either. Plan encryption and state encryption must be considered separately.
terraform { encryption { # Key provider and method intentionally omitted. state { enforced = true } plan { enforced = true } }}This incomplete policy illustration requires actual method configuration; it is not an executable setup. Enforcement is intended to prevent unencrypted writes; verify the exact version behavior and use a reviewed key provider and method. Never put a real passphrase in source, shell history or training artifacts. See https://opentofu.org/docs/language/state/encryption/.
Transition and key recovery
Existing plaintext state cannot simply be treated as encrypted. OpenTofu documents explicit migration using a primary encryption method and an appropriate fallback for existing data. The fallback is a reading mechanism; new writes use the primary method. For a key change, the old method may be needed to read previous artifacts. Do not leave a permissive fallback in place indefinitely.
Illustrative transition only1. Secure backup + tested key recovery2. New primary encryption method + old read fallback3. Authorized rewrite, then inspect format and consumer access4. Confirm recovery with new method5. Remove fallback only after review of old artifact retentionRetain access to old keys for authorized backups under the retention policy. Account for residual plaintext copies and old saved plans. Never delete or rotate real keys in this lab. An encrypted state without a recoverable key may be unusable. See https://opentofu.org/docs/v1.11/language/state/encryption/.
Rehearse before changing ownership
Create isolated copies using synthetic data, not a copy of live production state with real resource identifiers. If testing an actual migration is separately authorized, use an isolated disposable environment with no route to real resources and verify all provider and backend targets. Compare baseline and target plans by resource address, action and intended attributes; do not compare only summary totals.
Baseline: label unchangedTarget: label unchangedUnexpected replacement: noneReader checks: all compatibleRestore drill: passedOutcome: eligible for human review, not approved for applyAn unexplained replacement, missing reader test or untested restore stops the rehearsal. Use fresh plans from the correct tool; do not reuse plan binaries. Confirm application behavior separately. If a rehearsal has written any real external data, restoring an old state alone would not undo those changes. See https://opentofu.org/docs/intro/migration/migration-guide/ and https://opentofu.org/docs/cli/commands/plan/.
Design a rollback boundary
Before a migration, define who can stop it, the last verified checkpoint, how to restore state and keys, and how to verify ownership and live infrastructure before enabling writes. If the new tool wrote a state format the old tool cannot read, a direct binary switch is not a safe rollback. Reconcile actual side effects first.
Missing key -> HOLD; recover through approved key procedureReader rejects new format -> HOLD; resolve compatibilityUnexpected plan action -> HOLD; inspect differenceState restored but actual resources differ -> HOLD; reconcileAll evidence present -> eligible for human reviewUse the offline decision cases to explain which missing evidence blocks migration or rollback. The model tests policy decisions only, not OpenTofu encryption or state format compatibility. Next: Terragrunt units and configuration reuse.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
State encryption and migration rehearsal — knowledge check
Review migration compatibility, encryption and recovery.
What is a first migration preparation step?
What does native state encryption primarily protect?
What is a fallback method used for during a transition?
A required decryption key is unavailable. What now?
Why is restoring old state not always sufficient rollback?