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
- terraform plan or apply: on a laptop or in a pipeline.
- Takes the lock: a second run waits or stops.
- Reads and writes state: encrypted, every version kept.
- Releases the lock: the next run can start.
| Platform | Uses | The part that catches people |
|---|---|---|
| AWS | An S3 bucket with versioning, your own key, and native locking | Guides written before Terraform 1.10 tell you to create a DynamoDB table for locking. |
| Azure | A storage account using Entra ID sign-in and blob leases for locking | Owner on a subscription lets you manage a storage account and still gives no access to the blobs inside it. |
| Google Cloud | A Cloud Storage bucket with versioning, which the gcs backend locks on its own | The gcs backend locks by writing a small lock file beside the state. |
| Oracle Cloud | An Object Storage bucket reached through the S3-compatible API | Two different credentials are involved. |
Run the quick start
| Download providers | terraform init |
| Preview | terraform plan |
| Apply | terraform apply |
| Print the block to paste elsewhere | terraform 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.