Skip to content

OpenTofu: decryption failed for all provided methods for the state

IaCtested on OpenTofu v1.13.2, State-Verschlüsselung

OpenTofu can encrypt the state on the client side. If the configuration supplies the wrong key, OpenTofu cannot read the state – it is not broken, just unreadable. A single digit off is enough.

Symptom

tofu plan -no-color 2>&1 | grep -E '^Error'
Error: error loading state: decryption failed for all provided methods

“After switching machines I can no longer read the state. Is it broken?”

Diagnosis

Compare the value that is set with the expected one.

env | grep -c '^TF_VAR_state_passphrase='
echo "$TF_VAR_state_passphrase"
1
lab-state-passphrase-124

Causes

TF_VAR_state_passphraseWrong passphrase in the environment

The passphrase comes from an environment variable. The shell holds a value that is one digit off.

new machineKey not carried over

On a new machine the file that sets the key is missing or contains a different value.

Fix

Load the correct key and plan again.
source ~/tf-lab/setup/env.sh
tofu plan -no-color | tail -1
No changes. Your infrastructure matches the configuration.

Don't: Turn off encryption to get back in – without the right key the state cannot be decrypted anyway, and unencrypted states hold passwords in plain text.

▶
Break it yourself and find it – free
OpenTofu hands-on, Day 15: fix eight faults in state, providers, modules and encryption against the clock.
Go to day 15 →

FAQ

Where should the key come from in production?

From a vault or KMS, not from memory or a file in the repository.

Can I rotate the key?

Yes, with a fallback method – OpenTofu then reads with the old key and writes with the new one (Day 9).

Related error patterns

More