Enterprise-grade Terraform infrastructure for deploying MCP (Model Context Protocol) servers on AWS ECS Fargate, following 2024-2025 best practices.
- Environment-Based Configuration: Simple terraform.tfvars files per environment
- Modular Design: Complete ECS Fargate infrastructure in reusable module
- State Management: S3 backend with DynamoDB locking and KMS encryption
- CI/CD Integration: GitHub Actions workflows with OIDC authentication
- Default MCP Server: Ships with mcp/duckduckgo Docker image
- Security: Secrets Manager integration and least-privilege IAM
# Copy and customize bootstrap configuration
cd bootstrap
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your unique bucket name
# Create S3 bucket and DynamoDB table
terraform init
terraform apply# Deploy to EU West 2 environment
cd modules/ecs-fargate
terraform init
terraform plan -var-file="../../environments/terraform-eu-west-2/terraform.tfvars"
terraform apply -var-file="../../environments/terraform-eu-west-2/terraform.tfvars"- Configure GitHub repository secrets:
AWS_ROLE_ARN: OIDC role for environments
- Set repository variables:
AWS_REGION: Target AWS region (default: eu-west-2)
terraform/
βββ environments/
β βββ template/ # Reference configuration
β β βββ terraform.tfvars # Template variables
β βββ terraform-eu-west-2/ # EU West 2 environment
β βββ terraform.tfvars # Environment-specific variables
βββ modules/
β βββ ecs-fargate/ # Complete ECS Fargate infrastructure
β βββ main.tf # Infrastructure resources
β βββ variables.tf # Input variables
β βββ outputs.tf # Output values
β βββ *.tf # All infrastructure components
βββ bootstrap/ # State management setup
β βββ terraform.tfvars.example # Bootstrap template
β βββ *.tf # S3 and DynamoDB resources
βββ .github/workflows/ # CI/CD pipelines
βββ docs/ # Documentation
The infrastructure comes with sensible defaults:
- Container: MCP DuckDuckGo server (mcp/duckduckgo:latest)
- Compute: 1024 CPU / 3072 MB memory
- Scaling: 2 desired tasks
- Region: EU West 2 (eu-west-2)
- Load Balancer: Application Load Balancer with health checks
Override defaults in your environment's terraform.tfvars:
# Custom container image
container_image = "your-custom-mcp-server:latest"
# Different resource allocation
cpu = 512
memory = 1024
desired_count = 1
# Enable domain with Route53
enable_domain = true
domain_name = "your-domain.com"
subdomain = "mcp"- Format Check: Terraform format validation
- Plan Generation: Plans for terraform-eu-west-2 environment
- Comment Integration: Plan results posted to PR
- Artifact Storage: Plan files saved for deployment
- Environment Deploy: Auto-deploy on main branch merge
- Manual Approval: GitHub environment protection rules
- Drift Detection: Daily monitoring with GitHub Issues
- State Management: Centralized S3 backend with locking
Automated daily checks detect infrastructure drift:
# Manual drift check
cd modules/ecs-fargate
terraform plan -var-file="../../environments/terraform-eu-west-2/terraform.tfvars" -detailed-exitcode# Check ECS service status (from modules/ecs-fargate)
aws ecs describe-services --cluster $(terraform output -raw ecs_cluster_name)
# View application logs
aws logs tail $(terraform output -raw cloudwatch_log_group_name) --follow
# Test application endpoint
curl $(terraform output -raw application_url)GitHub Actions use OIDC instead of long-lived credentials:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ vars.AWS_REGION || 'eu-west-2' }}Sensitive data stored in AWS Secrets Manager:
secrets = [
{
name = "DATABASE_URL"
valueFrom = "arn:aws:secretsmanager:region:account:secret:name"
}
]- Backend Setup: S3 state management configuration
- Import Strategy: Migrating existing AWS resources
- CLAUDE.md: AI assistant guidance and patterns
- Research: Best practices research and rationale
# Plan changes
cd modules/ecs-fargate
terraform plan -var-file="../../environments/terraform-eu-west-2/terraform.tfvars"
# Apply changes
terraform apply -var-file="../../environments/terraform-eu-west-2/terraform.tfvars"
# Import existing resources
terraform import aws_ecs_cluster.main production-cluster- Create new directory:
mkdir environments/terraform-us-east-1 - Copy template:
cp environments/template/terraform.tfvars environments/terraform-us-east-1/ - Edit
terraform.tfvarswith region-specific settings - Add environment to GitHub Actions matrix in
.github/workflows/
If migrating from the legacy regional_http/ structure:
- Review Import Strategy
- Use configuration-driven imports
- Validate with drift detection
- Test rollback procedures
- Terraform: >= 1.7.0
- AWS Provider: >= 5.0
- AWS CLI: Configured with appropriate permissions
- GitHub: Repository with Actions enabled
- Fork the repository
- Create a feature branch
- Make changes with proper testing
- Submit pull request with plan output
- Ensure CI/CD pipeline passes
This project is licensed under the MIT License.
- Issues: Use GitHub Issues for bug reports
- Discussions: Use GitHub Discussions for questions
- Documentation: Check
docs/directory for detailed guides