Files
fleet-terraform/docs/ULTRA-COST-OPTIMIZATION.md

12 KiB

🚀 ULTRA Cost Optimization - Fleet Terraform

💰 Three-Tier Cost Comparison

Configuration Monthly Cost Savings Best For
Original ~$600 - Production HA, 100+ devices
Optimized ~$200-220 65% Production, 10-50 devices
ULTRA ~$120-150 75-80% 10 devices, maximum savings

📊 ULTRA Configuration Breakdown

Monthly Cost Estimate: $120-150

Component Configuration Monthly Cost
Aurora Serverless v2 0.5-1.0 ACU (capped) $35-50
Aurora Storage ~30GB @ $0.10/GB $3
Aurora I/O Minimal $2-3
Aurora Backups 3 days, ~30GB $3
ElastiCache DISABLED (in-memory only) $0
ECS Fargate 0.25 vCPU, 1GB, 100% Spot $12-18
ALB Minimal LCU usage $16-18
NAT Gateway Single AZ $32-35
S3 Minimal storage $1-2
CloudWatch Logs 3-day retention $1-2
KMS DISABLED (AWS-managed keys) $0
Data Transfer Minimal $2-3
TOTAL $107-134/month

Realistic Target: $120-150/month (with buffer for spikes)


🎯 Additional Optimizations Applied

1. No Redis/ElastiCache (Saves $15-20/month)

  • Fleet supports running without Redis for small deployments
  • Uses in-memory caching instead
  • Set FLEET_REDIS_ADDRESS="" to disable
  • Tradeoff: Slightly slower query caching, acceptable for 10 devices

2. Aurora Capped at 1 ACU (Saves $20-30/month)

  • Max capacity limited to 1 ACU instead of 2
  • Still handles 10 devices easily
  • Prevents cost spikes during load
  • Tradeoff: May slow down during heavy queries (rare with 10 devices)

3. 100% Fargate Spot (Saves $10-15/month)

  • No on-demand instances at all
  • Maximum cost savings (70% discount)
  • Auto-recovery on interruptions
  • Tradeoff: More frequent interruptions (still <5% of time)

4. Minimal Task Size (Saves $15-20/month)

  • 0.25 vCPU (minimum Fargate allows)
  • 1GB memory (minimum Fargate allows)
  • Sufficient for 10 devices
  • Tradeoff: Slower response under heavy load

5. No KMS Encryption (Saves $3/month)

  • Uses AWS-managed encryption keys
  • Still encrypted at rest
  • No monthly KMS key charges
  • Tradeoff: Less control over encryption keys

6. 3-Day Log Retention (Saves $5-8/month)

  • Down from 7 days
  • Still enough for troubleshooting
  • Tradeoff: Less historical data

7. 3-Day Database Backups (Saves $2-3/month)

  • Down from 7 days
  • Still provides recovery capability
  • Tradeoff: Shorter recovery window

⚖️ Configuration Comparison

Feature Matrix

Feature Original Optimized ULTRA
Availability Zones 3 1 1
Aurora Provisioned (2 instances) Serverless (0.5-2 ACU) Serverless (0.5-1 ACU)
Database Replicas 1 0 0
Redis 2 nodes 1 node DISABLED
Fargate Spot % 50% 70% 100%
Task Size 0.5 vCPU, 4GB 0.5 vCPU, 2GB 0.25 vCPU, 1GB
KMS Encryption Yes Yes No (AWS-managed)
Log Retention 30 days 5-7 days 3 days
Backup Retention 7 days 7 days 3 days
Performance Insights Yes No No
Container Insights Yes No No
VPC Flow Logs Yes No No

🚨 ULTRA Tradeoffs & Risks

What You're Giving Up

  1. No Redis Cache

    • Impact: Slower query responses (still fast for 10 devices)
    • Mitigation: Fleet's in-memory cache handles most queries
  2. 100% Spot Instances

    • Impact: More frequent interruptions (~2-5% of time vs <1%)
    • Mitigation: Auto-recovery in 1-2 minutes
  3. Minimal Task Resources

    • Impact: Slower under heavy load
    • Mitigation: 10 devices won't generate heavy load
  4. No KMS Encryption

    • Impact: Less control over encryption keys
    • Mitigation: Still encrypted with AWS-managed keys
    • WARNING: May not meet compliance requirements
  5. Shorter Backup Windows

    • Impact: Only 3 days of backups vs 7
    • Mitigation: Still provides recovery capability
  6. Minimal Logging

    • Impact: Only 3 days of logs for troubleshooting
    • Mitigation: Sufficient for most issues

When NOT to Use ULTRA

Don't use ULTRA if:

  • You have compliance requirements for KMS encryption
  • You need >7 days of backup retention
  • You have >20 devices
  • You need guaranteed sub-second query response
  • You can't tolerate 2-5% Spot interruption rate
  • You need detailed historical logs

ULTRA is perfect if:

  • You have 10-20 devices max
  • Cost is the #1 priority
  • You can tolerate occasional 1-2 minute interruptions
  • You don't have strict compliance requirements
  • You're okay with AWS-managed encryption

📈 Performance Expectations

ULTRA vs Optimized Performance

Metric Optimized ULTRA Difference
Query Response <100ms <200ms Slightly slower
Page Load <500ms <800ms Noticeable but acceptable
Uptime 99.5% 98-99% More interruptions
Recovery Time 1-2 min 1-2 min Same
Cold Start 2-5 sec 3-7 sec Slightly slower

Real-World Impact for 10 Devices

  • Daily queries: ~1,000-5,000 (very light)
  • Peak concurrent: 2-5 queries
  • Database load: <5% of 1 ACU capacity
  • Memory usage: ~400-600MB (well under 1GB limit)

Bottom line: ULTRA configuration is MORE than sufficient for 10 devices.


🚀 Migration Path

From Original → ULTRA (One Step)

# Backup current state
terraform state pull > backup-original.json

# Deploy ULTRA
terraform plan \
  -var-file="ultra-low-cost.tfvars" \
  -var="certificate_arn=YOUR_CERT_ARN" \
  -out=ultra.tfplan

terraform apply ultra.tfplan

Expected downtime: 15-20 minutes

From Optimized → ULTRA (Incremental)

# Already on optimized configuration
terraform state pull > backup-optimized.json

# Deploy ULTRA
terraform plan \
  -var-file="ultra-low-cost.tfvars" \
  -var="certificate_arn=YOUR_CERT_ARN" \
  -out=ultra.tfplan

terraform apply ultra.tfplan

Expected downtime: 5-10 minutes (smaller changes)


🔍 Monitoring ULTRA Configuration

Critical Metrics to Watch

# 1. Aurora ACU usage (should stay 0.5-1.0)
aws cloudwatch get-metric-statistics \
  --namespace AWS/RDS \
  --metric-name ServerlessDatabaseCapacity \
  --dimensions Name=DBClusterIdentifier,Value=fleet \
  --start-time $(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%S) \
  --end-time $(date -u +%Y-%m-%dT%H:%M:%S) \
  --period 300 \
  --statistics Maximum

# 2. Fargate Spot interruptions
aws ecs describe-services \
  --cluster fleet \
  --services fleet \
  --query 'services[0].events[0:5]'

# 3. Task memory usage (should be <800MB)
aws cloudwatch get-metric-statistics \
  --namespace AWS/ECS \
  --metric-name MemoryUtilization \
  --dimensions Name=ServiceName,Value=fleet Name=ClusterName,Value=fleet \
  --start-time $(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%S) \
  --end-time $(date -u +%Y-%m-%dT%H:%M:%S) \
  --period 300 \
  --statistics Average
# Alert if Aurora exceeds 0.9 ACU (approaching limit)
aws cloudwatch put-metric-alarm \
  --alarm-name fleet-ultra-aurora-high-acu \
  --alarm-description "Aurora approaching 1 ACU limit" \
  --metric-name ServerlessDatabaseCapacity \
  --namespace AWS/RDS \
  --statistic Average \
  --period 300 \
  --threshold 0.9 \
  --comparison-operator GreaterThanThreshold \
  --dimensions Name=DBClusterIdentifier,Value=fleet

# Alert if task memory exceeds 80%
aws cloudwatch put-metric-alarm \
  --alarm-name fleet-ultra-memory-high \
  --alarm-description "Task memory usage high" \
  --metric-name MemoryUtilization \
  --namespace AWS/ECS \
  --statistic Average \
  --period 300 \
  --threshold 80 \
  --comparison-operator GreaterThanThreshold \
  --dimensions Name=ServiceName,Value=fleet Name=ClusterName,Value=fleet

# Alert if no tasks running
aws cloudwatch put-metric-alarm \
  --alarm-name fleet-ultra-no-tasks \
  --alarm-description "No ECS tasks running" \
  --metric-name RunningTaskCount \
  --namespace AWS/ECS \
  --statistic Average \
  --period 60 \
  --threshold 1 \
  --comparison-operator LessThanThreshold \
  --dimensions Name=ServiceName,Value=fleet Name=ClusterName,Value=fleet

🆘 Troubleshooting ULTRA

Issue: Aurora hitting 1 ACU limit

Symptoms: Slow queries, timeouts
Solution:

# Increase max capacity to 1.5 ACU (+$15/month)
rds_config = {
  serverless_max_capacity = 1.5
}

Issue: Frequent Spot interruptions

Symptoms: >5% interruption rate
Solution:

# Add 20% on-demand capacity (+$5/month)
fargate_capacity_providers = {
  FARGATE = {
    default_capacity_provider_strategy = { weight = 20 }
  }
  FARGATE_SPOT = {
    default_capacity_provider_strategy = { weight = 80 }
  }
}

Issue: Task memory exhaustion

Symptoms: Task restarts, OOM errors
Solution:

# Increase to 2GB task memory (+$10/month)
fleet_config = {
  task_mem = 2048
  mem      = 1536
}

Issue: Need Redis for performance

Symptoms: Slow query responses
Solution:

# Re-enable Redis with micro instance (+$12/month)
redis_config = {
  cluster_size = 1
  instance_type = "cache.t4g.micro"
}

# Remove Redis disable flag
fleet_config = {
  extra_environment_variables = {}  # Remove FLEET_REDIS_ADDRESS=""
}

📊 Cost Scaling Matrix

Devices Config Aurora ACU Redis Fargate Monthly Cost
10 ULTRA 0.5-1.0 None 0.25 vCPU, 100% Spot $120-150
20 ULTRA+ 0.5-1.5 micro 0.25 vCPU, 80% Spot $150-180
50 Optimized 0.5-2.0 micro 0.5 vCPU, 70% Spot $200-230
100 Optimized+ 1.0-4.0 small 0.5 vCPU, 50% Spot $280-320
200+ Standard 2.0-8.0 medium 1 vCPU, multi-task $400-500

ULTRA Deployment Checklist

Pre-Deployment

  • Confirm you have <20 devices
  • Verify no compliance requirements for KMS
  • Acceptable with 3-day backup retention
  • Okay with 98-99% uptime (vs 99.5%)
  • Backup current Terraform state

Deployment

  • Review ultra-low-cost.tfvars
  • Update certificate ARN
  • Run terraform plan and review
  • Schedule maintenance window (15-20 min)
  • Apply Terraform changes
  • Monitor deployment progress

Post-Deployment

  • Verify Fleet application accessible
  • Test osquery enrollment (1-2 devices)
  • Confirm Aurora ACU <1.0
  • Check task memory usage <80%
  • Set up CloudWatch alarms
  • Monitor for 48 hours
  • Check AWS Cost Explorer after 1 week

🎯 Final Recommendation

Choose ULTRA if:

You have 10-20 devices
Cost is the absolute priority
No strict compliance requirements
Can tolerate occasional interruptions
Want to save $450-480/month (75-80%)

Choose Optimized if:

You have 10-50 devices
Need KMS encryption for compliance
Want 7-day backups
Prefer more stability (99.5% uptime)
Want to save $380-400/month (65%)

Stick with Original if:

You have 100+ devices
Need multi-AZ high availability
Require enterprise SLAs
Have strict compliance requirements


💡 Pro Tips

  1. Start with ULTRA, scale up if needed - It's easier to add resources than remove them
  2. Monitor costs weekly - Use AWS Cost Explorer to track actual spend
  3. Set billing alerts - Get notified if costs exceed $150/month
  4. Review quarterly - As you add devices, reassess configuration
  5. Keep backups - Always maintain Terraform state backups

Ready to deploy ULTRA?
See deployment instructions in the next section.