This directory contains all infrastructure definitions for PredictIQ using Terraform.
infrastructure/
├── terraform/
│ ├── main.tf # Main configuration
│ ├── variables.tf # Variable definitions with validation
│ ├── outputs.tf # Output definitions
│ ├── locals.tf # Common tags and locals
│ ├── bootstrap.sh # Bootstrap script for state backend
│ ├── backend-config.hcl # Default backend configuration
│ ├── environments/ # Environment-specific configurations
│ │ ├── README.md # Environment separation documentation
│ │ ├── dev.tfvars # Development environment variables
│ │ ├── staging/
│ │ │ ├── terraform.tfvars
│ │ │ └── backend.hcl
│ │ └── production/
│ │ ├── terraform.tfvars
│ │ └── backend.hcl
│ └── modules/ # Reusable modules
│ ├── vpc/
│ ├── rds/
│ ├── redis/
│ ├── ecs/
│ └── monitoring/
├── ROLLBACK.md # Rollback procedures
└── README.md # This file
- Terraform >= 1.5.0
- AWS CLI configured
- Appropriate AWS IAM permissions
- Access to Terraform state bucket
- GitHub repository secrets configured (see Deployment Process section)
The deployment workflow requires the following secrets to be configured in repository settings:
| Secret | Description | Example |
|---|---|---|
AWS_ROLE_DEV |
IAM role ARN for dev environment | arn:aws:iam::123456789:role/terraform-dev |
AWS_ROLE_STAGING |
IAM role ARN for staging environment | arn:aws:iam::123456789:role/terraform-staging |
AWS_ROLE_PROD |
IAM role ARN for production environment | arn:aws:iam::123456789:role/terraform-prod |
Note: The deploy workflow validates these secrets before attempting deployment. If any are missing, the workflow will fail with a clear error message.
Before initializing Terraform, you must create the S3 bucket and DynamoDB table for remote state management:
cd infrastructure/terraform
# Bootstrap for development environment
./bootstrap.sh us-east-1 dev
# Bootstrap for staging environment
./bootstrap.sh us-east-1 staging
# Bootstrap for production environment
./bootstrap.sh us-east-1 prodThe bootstrap script will:
- Create an S3 bucket for Terraform state
- Enable versioning and encryption on the bucket
- Block public access to the bucket
- Create a DynamoDB table for state locking
- Enable point-in-time recovery on the DynamoDB table
cd infrastructure/terraform
# Initialize with backend configuration
terraform init -backend-config=backend-config.hclNote: The backend-config.hcl file contains the S3 bucket and DynamoDB table names. Update this file if you used different names during bootstrap.
# For development environment
terraform plan -var-file="environments/dev.tfvars"
# For staging environment
terraform plan -var-file="environments/staging/terraform.tfvars"
# For production environment
terraform plan -var-file="environments/production/terraform.tfvars"# Apply changes (requires approval)
terraform apply -var-file="environments/production/terraform.tfvars"
# Auto-approve (use with caution)
terraform apply -auto-approve -var-file="environments/production/terraform.tfvars"PredictIQ uses separate Terraform state files and backends for each environment:
- Development: Local state (for testing only)
- Staging: Remote state in S3 with DynamoDB locking
- Production: Remote state in separate S3 bucket with DynamoDB locking
See environments/README.md for detailed environment management instructions.
- Single-node Redis
- Micro RDS instance
- 1 API task
- Minimal resources for testing
- Multi-node Redis (2 nodes)
- Small RDS instance
- 2 API tasks
- Production-like configuration
- Multi-node Redis (3 nodes)
- Medium RDS instance
- 3 API tasks
- High availability setup
- Creates isolated network environment
- Configurable CIDR blocks
- Public and private subnets
- NAT Gateway for private subnet egress
- PostgreSQL database
- Automated backups
- Multi-AZ deployment (prod)
- Encryption at rest
- ElastiCache replication group (Multi-AZ)
automatic_failover_enabled = trueandmulti_az_enabled = trueby default- Minimum 2 cache clusters to support failover
- Failover RTO: AWS promotes a read replica to primary in approximately 60–120 seconds. During this window, write operations will fail; read-only operations served by replicas remain available. Applications should implement retry logic with exponential backoff (recommended: 3 retries, starting at 200 ms) to handle the failover window gracefully.
- At-rest and in-transit encryption enabled
- Subnet group for VPC placement
- Fargate launch type
- Application Load Balancer
- Auto-scaling policies
- CloudWatch logging
- CloudWatch dashboards
- SNS alerts
- Log groups
- Metric alarms
Terraform state is stored in S3 with:
- Encryption enabled
- Versioning enabled
- DynamoDB table for state locking
- Restricted IAM access
- Create feature branch for infrastructure changes
- Update Terraform files
- Run
terraform planand review changes - Create pull request with plan output
- After approval, merge to main
- GitHub Actions automatically applies changes
Monitor infrastructure health:
# View Terraform state
terraform show
# Get outputs
terraform output
# Check AWS resources
aws ec2 describe-instances --filters "Name=tag:Project,Values=predictiq"
aws rds describe-db-instances
aws elasticache describe-cache-clusters# Force unlock (use with caution)
terraform force-unlock <LOCK_ID># Import existing resource
terraform import module.vpc.aws_vpc.main vpc-12345678
# Remove resource from state
terraform state rm module.vpc.aws_vpc.main# Refresh state
terraform refresh
# Validate configuration
terraform validate
# Format configuration
terraform fmt -recursive- Never commit sensitive values to git
- Use AWS Secrets Manager for secrets
- Enable MFA for AWS console access
- Restrict Terraform state bucket access
- Enable CloudTrail for audit logging
- Use IAM roles instead of access keys
- Implement least privilege access
- Use spot instances for non-critical workloads
- Right-size instances based on metrics
- Enable auto-scaling for variable workloads
- Use reserved instances for baseline capacity
- Monitor unused resources
For infrastructure issues:
- Check CloudWatch logs
- Review Terraform state
- Consult ROLLBACK.md for recovery procedures
- Contact infrastructure team