Multi-environment AWS infrastructure using Terragrunt (dev/test/prod) with remote state. It also includes:
- S3 remote backend
- DynamoDB state locking
- Environment inheritance
- Reusable modules
- Terragrunt folder hierarchy
- DRY Terragrunt configuration
- Remote backend (S3 + DynamoDB)
- Managing multiple AWS environments
terraform-terragrunt-aws-environments/
βββ live/
β βββ dev/
β β βββ s3/
β β βββ terragrunt.hcl
β βββ test/
β β βββ s3/
β β βββ terragrunt.hcl
β βββ prod/
β βββ s3/
β βββ terragrunt.hcl
βββ modules/
β βββ s3/
β βββ main.tf
β βββ variables.tf
β βββ outputs.tf
βββ README.md
βββ .gitignore
π Remote State & Locking
This setup uses:
S3 bucket: for storing Terraform state
DynamoDB table: for locking to prevent concurrent state writes
To create DynamoDB table manually (if required):
aws dynamodb create-table --table-name terraform-locks --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --billing-mode PAY_PER_REQUEST --region us-west-2
Confirm table creation
aws dynamodb list-tables --region us-west-2
You should see:
{
"TableNames": [
"terraform-locks"
]
}
Initialize Terragrunt:
cd live/dev/s3
terragrunt initApply changes:
terragrunt apply
Repeat for test and prod folders.
To remove all resources:
terragrunt destroy -auto-approve
aws s3 rb s3://<state-bucket> --force --region <region>
aws dynamodb delete-table --table-name <lock-table> --region <region>
# Destroy Terraform-managed resources
cd live/dev/s3
terragrunt destroy -auto-approve
cd ../test/s3
terragrunt destroy -auto-approve
cd ../prod/s3
terragrunt destroy -auto-approve
# Remove the S3 backend bucket
aws s3 rb s3://my-tf-state-bucket-martin-001 --force --region us-west-2
# Delete the DynamoDB state locking table
aws dynamodb delete-table --table-name terraform-locks --region us-west-2
Scalable: Add more environments/modules without duplicating code
Safe: Remote state + locking prevents race conditions
Reusable: Terraform modules can be shared or extended
Secure: State is encrypted and managed in a centralized place
Maintainable: Clear folder structure and separation of concerns