iac.htora.dev · security templates

Home/Refreshers/A safe place to keep Terraform state: refresher

A safe place to keep Terraform state

The state file contains secrets in plain text. This stores it encrypted, versioned, and locked while a run is in progress.

Remember

  1. terraform plan or apply: on a laptop or in a pipeline.
  2. Takes the lock: a second run waits or stops.
  3. Reads and writes state: encrypted, every version kept.
  4. Releases the lock: the next run can start.
PlatformUsesThe part that catches people
AWSAn S3 bucket with versioning, your own key, and native lockingGuides written before Terraform 1.10 tell you to create a DynamoDB table for locking.
AzureA storage account using Entra ID sign-in and blob leases for lockingOwner on a subscription lets you manage a storage account and still gives no access to the blobs inside it.
Google CloudA Cloud Storage bucket with versioning, which the gcs backend locks on its ownThe gcs backend locks by writing a small lock file beside the state.
Oracle CloudAn Object Storage bucket reached through the S3-compatible APITwo different credentials are involved.

Run the quick start

Download providersterraform init
Previewterraform plan
Applyterraform apply
Print the block to paste elsewhereterraform output backend_block

Watch out

  • If a run dies halfway, the lock can stay behind.
  • Moving an existing folder onto this backend is one command.
  • The key or prefix in each backend block is the file's name in the bucket.
Check it worked

Start terraform plan in two terminals against a folder that uses this backend. The second waits or stops because the state is locked.

Read the full pageDownload PDF

Registry 0.23.0. Built 2026-11-17.

Made by Habibullah Tora. Source on GitHub. Code under the MIT licence, writing under CC BY 4.0.