This document describes the automated environment provisioning system for Brain-Storm.
The environment provisioning system automatically creates, monitors, and cleans up test environments using Terraform and AWS resources.
- Infrastructure Templates: Reusable Terraform modules for consistent environment creation
- Automated Creation: One-command environment provisioning
- Monitoring: CloudWatch metrics and alarms for resource health
- Cost Tracking: Automatic cost categorization and tracking
- Cleanup: TTL-based automatic resource cleanup
┌─────────────────────────────────────────────────────────────┐
│ Environment Provisioning │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ Terraform │ │ AWS Resources │ │
│ │ Modules │──│ (EC2, RDS, S3) │ │
│ └──────────────────┘ └──────────────────┘ │
│ │ │ │
│ └──────────┬───────────┘ │
│ │ │
│ ┌──────────▼──────────┐ │
│ │ CloudWatch │ │
│ │ Monitoring │ │
│ └──────────┬──────────┘ │
│ │ │
│ ┌──────────▼──────────┐ │
│ │ Cleanup Script │ │
│ │ (TTL-based) │ │
│ └────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
# Using Terraform
cd infra/terraform
terraform apply -var-file=environments/dev.tfvars
# Or using the provisioning script
./scripts/provision-environment.sh dev# View CloudWatch metrics
aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-xxxxx \
--start-time 2024-01-01T00:00:00Z \
--end-time 2024-01-02T00:00:00Z \
--period 300 \
--statistics Average# Dry run (preview what will be deleted)
./scripts/environment-cleanup.sh dev 24 true
# Actual cleanup
./scripts/environment-cleanup.sh dev 24 false# Terraform variables
export TF_VAR_environment="dev"
export TF_VAR_aws_region="us-east-1"
export TF_VAR_instance_type="t3.medium"
export TF_VAR_enable_monitoring="true"
export TF_VAR_enable_cost_tracking="true"
export TF_VAR_auto_cleanup_enabled="true"
export TF_VAR_cleanup_ttl_hours="24"Create infra/terraform/environments/dev.tfvars:
environment = "dev"
aws_region = "us-east-1"
instance_type = "t3.medium"
enable_monitoring = true
enable_cost_tracking = true
auto_cleanup_enabled = true
cleanup_ttl_hours = 24The system automatically creates alarms for:
- CPU Utilization: Alert when > 80%
- Disk Space: Alert when > 85%
- Memory Usage: Alert when > 90%
Resources are tagged with:
Environment: Environment nameCostCenter: engineeringCreatedAt: Creation timestampAutoCleanup: Cleanup eligibility
View costs:
aws ce get-cost-and-usage \
--time-period Start=2024-01-01,End=2024-01-31 \
--granularity MONTHLY \
--metrics "UnblendedCost" \
--group-by Type=DIMENSION,Key=TAGResources are automatically cleaned up based on:
- TTL (Time To Live): Default 24 hours
- AutoCleanup Tag: Must be set to
true - Environment Tag: Must match the target environment
- Query resources with matching tags
- Check creation time against TTL
- Terminate/delete resources older than TTL
- Log all cleanup actions
- Always use dry-run first: Test cleanup before executing
- Set appropriate TTL: Balance between cost and availability
- Monitor costs: Review CloudWatch cost metrics regularly
- Tag resources: Ensure all resources have proper tags
- Document changes: Keep infrastructure-as-code updated
# Check resource tags
aws ec2 describe-instances \
--filters "Name=tag:Environment,Values=dev" \
--query "Reservations[].Instances[].Tags"
# Check creation time
aws ec2 describe-instances \
--instance-ids i-xxxxx \
--query "Reservations[].Instances[].LaunchTime"# Verify CloudWatch agent
aws cloudwatch list-metrics \
--namespace AWS/EC2 \
--dimensions Name=InstanceId,Value=i-xxxxx
# Check alarm status
aws cloudwatch describe-alarms \
--alarm-names brain-storm-dev-cpu