OpenTofu state encryption: migrate, rotate keys and recover
Encrypt an existing OpenTofu workspace only after both its normal runner and its recovery runner can read a test copy with the intended keys.
- Files covered by separate settings
- State and saved plans
- Method used for new writes
- Primary method
- Recovery prerequisite
- Usable key material and encryption configuration
Where does encryption take effect?
OpenTofu can encrypt state and saved plans before it writes them to local storage or a backend. State and plan encryption have separate settings. Start by deciding who may run OpenTofu, who may read stored objects and who can restore an older copy.
This guide follows the encryption configuration documented in the OpenTofu 1.12 manual. Record the exact CLI version in your runner image before adopting an example. The backend stores the resulting object and may offer its own encryption, version retention and access controls. OpenTofu's encryption adds a requirement on the reader: obtaining the object is no longer enough to decode it without the corresponding method and key. The provider plugin that manages cloud resources has a separate role from the key provider that supplies encryption material.
Draw the actual route from a workspace to storage. Include the machine that generates a saved plan, the approval job that retains it, and the job that later applies it. List the environment settings and identities available at each point. If a different identity performs disaster recovery, give it a separate entry. A successful normal apply tells you only that one identity could perform that run. It says little about whether tomorrow's approval runner or next month's restore job can read what was written.
How do key providers and methods fit together?
An encryption configuration names a key provider, names a method that uses its keys, then selects that method for state or plan. Keep those names stable once artifacts exist.
In a passphrase-based setup, the documented example uses a PBKDF2 key provider and an AES-GCM method. A managed key service introduces different dependencies, such as the runner's cloud credentials, region and access to the key service. Choose a provider for which the team can recover both the key and its configuration. A passphrase pasted into tracked configuration defeats much of the access separation. Supply it through a controlled secret source and keep command output and runner logs out of broadly readable build artifacts.
An illustrative state selection inside the encryption configuration is state { method = method.aes_gcm.primary } when a method named primary has actually been configured. A saved plan needs its own selection in the plan block. Treat the key provider and method names as durable identifiers. The encrypted file carries metadata needed to locate the right settings. A routine refactor that renames these blocks can make historical state unreadable. Prepare a rollover when changing the method or provider identity.
What must work before touching production state?
Use a separate backend location and an isolated copy of the workspace. Test the normal reader, an approval runner if applicable and a recovery identity with fresh credentials.
Retain the original backend version or backup in a restricted location, together with its workspace identity and the configuration needed to read it. Check the backend path before any rehearsal that can write. A copied working directory can still point at the production bucket and key. Prevent the rehearsal identity from modifying live cloud resources. If a full plan would require credentials the rehearsal environment should not have, use a disposable stack with representative state, then document what remains to be tested against a genuine restricted backup.
Create and read an encrypted test artifact through the same OpenTofu binary used for delivery. Repeat after starting a new runner with no cached environment or cloud session. Check that the authorized reader succeeds and that a runner deliberately lacking the decryption grant fails at the expected boundary. Avoid printing decrypted state or plan contents merely to prove that access works. Record the checked identities and result, the backend reference, the CLI version and the key identifier. A screenshot showing unreadable object bytes cannot substitute for a recovery exercise.
How does an existing plaintext workspace move across?
Configure the new encrypted method as primary and allow the documented unencrypted method as a temporary fallback for the existing workspace. Coordinate all writers before the first encrypted write.
Without the migration fallback, an encryption-configured reader can reject the old plaintext state. An older pipeline lacking the encryption configuration can fail on the new state after the change. Book a window in which one runner owns the workspace, freeze scheduled jobs and check for manual applies. Use the rehearsed configuration and review the ordinary infrastructure plan. Unexpected resource replacements or provider changes need investigation before an encryption migration can safely share that change.
Verify that the intended runner can read the rewritten current state, then check downstream readers that consume remote-state outputs. They may need their own decryption settings and access to the key. Test both creation and later consumption of any saved plan that crosses jobs. Remove the plaintext fallback and enable the documented enforcement settings only after the intended artifacts and readers have been verified. Existing backup generations and old plan artifacts can still contain plaintext. Keep their access restricted and apply the organization's retention policy to them explicitly.
How do you rotate a method without stranding old files?
Place the new method first and the previous method in a fallback. OpenTofu writes with the primary method and can try the fallback when it cannot read an existing file with the primary method.
Work through the file lifetimes before deciding when to withdraw the old key. The current state object may be rewritten immediately, a previously approved plan may wait for its apply job, and a retained state backup may remain within policy for weeks. Inventory all three categories. Decide whether to regenerate outstanding plans, finish them with an approved compatible reader, or invalidate them. Confirm that the configuration and credentials for each route match that choice.
For example, suppose the current state now reads correctly with only the new key, but the recovery policy requires restoring yesterday's state. Run that restoration in an isolated backend using the fallback and the old key. A failure there is a reason to retain or repair the recovery path before withdrawing access. Store the prior configuration and its key identifier where the recovery operator can find them. Record the oldest retained artifact that still needs the old method and assign an owner to verify the next retirement date.
What do you do when a runner cannot decrypt state?
Stop further writes, identify the exact state generation and check the runner identity, key availability and named encryption configuration. Recover access in isolation before allowing another production apply.
Compare the failing run with the last successful run. A new runner image can omit environment configuration. A changed cloud identity can lose decrypt permission while retaining access to the backend object. A renamed method can prevent OpenTofu from matching metadata even when the key material is intact. If an older backup works but the latest object fails, check which method wrote each generation. Copying an old state over the current one before understanding that boundary can discard legitimate changes.
If the only usable key has been permanently lost, possession of an encrypted backup does not make that backup recoverable. Escalate to the infrastructure owners and reconstruct the situation from the backend history and cloud inventory. Document which resources may have changed since the last readable generation. Resume ordinary operations after the team can read the intended state, review a plan for unexpected changes and demonstrate that the configured recovery path also works from a clean runner.
Frequently asked questions
Does marking a variable sensitive replace encryption?
Sensitive display handling can limit what appears in normal output. The stored state and exported artifacts still need access controls and the chosen encryption policy.
Will a backend backup include my decryption key?
A backup of the encrypted state object preserves its contents. Test recovery of the key and configuration separately under the identities your recovery procedure uses.
Methodology and sources
The procedures follow the versioned OpenTofu 1.12 documentation checked December 3, 2026. Key-service policies, access to retained generations and remote-state consumers must be verified for the selected backend.