OpenTofu vs Terraform: encryption and migration

Advanced · 20 min

OpenTofu's state and plan encryption, how the two tools have diverged since 1.7 by release, and how to migrate safely with backups and a small first change.

Before thisUpgrading Terraform and providersSensitive values and secrets in state

Ned in the Cloud · 20 min

OpenTofu vs Terraform: encryption and migration

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Ned in the CloudReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • State files hold secrets as plain JSON; OpenTofu 1.7 added optional client-side encryption of state and plan, and I found nothing equivalent in Terraform's 1.7 to 1.17 changelogs.
  • The encryption block takes a key provider (pbkdf2, aws_kms, gcp_kms, azure_vault, openbao, external), a method such as aes_gcm, and state and plan sections.
  • Use a fallback of unencrypted to encrypt existing state, and enforced = true to refuse unencrypted writes; losing the key means losing the state.
  • Migrate by backing up state, running init and plan in a copy, trying one low-risk configuration first; the first tofu apply can rewrite the state format.

Secrets in a file in a bucket

Anything a resource knows about, the state file knows about. A database password you set through Terraform, a generated private key, an API token returned by a provider: all of it is written into state as plain JSON. The sensitive-values lesson explained why sensitive = true hides a value from the terminal but not from the file. So the state bucket, its backups and every person and CI role with read access become part of your secret store.

The standard answers are access control on the bucket, server-side encryption and, since Terraform 1.10 and 1.11, ephemeral values and write-only arguments that keep a secret out of state entirely. OpenTofu added another one: encrypt the state file itself before it is written, so the bucket only ever holds ciphertext. This lesson covers that, then the practical question of how you move between the two tools.

What I could demonstrate

OpenTofu is not installed here, so I could not run its encryption. To show the value of the idea I used OpenSSL on a hand-made state file with a fake password. This is not OpenTofu’s method (OpenTofu uses AES-GCM with a key provider; I used aes-256-cbc with a PBKDF2-derived key because that is what the openssl enc command offers). It only shows what encryption at rest changes:

$ grep -o '"password":"[^"]*"' terraform.tfstate
"password":"hunter2-s3cret-value"

$ openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -salt -pass env:PASS -in terraform.tfstate -out terraform.tfstate.enc
$ grep -q hunter2 terraform.tfstate.enc && echo found || echo "not found"
not found
$ openssl enc -d ... -in terraform.tfstate.enc | grep -o '"serial":[0-9]*'
"serial":7
$ PASS="wrong" openssl enc -d ... -in terraform.tfstate.enc
bad decrypt

The secret is readable in the plain file, absent from the encrypted one, recoverable with the passphrase and not with a wrong one. The same properties apply to OpenTofu’s feature, and so does the hazard: lose the key and the state is gone.

How OpenTofu configures it

The OpenTofu docs, which cover v1.13.x, say state and plan encryption is configured in an encryption block inside terraform. The OpenTofu 1.7 changelog says it introduced optional end-to-end encryption for state files, and I take that as the version that introduced it. The documented shape:

terraform {
  encryption {
    key_provider "pbkdf2" "main" {
      passphrase = var.state_passphrase   # at least 16 characters
    }

    method "aes_gcm" "main" {
      keys = key_provider.pbkdf2.main
    }

    state {
      method = method.aes_gcm.main
    }

    plan {
      method = method.aes_gcm.main
    }
  }
}

I wrote that from the docs’ structure and did not run it. A passphrase through a variable is the simplest case. The docs list other key providers: aws_kms, gcp_kms, azure_vault, openbao and external. Those let a cloud’s key service hold the key, which suits production better than a passphrase in a CI variable. The methods are aes_gcm, external, and unencrypted, which is the migration step.

Three behaviours matter in practice:

  • fallback lets OpenTofu read data with an old method while it always writes with the new one. Use it for key rotation and for turning encryption on for an existing state: set the fallback to unencrypted, apply, and the state is rewritten encrypted.
  • enforced = true on state or plan stops OpenTofu from writing unencrypted data, which the docs recommend when configuring encryption through environment variables.
  • remote_state_data_sources configures decryption for terraform_remote_state readers, so split states can still read each other’s outputs.

The plan file can be encrypted too, which matters for the saved plans in CI, since a plan holds sensitive values in cleartext. What encryption does not do: hide secrets from anyone who can run tofu with the key, or from logs. It protects the file at rest.

I searched the Terraform changelogs from 1.7 through 1.17 for a state encryption feature and found none. So it looks OpenTofu-only as of 28 November 2026.

How far the two have drifted

The projects share a history and a lot of syntax, and each now has things the other lacks. From the two changelogs, by the version that first listed the item:

Feature OpenTofu Terraform
State and plan encryption 1.7 not found in changelogs
provider::name::func() functions 1.7 1.8
-exclude plan option 1.9 not found in my search
for_each on provider blocks 1.9 not found in my search
S3 locking without DynamoDB 1.10 1.11 (use_lockfile)
Ephemeral values 1.11 1.10
enabled meta-argument 1.11 not found
Variables in module sources 1.8 (limits) 1.16
Terraform Policy, Stacks, Actions none 1.14 to 1.17

That table is only as good as my grep through changelog text, and “not found” means I did not see it, not that it is absent. Check both projects’ current docs for any feature you rely on.

Migrating

The OpenTofu migration docs describe it as designed to be safe and reversible, in four steps: back up your state and code, install OpenTofu, initialise and verify, test with a small change. Concretely:

  1. Back up. For remote state, use your backend’s mechanism, such as S3 versioning. The docs warn that tofu apply can update the state file format even when there are no infrastructure changes. So the first apply is the point of no easy return.
  2. Run tofu init in a copy of your configuration and tofu plan. A clean migration shows no changes. The lock file will be rewritten; OpenTofu’s registry mirrors providers, so check that the providers you use resolve.
  3. If you use terraform_remote_state to read other states, the docs say it needs extra care and link to a guide.
  4. Move one low-risk configuration first, then CI (opentofu/setup-opentofu was v2.0.2 on 28 November 2026), then the rest. Turn on encryption only after that.

I did not find moving back documented, so do not treat migration as a casual trial.

Choosing

Terraform 1.17.2 and OpenTofu 1.13.10 were current on my check date. Pick OpenTofu if you want state encryption, an open-source governance model, or its extra language features. Pick Terraform if you use HCP Terraform, depend on its newer features such as Policy or Stacks, or your organisation already contracts with HashiCorp, now part of IBM. Whichever you pick, read its release notes every quarter, as the upgrade lesson argued.

About the video

The embed is Ned in the Cloud’s “Encrypting State With OpenTofu”, 19 minutes 36 seconds, June 2024. Its description says OpenTofu introduced state and plan encryption in version 1.7, and covers why you should encrypt state and plan, how the feature works and how to encrypt existing state. It covers encryption, not migration or the wider differences; this lesson takes those from OpenTofu’s docs and changelogs. It predates later key provider additions and fixes, so check the current docs. I did not watch it fully. English was judged from the title, description, channel and its English auto-caption track. In deeper, Christian Lempa’s January 2025 video covers migrating from Terraform to OpenTofu.

Where this fits

That finishes the CI and teams group. The last group builds real infrastructure end to end, beginning with a VPC on AWS.

Check yourself

  1. 01How do you turn on encryption for an existing unencrypted OpenTofu state?

    Configure the new method with a fallback of unencrypted, then apply so the state is rewritten encrypted.

  2. 02What does enforced = true do?

    It stops OpenTofu from writing unencrypted state or plan data.

  3. 03Why is the first tofu apply after migrating a decision point?

    It can update the state file format even with no infrastructure changes, so back up state before it.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark