Terraformterraformstates3lockingproduction-ready

Remote State Management

Terraform remote state configuration with S3 backend, DynamoDB locking, cross-stack references, and state import patterns.


title: "Remote State Management" description: "Terraform remote state configuration with S3 backend, DynamoDB locking, cross-stack references, and state import patterns." category: "Terraform" language: "hcl" tags: ["terraform", "state", "s3", "locking", "production-ready"] createdAt: "2025-04-15" updatedAt: "2025-05-01"

Overview

Terraform remote state management using S3 with DynamoDB locking for team collaboration. Includes state bucket bootstrap, KMS encryption, cross-stack references for modular infrastructure, and resource import patterns.

Code

backend "s3" {
  bucket         = "terraform-state"
  key            = "env/component/terraform.tfstate"
  dynamodb_table = "terraform-locks"
  encrypt        = true
}

Notes

  • DynamoDB locking prevents concurrent state modifications (critical for teams)
  • S3 versioning enables state rollback if a bad apply corrupts state
  • KMS encryption protects sensitive values stored in state (database passwords, keys)
  • prevent_destroy lifecycle rule prevents accidental deletion of state infrastructure
  • Cross-stack references via terraform_remote_state enable modular, layered infrastructure
  • Key path convention: {environment}/{component}/terraform.tfstate for organized state