Home/Templates/A safe place to keep Terraform state
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.
Terraform
Why bother
Terraform writes the values it handled into the state file in plain text, passwords and keys included. A state file anyone can read is a short path from minor access to full control. It also needs a lock, or two people running at once overwrite each other and leave the file describing things that do not exist. Build this first. Every Standard template on this site has a commented backend block waiting for it.
Start terraform plan in two terminals against a folder that uses this backend. The second waits or stops because the state is locked.
How it works
Set it up
Pick your platform, then open the level you want. Each level is complete on its own. Read the comments in the files as you go. Anything with a real consequence is explained on the line where it happens.
Terraform. An S3 bucket with versioning, your own key, and native locking
What you need first
- Terraform 1.10 or later, for the lock file that replaces the old DynamoDB table.
- An account where you can create S3 buckets and KMS keys, and aws sts get-caller-identity showing the account you expect.
What it creates
- A bucket with versioning, encryption, public access blocked, and ACLs off
- A policy refusing plain HTTP
- A KMS key, a rule that every write must use it, and expiry for replaced versions after 90 days (Standard)
The code
Quick startReady
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: 24 checks passed, and five rules flagged, each with a written reason:
- S3 lifecycle configuration
- S3 event notifications
- S3 cross-region replication
- S3 encrypted with KMS
- S3 server access logging
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open main.tf as plain text
StandardReady
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.
Formatting passedValid passedLint passed
Security scan: 39 checks passed, and six rules flagged, each with a written reason:
- S3 event notifications
- IAM permissions management without constraints
- IAM write access without constraints
- S3 cross-region replication
- S3 server access logging
- IAM resource of *
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.
Formatting passedValid passedLint passed
Security scan: 36 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
Guides written before Terraform 1.10 tell you to create a DynamoDB table for locking. That is no longer needed. use_lockfile keeps a small lock object beside the state, so there is nothing else to build or pay for.
Everyone who runs Terraform against the Standard bucket needs kms:Decrypt and kms:GenerateDataKey on its key as well as S3 access. Missing either one fails with access denied on init, which looks like a bucket problem and is a key problem.
How to undo it
The quick start has force_destroy on, so terraform destroy removes the bucket and every state file in it. The Standard bucket has prevent_destroy set, so a destroy that reaches it stops with an error. Removing that line is a deliberate edit that shows up in review.
What it costs
Pennies for storage. About a dollar a month for the KMS key in the Standard version.
Terraform. A storage account using Entra ID sign-in and blob leases for locking
What you need first
- az login, and Owner or User Access Administrator on the subscription so the template can grant the data role.
- For the Standard version, your public IP in allowed_ip_ranges, because the account refuses everything else.
What it creates
- A storage account with versioning, key access off, and recoverable deletes
- A tfstate container, and the Storage Blob Data Contributor role for whoever applies it
- Your own key from Key Vault, a default-deny network rule, named writers, and a lock refusing deletion (Standard)
The code
Quick startReady
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: 11 checks passed, and six rules flagged, each with a written reason:
- Storage encrypted with your own key
- Classic blob read logging
- Storage private endpoint
- Storage replication
- Classic queue logging
- Storage public network access off
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.0.
Open main.tf as plain text
StandardReady
subscription_id. Subscription the state account goes in. The provider has required it since version 4.
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.# terraform.tfvars subscription_id = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.
Formatting passedValid passedLint passed
Security scan: 18 checks passed, and eight rules flagged, each with a written reason:
- Classic blob read logging
- Key Vault private endpoint
- Storage private endpoint
- Key backed by an HSM
- Key Vault public network access off
- Classic queue logging
- Key expiration date
- Storage public network access off
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.0.
Open keys.tf as plain text
Open logging.tf as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
subscription_id. Subscription the state account goes in. The provider has required it since version 4.
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.# terraform.tfvars subscription_id = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.
Formatting passedValid passedLint passed
Security scan: 18 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
Owner on a subscription lets you manage a storage account and still gives no access to the blobs inside it. Reading and writing state needs Storage Blob Data Contributor. The templates grant it to whoever applies them. It can take a minute to take effect, so a 403 on the first init usually clears on the second.
The Standard account denies all network access by default, and that includes your laptop. Add your address to allowed_ip_ranges or run Terraform from inside the network.
How to undo it
terraform destroy removes the quick start completely. The Standard account has a CanNotDelete management lock and prevent_destroy. Removing it means deleting the lock first, on purpose, and then the line.
What it costs
Pennies for storage, plus a small monthly charge for the Key Vault key in the Standard version.
Terraform. A Cloud Storage bucket with versioning, which the gcs backend locks on its own
What you need first
- gcloud auth application-default login, and a project where you can create buckets.
- For the Standard version, permission to switch on the Cloud KMS API.
What it creates
- A bucket with versioning, uniform access, and public access prevention
- Your own key from Cloud KMS, a cap on old versions, recoverable deletes, and named writers (Standard)
The code
Quick startReady
project_id = "your-project-id"main.tf, line 22
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: three checks passed, and one rule flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open main.tf as plain text
StandardReady
project_id. Project that contains the state bucket and its key.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.
Formatting passedValid passedLint passed
Security scan: eight checks passed, and one rule flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open keys.tf as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
project_id. Project that contains the state bucket and its key.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.
Formatting passedValid passedLint passed
Security scan: eight checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
The gcs backend locks by writing a small lock file beside the state. Nothing extra is needed, which is the good news. The catch is that anyone who can write the bucket can also delete that lock file, so keep the writer list short.
How to undo it
terraform destroy removes the quick start completely. The Standard bucket has prevent_destroy set. Its key ring and key can never be deleted from the project, only their key versions.
What it costs
Pennies for storage, plus a small monthly charge per Cloud KMS key version in the Standard version.
Terraform. An Object Storage bucket reached through the S3-compatible API
What you need first
- The OCI command line tool set up, so ~/.oci/config works. Terraform reads it to create the bucket.
- A Customer Secret Key for your user, created in the console. The s3 backend signs in with it, exported as AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
- Terraform 1.10 or later.
What it creates
- A versioned, private bucket
- Your own HSM-protected key, expiry for replaced versions, and the policy that lets Object Storage do both (Standard)
The code
Quick startReady
tenancy_ocid = "ocid1.tenancy.oc1..change-me"main.tf, line 21region = "us-ashburn-1"main.tf, line 22
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: three checks passed, and two rules flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
StandardReady
tenancy_ocid. The tenancy. The IAM policy is created here.compartment_ocid. Compartment that contains the bucket and vault.region. Region identifier, such as us-ashburn-1.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" compartment_ocid = "" region = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.
Formatting passedValid passedLint passed
Security scan: four checks passed, and one rule flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open keys.tf as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
tenancy_ocid. The tenancy. The IAM policy is created here.compartment_ocid. Compartment that contains the bucket and vault.region. Region identifier, such as us-ashburn-1.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" compartment_ocid = "" region = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output backend_block
Prints the block to paste into your other folders.
Formatting passedValid passedLint passed
Security scan: four checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
Two different credentials are involved. Terraform creates the bucket with your API key from ~/.oci/config. The backend then reads and writes state with a Customer Secret Key, because it speaks the S3 protocol. Having one and not the other is the usual reason the first init fails.
Locking through the S3-compatible API depends on it honouring the conditional writes Terraform uses for its lock file. Test it with two plans before a team relies on it. If the second plan does not wait, treat the backend as unlocked and run Terraform from one pipeline only.
Lifecycle rules run as the Object Storage service, which needs its own policy. Without it the rule is accepted and old versions are never deleted. The Standard version adds the policy.
How to undo it
Empty the quick start bucket, then terraform destroy removes it. The Standard bucket has prevent_destroy set, and its vault and key only schedule deletion with a waiting period of at least seven days.
What it costs
Pennies for storage. Vault keys beyond your tenancy's free allowance are billed.
Good to know
- If a run dies halfway, the lock can stay behind. The next run then reports that the state is locked and shows a lock ID. Check that nobody else is running, then clear it with terraform force-unlock and that ID.
- Moving an existing folder onto this backend is one command. Add the backend block, then run terraform init -migrate-state, and Terraform copies the local state up and asks before it does.
- The key or prefix in each backend block is the file's name in the bucket. Give every folder its own, or two folders will share one state and each will try to delete the other's resources.