AWS for DevOps — Cost Optimization Exercises

4 minute read

14.6 — Practice Exercises: Cost Optimization

Test your understanding of pricing models, budgets, reserved instances, spot, tagging, and right-sizing.


Exercise 1 — Analyze Your Costs

Use Cost Explorer to answer: Which service costs the most? Which team spends the most? What’s the forecast for next month?

Show Answer
# Most expensive service
$ aws ce get-cost-and-usage \
    --time-period Start=2026-03-01,End=2026-03-31 \
    --granularity MONTHLY \
    --metrics BlendedCost \
    --group-by Type=DIMENSION,Key=SERVICE

# Spending by team (using tags)
$ aws ce get-cost-and-usage \
    --time-period Start=2026-03-01,End=2026-03-31 \
    --granularity MONTHLY \
    --metrics BlendedCost \
    --group-by Type=TAG,Key=Team

# Next month's forecast
$ aws ce get-cost-forecast \
    --time-period Start=2026-04-01,End=2026-04-30 \
    --metric BLENDED_COST \
    --granularity MONTHLY

Action items based on results:

  1. If EC2 is #1 → check for right-sizing opportunities
  2. If untagged costs are high → enforce tagging
  3. If forecast exceeds budget → identify recent changes causing the spike

Exercise 2 — Set Up Budget Alerts

Create three budgets: (1) total monthly budget of $1,000 with alerts at 80% and 100%, (2) EC2-specific budget of $500, (3) forecasted overspend alert.

Show Answer
# Budget 1: Total monthly $1000
$ aws budgets create-budget \
    --account-id 123456789012 \
    --budget '{
      "BudgetName": "total-monthly",
      "BudgetLimit": {"Amount": "1000", "Unit": "USD"},
      "TimeUnit": "MONTHLY",
      "BudgetType": "COST"
    }' \
    --notifications-with-subscribers '[
      {
        "Notification": {
          "NotificationType": "ACTUAL",
          "ComparisonOperator": "GREATER_THAN",
          "Threshold": 80,
          "ThresholdType": "PERCENTAGE"
        },
        "Subscribers": [{"SubscriptionType": "EMAIL", "Address": "devops@example.com"}]
      },
      {
        "Notification": {
          "NotificationType": "ACTUAL",
          "ComparisonOperator": "GREATER_THAN",
          "Threshold": 100,
          "ThresholdType": "PERCENTAGE"
        },
        "Subscribers": [{"SubscriptionType": "EMAIL", "Address": "devops@example.com"}]
      }
    ]'

# Budget 2: EC2-specific $500
$ aws budgets create-budget \
    --account-id 123456789012 \
    --budget '{
      "BudgetName": "ec2-monthly",
      "BudgetLimit": {"Amount": "500", "Unit": "USD"},
      "TimeUnit": "MONTHLY",
      "BudgetType": "COST",
      "CostFilters": {"Service": ["Amazon Elastic Compute Cloud - Compute"]}
    }' \
    --notifications-with-subscribers '[
      {
        "Notification": {
          "NotificationType": "ACTUAL",
          "ComparisonOperator": "GREATER_THAN",
          "Threshold": 80,
          "ThresholdType": "PERCENTAGE"
        },
        "Subscribers": [{"SubscriptionType": "EMAIL", "Address": "devops@example.com"}]
      }
    ]'

# Budget 3: Forecasted overspend
$ aws budgets create-budget \
    --account-id 123456789012 \
    --budget '{
      "BudgetName": "forecast-alert",
      "BudgetLimit": {"Amount": "1000", "Unit": "USD"},
      "TimeUnit": "MONTHLY",
      "BudgetType": "COST"
    }' \
    --notifications-with-subscribers '[
      {
        "Notification": {
          "NotificationType": "FORECASTED",
          "ComparisonOperator": "GREATER_THAN",
          "Threshold": 100,
          "ThresholdType": "PERCENTAGE"
        },
        "Subscribers": [{"SubscriptionType": "EMAIL", "Address": "devops@example.com"}]
      }
    ]'

Exercise 3 — Design a Spot Strategy

Your CI/CD system runs 50 builds per day, each taking 10 minutes on an m5.large. Design a cost-effective strategy using Spot Instances.

Show Answer

Current cost (On-Demand):

  • 50 builds × 10 min = 500 minutes/day = 8.3 hours/day
  • m5.large On-Demand: ~$0.096/hr
  • Daily: 8.3 × $0.096 = $0.80/day = ~$24/month

Spot strategy:

  • Use Spot with multiple instance types for resilience
  • Spot price: ~$0.03/hr (70% savings)
  • Monthly: ~$7.50
# Auto Scaling Group for CI/CD agents
$ aws autoscaling create-auto-scaling-group \
    --auto-scaling-group-name cicd-agents \
    --mixed-instances-policy '{
      "LaunchTemplate": {
        "LaunchTemplateSpecification": {
          "LaunchTemplateId": "lt-cicd-agent",
          "Version": "$Latest"
        },
        "Overrides": [
          {"InstanceType": "m5.large"},
          {"InstanceType": "m5a.large"},
          {"InstanceType": "m5d.large"},
          {"InstanceType": "m4.large"},
          {"InstanceType": "c5.large"},
          {"InstanceType": "c5a.large"}
        ]
      },
      "InstancesDistribution": {
        "OnDemandBaseCapacity": 0,
        "OnDemandPercentageAboveBaseCapacity": 0,
        "SpotAllocationStrategy": "capacity-optimized"
      }
    }' \
    --min-size 0 \
    --max-size 10 \
    --desired-capacity 0

Key decisions:

  • OnDemandBaseCapacity: 0 — 100% Spot (acceptable for CI/CD)
  • 6 instance types across families for availability
  • capacity-optimized to minimize interruptions
  • Scale to 0 when no builds are running

Exercise 4 — Right-Sizing Audit

You have 10 EC2 instances. Five have average CPU below 10%. Three have average CPU below 30%. Two are above 60%. Create a right-sizing plan.

Show Answer
Instance Current Size Avg CPU Action New Size Savings
i-001 m5.2xlarge 5% Downsize 2 sizes m5.large 75%
i-002 m5.2xlarge 8% Downsize 2 sizes m5.large 75%
i-003 m5.xlarge 7% Downsize 2 sizes m5.small 75%
i-004 m5.xlarge 9% Downsize 2 sizes m5.small 75%
i-005 m5.xlarge 4% Consider terminating — 100%
i-006 m5.xlarge 22% Downsize 1 size m5.large 50%
i-007 m5.xlarge 28% Downsize 1 size m5.large 50%
i-008 m5.large 25% Downsize 1 size m5.medium 50%
i-009 m5.xlarge 65% Keep m5.xlarge 0%
i-010 m5.large 72% Keep or upsize m5.large 0%

Process:

  1. Start with the lowest-CPU instances (biggest savings)
  2. Downsize one at a time, monitor for 1 week
  3. Check memory and network usage too (not just CPU)
  4. Never right-size all at once — do it in batches

Exercise 5 — Create a Cost Optimization Plan

Design a comprehensive cost optimization plan for a company spending $10,000/month on AWS. Break it down by strategy and estimate savings.

Show Answer
Strategy Target Estimated Savings
Turn off unused resources Unattached EBS, idle instances, unused EIPs $500 (5%)
Schedule dev/staging Run 10h/day × 5 days instead of 24/7 $1,000 (10%)
Right-size instances Downsize over-provisioned instances $1,500 (15%)
Savings Plans (1 yr) Commit for production workloads $1,500 (15%)
Spot for CI/CD + batch Use Spot for non-critical workloads $500 (5%)
S3 Intelligent-Tiering Auto-tier infrequently accessed data $200 (2%)
NAT Gateway optimization Consolidate NAT Gateways, use VPC endpoints $300 (3%)
Total estimated savings   $5,500 (55%)

Implementation timeline:

  • Week 1: Enable tagging + Cost Explorer + Budget alerts (visibility)
  • Week 2: Terminate unused resources (quick wins)
  • Week 3: Schedule dev/staging environments
  • Week 4: Start right-sizing (batch 1)
  • Month 2: Purchase Savings Plans based on 30 days of data
  • Month 2: Implement Spot for CI/CD
  • Ongoing: Monthly cost review meeting

Next Chapter → Practice Projects

Updated: