Skip to main content

Practical AWS Cost Optimization

Introduction​

AWS cost optimization is not a one-time event but a continuous process. This chapter presents practical and immediate techniques to reduce your AWS bills.

The 7 Optimization Levers​

┌─────────────────────────────────────────────────────────────────┐
│ 7 AWS OPTIMIZATION LEVERS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. RIGHTSIZING Adjust resource size │
│ 2. SCHEDULING Stop when not in use │
│ 3. RESERVED/SAVINGS Commitment for discounts │
│ 4. SPOT INSTANCES Use excess capacity │
│ 5. STORAGE TIERING Move to cheaper storage │
│ 6. DATA TRANSFER Optimize transfers │
│ 7. ARCHITECTURE Design for cost │
│ │
└─────────────────────────────────────────────────────────────────┘

1. Rightsizing​

Identify Oversized Resources​

┌─────────────────────────────────────────────────────────────────┐
│ RIGHTSIZING ANALYSIS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Current instance: m5.xlarge (4 vCPU, 16 GB RAM) │
│ Cost: $140/month │
│ │
│ Observed usage (30 days): │
│ ┌────────────────────────────────────────────┐ │
│ │ CPU ████░░░░░░░░░░░░░░░░ 20% average │ │
│ │ RAM ██████░░░░░░░░░░░░░░ 30% average │ │
│ └────────────────────────────────────────────┘ │
│ │
│ Recommendation: t3.medium (2 vCPU, 4 GB RAM) │
│ New cost: $30/month │
│ Savings: $110/month (78%) │
│ │
└─────────────────────────────────────────────────────────────────┘

Metrics to Monitor​

ResourceMetricOversizing threshold
EC2Average CPU< 40% over 14 days
EC2Memory< 40% over 14 days
RDSAverage CPU< 40% over 14 days
RDSConnections< 20% of max
LambdaMemory used< 50% allocated
Oversized (CPU < 40%)     → Reduce by one size
Well-sized (40-80%) → Keep
Undersized (> 80%) → Increase or Auto Scale

2. Scheduling​

Non-Production Environments​

┌─────────────────────────────────────────────────────────────────┐
│ NON-PROD SCHEDULING │
├─────────────────────────────────────────────────────────────────┤
│ │
│ DEV/TEST Environment: │
│ │
│ Office hours: 8am - 6pm (10h/day) │
│ Business days: Monday - Friday (5 days) │
│ │
│ ON hours : 10h × 5d = 50h/week │
│ OFF hours : 168h - 50h = 118h/week │
│ │
│ Potential savings: 70%! │
│ │
│ Implementation: │
│ • AWS Instance Scheduler │
│ • Lambda + CloudWatch Events │
│ • Tags: Schedule=office-hours │
│ │
└─────────────────────────────────────────────────────────────────┘

Simple Scheduling Script​

# Lambda to stop DEV instances in the evening
import boto3

def lambda_handler(event, context):
ec2 = boto3.client('ec2')

# Find instances with tag Environment=dev
instances = ec2.describe_instances(
Filters=[
{'Name': 'tag:Environment', 'Values': ['dev', 'test']},
{'Name': 'instance-state-name', 'Values': ['running']}
]
)

instance_ids = []
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
instance_ids.append(instance['InstanceId'])

if instance_ids:
ec2.stop_instances(InstanceIds=instance_ids)
print(f"Stopped {len(instance_ids)} instances")

3. Reserved Instances and Savings Plans​

Comparison of Options​

┌─────────────────────────────────────────────────────────────────┐
│ COMMITMENT OPTIONS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ON-DEMAND (No commitment) │
│ └── Full price, maximum flexibility │
│ Ideal for: unpredictable workloads │
│ │
│ RESERVED INSTANCES (1 or 3 years) │
│ └── Up to -72% vs On-Demand │
│ Tied to: instance type + region │
│ Ideal for: stable and predictable workloads │
│ │
│ SAVINGS PLANS (1 or 3 years) │
│ └── Up to -72% vs On-Demand │
│ More flexible than RI │
│ Compute SP: EC2, Fargate, Lambda │
│ EC2 SP: instance family in a region │
│ │
│ SPOT INSTANCES │
│ └── Up to -90% vs On-Demand │
│ Can be interrupted with 2 min notice │
│ Ideal for: batch, CI/CD, tolerant workloads │
│ │
└─────────────────────────────────────────────────────────────────┘

Coverage Strategy​

┌─────────────────────────────────────────────────────────────────┐
│ COVERAGE STRATEGY │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Total capacity needed: 100 units │
│ │
│ ┌────────────────────────────────────────────────────────────┐ │
│ │████████████████████████████████████████░░░░░░░░░░░░░░░░░░░│ │
│ │ Reserved/Savings (60%) │ On-Demand │ Spot │ │
│ │ -60% │ (20%) │ (20%) │ │
│ │ Stable baseline │ Variable │ -90% │ │
│ └────────────────────────────────────────────────────────────┘ │
│ │
│ Overall savings: ~50% │
│ │
└─────────────────────────────────────────────────────────────────┘

4. Storage Optimization​

S3 Storage Classes​

┌─────────────────────────────────────────────────────────────────┐
│ S3 STORAGE CLASSES │
├─────────────────────────────────────────────────────────────────┤
│ │
│ S3 STANDARD $0.023/GB Frequent access │
│ ↓ after 30 days │
│ S3 INFREQUENT ACCESS $0.0125/GB Access < 1x/month │
│ ↓ after 90 days │
│ S3 GLACIER IR $0.004/GB Archive, fast access │
│ ↓ after 180 days │
│ S3 GLACIER DA $0.00099/GB Long-term archive │
│ │
│ Savings: Up to 95% with lifecycle policies │
│ │
└─────────────────────────────────────────────────────────────────┘

EBS Optimization​

Common issues:
• Unattached volumes (orphans) → Delete
• Old snapshots → Lifecycle policy
• gp2 instead of gp3 → Migration (20% cheaper)
• Oversized volumes → Reduce

5. Lambda Optimization​

┌─────────────────────────────────────────────────────────────────┐
│ LAMBDA OPTIMIZATION │
├─────────────────────────────────────────────────────────────────┤
│ │
│ BEFORE: 1024 MB, 500ms average │
│ Cost: 1M invocations × 0.5s × 1GB = $8.33/month │
│ │
│ AFTER: 512 MB, 600ms average (slower CPU) │
│ Cost: 1M invocations × 0.6s × 0.5GB = $5.00/month │
│ │
│ Savings: 40% │
│ │
│ OR │
│ │
│ AFTER: 2048 MB, 200ms average (faster CPU) │
│ Cost: 1M invocations × 0.2s × 2GB = $6.67/month │
│ │
│ Savings: 20% + better performance │
│ │
│ → Test different configurations! │
│ │
└─────────────────────────────────────────────────────────────────┘

6. Quick Optimization Checklist​

Immediate Actions (Quick Wins)​

ActionPotential savingsEffort
Delete unused resources10-30%Low
Stop DEV instances at night/weekend70% on DEVLow
Delete orphaned EBS volumesVariableLow
Delete old snapshotsVariableLow
Migrate gp2 to gp320% on EBSMedium

Medium-Term Actions​

ActionPotential savingsEffort
Rightsizing EC2/RDS20-50%Medium
Reserved Instances/Savings Plans30-60%Medium
S3 Lifecycle Policies50-90% on storageMedium
Spot Instances for batch60-90%Medium

Long-Term Actions​

ActionPotential savingsEffort
Serverless architecture30-70%High
Optimized multi-regionVariableHigh
Containerization (Fargate)20-40%High

7. AWS Tools for Optimization​

┌─────────────────────────────────────────────────────────────────┐
│ AWS FINOPS TOOLS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ AWS COST EXPLORER │
│ └── Cost visualization, trends, forecasts │
│ │
│ AWS BUDGETS │
│ └── Alerts when budget exceeded │
│ │
│ AWS TRUSTED ADVISOR │
│ └── Automatic recommendations │
│ │
│ AWS COMPUTE OPTIMIZER │
│ └── Rightsizing EC2, Lambda, EBS │
│ │
│ AWS COST ANOMALY DETECTION │
│ └── Automatic anomaly detection │
│ │
│ SAVINGS PLANS RECOMMENDATIONS │
│ └── Commitment recommendations │
│ │
└─────────────────────────────────────────────────────────────────┘

Practical Exercise​

Scenario: Optimize this bill​

Current monthly bill: $5,000

Breakdown:
• EC2 (10 m5.xlarge instances 24/7) : $1,400
• RDS (db.r5.large Multi-AZ) : $400
• EBS (500 GB gp2) : $50
• S3 (10 TB Standard) : $230
• Data Transfer (500 GB) : $45
• Lambda (50M invocations) : $100
• Others : $2,775

Observations:
• 5 EC2 instances are for DEV/TEST
• Average EC2 PROD CPU: 35%
• 3 TB of S3 data is over 1 year old
• 100 GB EBS unattached
See recommendations

Recommended optimizations:

  1. Scheduling DEV/TEST: 5 instances × 70% OFF = -$490/month

  2. Rightsizing PROD: m5.xlarge → m5.large = -$350/month

  3. Reserved Instances PROD: 5 instances 1 year = -$280/month

  4. Orphaned EBS: Delete 100 GB = -$10/month

  5. gp2 → gp3: 500 GB = -$10/month

  6. S3 Lifecycle: 3 TB to Glacier = -$60/month

Total savings: ~$1,200/month (24%)

New bill: ~$3,800/month

Summary​

  • Rightsizing: Adjust resources to actual usage
  • Scheduling: Stop non-production resources
  • Commitment: Reserved Instances or Savings Plans for stable workloads
  • Spot: Use for workloads tolerant to interruptions
  • Storage: Lifecycle policies for S3, delete orphans
  • Monitoring: Use AWS tools (Cost Explorer, Trusted Advisor)

Next section: Validation Quiz