iac.htora.dev · security templates

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.

How you know 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.

How it works

1terraform plan or applyon a laptop or in a pipeline2Takes the locka second run waits or stops3Reads and writes stateencrypted, every version kept4Releases the lockthe next run can start
Step by step, from the thing that happens to the result you want.

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 startReadyone file, 135 lines
One file. Versioned, encrypted, private bucket, and an output that prints the backend block to paste elsewhere. Destroys cleanly.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. terraform init
    Downloads the provider. Run it once per folder.
  3. terraform plan
    Shows what will be created. Changes nothing. Read it.
  4. terraform apply
    Shows the plan again and waits for you to type yes.
  5. terraform output backend_block
    Prints the block to paste into your other folders.
  6. terraform destroy
    When you have finished. If it refuses, How to undo it below explains why.
Download main.tf
Checks

Formatting passedValid passedLint passed

Security scan: 24 checks passed, and five rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/state-backend/aws/t0
main.tf
Open main.tf as plain text
StandardReadyfour files, 241 lines
Adds your own KMS key, a policy refusing writes without it, expiry for replaced versions, and protection against being destroyed.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. terraform init
    Downloads the provider. Run it once per folder.
  3. terraform plan
    Shows what will be created. Changes nothing. Read it.
  4. terraform apply
    Shows the plan again and waits for you to type yes.
  5. terraform output backend_block
    Prints the block to paste into your other folders.
Download all four files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 39 checks passed, and six rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/state-backend/aws/t1
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text
HardenedReadysix files, 214 lines
Standard as a module. Plan rules refuse the bucket or key deleted, force_destroy, versioning off, public access allowed, encryption other than the KMS key, key rotation off, ACLs back on. A daily plan reports drift, and the scan fails on any unexplained finding.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. terraform init
    Downloads the provider. Run it once per folder.
  3. terraform plan
    Shows what will be created. Changes nothing. Read it.
  4. terraform apply
    Shows the plan again and waits for you to type yes.
  5. terraform output backend_block
    Prints the block to paste into your other folders.
Download all six files as a zip
Checks

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.

outcomes/state-backend/aws/t2
.checkov.yml
Open .checkov.yml as plain text
guard.yml
Open guard.yml as plain text
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text
What catches people

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.

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.

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.