- Centralized state for collaboration and CI/CD.
- Durable storage with S3 versioning for rollbacks.
- Optional DynamoDB locking to prevent concurrent updates.
- Server-side encryption and KMS allow fine-grained key management and auditing.
- An AWS account with permissions to create S3 buckets, DynamoDB tables, and KMS keys (or reuse existing keys).
- Terraform installed locally.
- AWS credentials configured (environment variables, AWS CLI profile, or assume-role).
- Create an S3 bucket to hold Terraform state. Choose a globally unique bucket name and the same AWS region you plan to use for Terraform operations (this example uses
us-east-2). - Keep Block Public Access enabled (recommended).
- Enable bucket versioning so each apply creates a new state file version you can restore if needed.
- Configure default server-side encryption using an AWS KMS key for stronger control and auditing.



backend.tf or included in terraform.tf). The backend block tells Terraform where to persist state. Below is an example that uses an S3 bucket, enables encryption, and references a DynamoDB table for state locking.
- Create the DynamoDB table
terraform-state-lockahead of time with a primary key namedLockID(String). This table is used for state locking and preventing concurrentapplyoperations. - If you omit
dynamodb_table, Terraform will still store state in S3 but you lose server-side locking guarantees.
| Resource | Purpose | Example / Notes |
|---|---|---|
| S3 Bucket | Store terraform state objects | krausen-terraform-state-bucket (enable versioning & block public access) |
| DynamoDB Table | State locking | Table name: terraform-state-lock, primary key: LockID (String) |
| KMS Key | Server-side encryption for S3 | Use an existing key or create a new one and set as bucket default |
Make sure your AWS credentials are available to Terraform before running
terraform init. You can authenticate using environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), AWS CLI profiles, or by assuming an IAM role. Choose the method best suited to your environment.terraform init. Terraform will detect the backend configuration, prompt you as necessary to migrate local state (if present), and download provider plugins.
Example output:
- Optionally run
terraform fmtto ensure consistent formatting:
- Apply the configuration (this example assumes the
prdkey in S3):
terraform.tfstate file locally — Terraform stores and manages state in the configured backend. Browse the S3 bucket and the specified prefix (e.g., prd/) to confirm the terraform.tfstate object exists. With bucket versioning enabled you can inspect and restore earlier state versions if required.
Final recommended backend block (for reference)
- Terraform Backends documentation: https://developer.hashicorp.com/terraform/language/settings/backends
- AWS S3: https://aws.amazon.com/s3/
- AWS DynamoDB: https://aws.amazon.com/dynamodb/
- AWS KMS: https://aws.amazon.com/kms/