Chapter 16: AWS Cost & Performance Optimization Guide for Terraform
Cut your AWS bill by 40% and speed up Terraform pipelines. Technical guide to Spot Instances, Right-Sizing, and Terragrunt Caching.
The Friction
- ✕ Over-Provisioning: Paying for idle m5.2xlarge instances.
- ✕ Slow Builds: waiting 20 minutes for terraform plan.
- ✕ Network Latency: Traffic hairpinning through NAT Gateways.
The R8way Solution
- ✓ Right-Sizing: Performance tests determine exact needs.
- ✓ Caching: Terragrunt caching speeds up plan/apply.
- ✓ VPC Endpoints: Private links to AWS services reduce latency/cost.
16.1 Terraform Performance
Parallelism
Default Parallelism:
- Terraform defaults to 10 parallel operations
- Can be increased for faster applies
Configuration:
Best Practices:
- Increase parallelism for independent resources
- Reduce parallelism for dependent resources
- Monitor AWS API rate limits
State Size Optimization
Strategies:
- Separate components into modules
- Use isolated state files (already implemented)
- Exclude unnecessary data sources:
- Remove unused data sources
- Use specific filters
- Clean up old state:
- Remove deleted resources from state
- Compact state files
State Cleanup:
Module Caching
Terraform Cache:
- Modules cached in .terraform/
- Provider plugins cached
- Speeds up subsequent runs
Cache Location:
16.2 AWS Cost Optimization
Right-Sizing
VPC:
- Use single NAT Gateway for dev/test
- Use one-per-AZ for production
EKS:
- Use smaller instance types for dev (t3.medium)
- Use larger types for production (t3.large, m5.large)
- Use Spot instances for dev/test
RDS:
- Use db.t3.micro for dev
- Use db.t3.small or larger for production
- Enable auto-scaling for storage
Reserved Instances
EKS Nodes:
- Purchase Reserved Instances for predictable workloads
- 1-year or 3-year terms
- Up to 72% savings vs On-Demand
RDS:
- Purchase Reserved Instances for databases
- Match instance types to usage
- Consider Multi-AZ for production
Spot Instances
EKS:
Cost Savings:
- Up to 90% discount vs On-Demand
- Best for dev/test environments
- Use On-Demand for production
NAT Gateway Optimization
Cost:
- Single NAT: ~$32/month + data transfer
- Per-AZ NAT: ~$96/month + data transfer
Optimization:
Alternative:
- Use NAT Instances (cheaper but less reliable)
- Use VPC endpoints (eliminate NAT for AWS services)
16.3 Network Performance
VPC Endpoints
S3 Endpoint:
Benefits:
- No NAT Gateway charges
- Lower latency
- Private connectivity
Cost:
- Free for S3 endpoints
- $7.20/month per endpoint for other services
EKS Performance
Node Group Configuration:
- Use larger instance types (fewer nodes)
- Reduce cluster overhead
- Better resource utilization
Example:
16.4 Database Performance
RDS Optimization
Instance Types:
- Use db.t3.medium or larger for production
- Match instance size to workload
- Enable auto-scaling
Storage:
- Use GP3 (cheaper, faster)
- Enable auto-scaling
- Configure backup retention
Multi-AZ:
- Required for production (HA)
- 2x cost but 2x availability
- Automatic failover
16.5 Monitoring Performance
Cost Monitoring
AWS Cost Explorer:
- Track costs by resource
- Set budgets
- Create alerts
Cost Allocation Tags:
- All resources automatically tagged
- Track costs by environment, project
- Enable chargeback
Script:
Performance Monitoring
CloudWatch Metrics:
- EKS cluster metrics
- RDS performance insights
- VPC flow log analysis
Dashboards:
- Create CloudWatch dashboards
- Monitor key metrics
- Set up alarms
16.6 Cost Optimization Checklist
Initial Setup
- [ ] Use single NAT Gateway for dev
- [ ] Use smaller instance types for dev
- [ ] Use Spot instances for dev
- [ ] Enable auto-scaling
Ongoing Optimization
- [ ] Review instance sizes monthly
- [ ] Purchase Reserved Instances
- [ ] Use VPC endpoints where possible
- [ ] Clean up unused resources
- [ ] Review backup retention
Production Checklist
- [ ] Use per-AZ NAT for HA
- [ ] Use On-Demand instances
- [ ] Enable Multi-AZ for RDS
- [ ] Configure backup retention
- [ ] Monitor costs daily
16.7 Chapter Summary
Executive Summary
-
★Performance Tuning
- Terraform parallelism & caching
- VPC Endpoint optimization
- Database performance
-
★Cost Optimization
- Right-sizing instances
- Spot & Reserved Instances
- Monitoring & budgets