This Terraform configuration follows HashiCorp's recommended best practices for organizing infrastructure code. It creates ECS Fargate clusters for multiple environments using a proper directory structure with environment separation.
This project demonstrates a production-ready Terraform structure that includes:
- Environment Separation: Separate directories for each environment (dev, staging, prod)
- Module Organization: Reusable modules in a dedicated modules directory
- Shared Configuration: Common configurations and backend setup
- Proper State Management: Environment-specific state files with S3 backend
demo-infra-restructured/
├── README.md # This file
├── .gitignore # Git ignore rules
├── backend-setup/ # Backend infrastructure setup
│ ├── main.tf # S3 bucket and DynamoDB table
│ ├── variables.tf # Backend setup variables
│ ├── outputs.tf # Backend resource outputs
│ ├── terraform.tfvars.example # Example variables
│ └── README.md # Backend setup instructions
├── modules/ # Reusable modules
│ └── ecs-cluster/ # ECS cluster module
│ ├── main.tf # Module resources
│ ├── variables.tf # Module variables
│ ├── outputs.tf # Module outputs
│ ├── versions.tf # Provider requirements
│ └── README.md # Module documentation
├── environments/ # Environment-specific configurations
│ ├── dev/ # Development environment
│ │ ├── main.tf # Dev environment resources
│ │ ├── variables.tf # Dev environment variables
│ │ ├── outputs.tf # Dev environment outputs
│ │ ├── terraform.tfvars # Dev variable values
│ │ ├── backend.tf # Dev backend configuration
│ │ └── versions.tf # Dev provider requirements
│ ├── staging/ # Staging environment
│ │ ├── main.tf # Staging environment resources
│ │ ├── variables.tf # Staging environment variables
│ │ ├── outputs.tf # Staging environment outputs
│ │ ├── terraform.tfvars # Staging variable values
│ │ ├── backend.tf # Staging backend configuration
│ │ └── versions.tf # Staging provider requirements
│ └── prod/ # Production environment
│ ├── main.tf # Prod environment resources
│ ├── variables.tf # Prod environment variables
│ ├── outputs.tf # Prod environment outputs
│ ├── terraform.tfvars # Prod variable values
│ ├── backend.tf # Prod backend configuration
│ └── versions.tf # Prod provider requirements
├── scripts/ # Automation scripts
│ ├── deploy.sh # Deployment automation
│ ├── setup-backend.sh # Backend setup automation
│ └── cleanup.sh # Cleanup automation
└── docs/ # Additional documentation
├── DEPLOYMENT.md # Deployment guide
├── ARCHITECTURE.md # Architecture documentation
└── TROUBLESHOOTING.md # Troubleshooting guide
- Each environment has its own directory and state file
- Prevents accidental changes to production from development
- Allows environment-specific configurations
- Common infrastructure patterns are abstracted into modules
- Modules can be versioned and shared across projects
- Reduces code duplication and improves maintainability
- Backend setup is separate from application infrastructure
- Scripts are organized in their own directory
- Documentation is centralized and comprehensive
- Easy to add new environments
- Simple to add new modules
- Clear patterns for team collaboration
# Navigate to backend setup
cd backend-setup
# Copy and configure variables
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your values
# Deploy backend infrastructure
terraform init
terraform plan
terraform apply# Navigate to desired environment
cd environments/dev
# Initialize with backend
terraform init
# Plan and apply
terraform plan
terraform apply# Deploy using automation script
./scripts/deploy.sh dev plan
./scripts/deploy.sh dev apply
# Deploy to staging
./scripts/deploy.sh staging apply
# Deploy to production
./scripts/deploy.sh prod apply- Purpose: Development and testing
- Log Retention: 7 days (cost optimization)
- Fargate Spot: Enabled (cost savings)
- Monitoring: Basic CloudWatch logs
- Backup: Disabled
- Purpose: Pre-production testing
- Log Retention: 30 days
- Fargate Spot: Enabled (cost savings)
- Monitoring: Enhanced monitoring
- Backup: Enabled
- Purpose: Live production workloads
- Log Retention: 365 days (compliance)
- Fargate Spot: Disabled (reliability)
- Monitoring: Full monitoring and alerting
- Backup: Enabled with cross-region replication
The ECS cluster module can be used independently:
module "ecs_cluster" {
source = "../../modules/ecs-cluster"
cluster_name = "my-cluster"
environment = "dev"
# VPC Configuration
vpc_id = "vpc-12345678"
subnet_ids = ["subnet-12345678", "subnet-87654321"]
# Logging Configuration
log_retention_days = 7
# Tags
tags = {
Environment = "dev"
Project = "demo-app"
}
}- Each module follows HashiCorp's standard structure
- Proper variable descriptions and validation
- Comprehensive outputs for integration
- Directory-based environment separation
- Environment-specific variable files
- Isolated state management
- Logical grouping of resources
- Clear naming conventions
- Consistent file structure
- README files at every level
- Inline code documentation
- Architecture diagrams and guides
- Deployment scripts for consistency
- Backend setup automation
- Cleanup and maintenance scripts
- State File Security: S3 backend with encryption and versioning
- Access Control: IAM roles with least privilege
- Network Security: Private subnets and security groups
- Secrets Management: Integration with AWS Secrets Manager
- Environment-Specific Settings: Different configurations for cost vs. reliability
- Fargate Spot: Enabled in non-production environments
- Log Retention: Shorter retention in development environments
- Resource Tagging: Comprehensive tagging for cost allocation
- CloudWatch Logs: Centralized logging with configurable retention
- CloudWatch Metrics: ECS cluster and service metrics
- Alarms: Environment-specific alerting (production has more comprehensive monitoring)
- Add More Modules: Create modules for other infrastructure components
- CI/CD Integration: Integrate with your CI/CD pipeline
- Monitoring: Add comprehensive monitoring and alerting
- Security: Implement additional security controls
- Documentation: Expand documentation as the project grows
- Check the
docs/TROUBLESHOOTING.mdfor common issues - Review CloudWatch logs for application issues
- Use the deployment scripts for consistent operations
- Follow the architecture documentation for understanding the system
When adding new infrastructure:
- Follow the established directory structure
- Create modules for reusable components
- Add proper documentation
- Include examples and tests
- Update this README with any new patterns