AWS Backend Setup
Configure S3 bucket and DynamoDB table for Terraform remote state management with encryption, versioning, and state locking.Overview
Terraform backend stores infrastructure state in S3 with DynamoDB for state locking, preventing concurrent modifications.Benefits
- Team collaboration: Shared state across team members
- State locking: Prevents concurrent modifications
- Version history: S3 versioning for rollback capability
- Encryption: KMS encryption for sensitive data
- Audit trail: Access logging for compliance
Architecture
Quick Setup
1
Navigate to backend setup
2
Create terraform.tfvars
3
Initialize and deploy
- S3 bucket:
mcp-langgraph-terraform-state-prod - DynamoDB table:
mcp-langgraph-terraform-lock-prod - KMS key for encryption
- S3 bucket for access logs
4
Note the outputs
state_bucket_namelock_table_name
Backend Configuration
For Environment Deployments
Initialize Backend
Features
S3 State Bucket
- Versioning
- Encryption
- Access Logging
- Lifecycle
Enabled for rollback capability
DynamoDB Lock Table
- State Locking
- On-Demand Billing
Prevents concurrent Terraform runs
Multiple Environments
Separate State Per Environment
Same Bucket, Different Keys
Security Best Practices
Bucket Policy
Bucket Policy
Restrict access to authorized users only
DynamoDB Permissions
DynamoDB Permissions
Minimal permissions for state locking
MFA Delete
MFA Delete
Require MFA to delete state versions
Disaster Recovery
State Backup
1
Manual backup
2
Cross-region replication
State Recovery
1
List versions
2
Restore specific version
3
Verify state
Troubleshooting
Error: Failed to get existing workspaces
Error: Failed to get existing workspaces
Cause: S3 bucket doesn’t exist or no accessSolution:
Error: Error acquiring the state lock
Error: Error acquiring the state lock
Cause: Previous Terraform run didn’t release lock (e.g., Ctrl+C during apply)Solution:
Error: NoSuchBucket: The specified bucket does not exist
Error: NoSuchBucket: The specified bucket does not exist
Cause: Backend not created yetSolution:
Cost
| Component | Monthly Cost | Notes |
|---|---|---|
| S3 Storage | ~$0.02 | ~1 MB state file |
| S3 Requests | ~$0.01 | Minimal GET/PUT |
| DynamoDB | ~$0.01 | Pay-per-request |
| KMS | $1.00 | First 20K requests/month free |
| Total | ~$1.04/month | Negligible cost |