💰 Cost Optimization#

Learning Objectives#

  • Understand AWS pricing models and reduce costs
  • Use Compute Optimizer, Cost Explorer, and Trusted Advisor
  • Implement cost allocation tags and budgets
  • Choose the right pricing model for different workloads

1. AWS Pricing Models#

1.1 Compute Pricing#

Option Discount Commitment Use Case
On-Demand None None Short-term, spiky, unknown
Reserved Instances Up to 72% 1 or 3 years Steady-state workloads
Savings Plans Up to 72% 1 or 3 years ($/hr) Flexible across services
Spot Instances Up to 90% None Fault-tolerant, batch
Dedicated Hosts Special 1 or 3 years (per host) BYOL, compliance

Reserved Instance Types:

Standard RI: Fixed instance type. Best discount. Can sell on marketplace.
Convertible RI: Change instance family. Lower discount. Flexible.
Scheduled RI: Run during specific times (e.g., 9-5 weekdays).

Savings Plans:

Compute SP: Covers EC2, Fargate, Lambda ($/hr commitment)
EC2 SP: Covers EC2 only. Best discount within family → region.
Machine Learning SP: Covers SageMaker.

1.2 Storage Pricing#

Service Pricing Model Cost Optimization
S3 Per GB/month + requests Lifecycle policies, Intelligent-Tiering
EBS Per GB/month provisioned gp3 (baseline IOPS), snapshots
EFS Per GB/month used Lifecycle to IA tier
RDS Instance + storage Reserved Instances, Aurora

1.3 Data Transfer Pricing#

Free:

  • Inbound data transfer (into AWS)
  • Data between AZs in same VPC (using private IP)
  • Data to CloudFront (origin)

Paid:

  • Outbound to internet ($0.09/GB for first 10 TB)
  • Cross-region data transfer
  • NAT Gateway ($0.045/hr + $0.045/GB processed)

2. Cost Optimization Strategies#

graph TD
    subgraph Strategies["Cost Optimization Framework"]
        direction LR
        
        subgraph RightSize["1. Right-Sizing"]
            RS1["Check CloudWatch\nCPU/Memory/Network"]
            RS2["Downsize if < 20% CPU\nUpsize if > 80% CPU"]
            RS3["Switch to T3\nfor variable loads"]
        end
        
        subgraph Purchase["2. Purchase Models"]
            PM1["On-Demand → Spot\n90% savings"]
            PM2["Add Reserved Instances\n72% savings"]
            PM3["Savings Plans\nFlexible $/hr commit"]
        end
        
        subgraph Storage["3. Storage Optimization"]
            SO1["S3 Lifecycle Policies\nStandard → IA → Glacier"]
            SO2["EBS gp3 vs io2\nDelete unused volumes"]
            SO3["Enable EFS IA\nfor infrequent access"]
        end
        
        subgraph Network["4. Network Optimization"]
            NO1["VPC Endpoints\ninstead of NAT"]
            NO2["Keep data in-same\nregion to avoid x-fer"]
            NO3["CloudFront for\nreduced data out"]
        end
    end

    RightSize -->|Next step| Purchase
    Purchase -->|Next step| Storage
    Storage -->|Next step| Network

    style RightSize fill:#01ab5c,color:#fff
    style Purchase fill:#527fff,color:#fff
    style Storage fill:#ff9900,color:#fff
    style Network fill:#888,color:#fff

2.1 Right-Sizing#

Match instance types to actual workload needs:

# Get EC2 utilization data
aws cloudwatch get-metric-statistics \
  --namespace AWS/EC2 \
  --metric-name CPUUtilization \
  --statistics Average \
  --period 86400 \
  --start-time 2024-01-01T00:00:00Z \
  --end-time 2024-01-31T00:00:00Z \
  --dimensions Name=InstanceId,Value=i-abc123

Right-Sizing Process:

  1. Check CPU, memory, network utilization (CloudWatch)
  2. Identify under-utilized instances (< 20% CPU = candidate for downsizing)
  3. Identify over-utilized instances (> 80% CPU = candidate for upsizing)
  4. Consider T3 burstable instances for variable workloads

2.2 Auto Scaling#

  • Scale out during high demand, scale in during low demand
  • Use target tracking policies (e.g., maintain 60% CPU)
  • Scheduled scaling for predictable patterns
  • Predictive scaling for ML-based forecasting

2.3 Storage Optimization#

# Set S3 lifecycle rule
aws s3api put-bucket-lifecycle-configuration \
  --bucket my-data-lake \
  --lifecycle-configuration '{"Rules": [{ "Id": "archive-old-data", "Status": "Enabled", "Filter": {"Prefix": "logs/"},
      "Transitions": [
        {"Days": 30, "StorageClass": "STANDARD_IA"},
        {"Days": 90, "StorageClass": "GLACIER"},
        {"Days": 365, "StorageClass": "DEEP_ARCHIVE"}
      ]
    }]
  }'

2.4 Cost Allocation Tags#

# Tag resources
aws ec2 create-tags \
  --resources i-abc123 \
  --tags Key=CostCenter,Value=DataAnalytics Key=Environment,Value=Production

# Activate tags for cost allocation (via Billing console)

Recommended Tags:

  • Environment (Production, Development, Testing)
  • CostCenter (Department, Project, Team)
  • Owner (Team or individual responsible)
  • Application (App name for grouping)

3. AWS Cost Management Tools#

3.1 AWS Cost Explorer#

Visualize and analyze costs:

  • Default views: Monthly costs by service, linked account, region
  • Custom reports: Filter by tags, API operations, instance type
  • Recommendations: Reserved Instance and Savings Plans recommendations

3.2 AWS Budgets#

# Create cost budget
aws budgets create-budget \
  --account-id 123456789012 \
  --budget '{"BudgetName": "monthly-infra-budget", "BudgetLimit": {"Amount": "10000", "Unit": "USD"},
    "TimePeriod": {"Start": "2024-01-01T00:00:00Z"},
    "TimeUnit": "MONTHLY",
    "BudgetType": "COST",
    "CostFilters": {"TagKeyValue": ["Environment$Production"]}
  }' \
  --notifications-with-subscribers '[
    {"Notification": { "ComparisonOperator": "GREATER_THAN", "Threshold": 80, "ThresholdType": "PERCENTAGE", "NotificationType": "ACTUAL" },
      "Subscribers": [{"Address": "team@example.com", "SubscriptionType": "EMAIL"}]
    },
    {"Notification": { "ComparisonOperator": "GREATER_THAN", "Threshold": 100, "ThresholdType": "PERCENTAGE", "NotificationType": "FORECASTED" },
      "Subscribers": [{"Address": "finance@example.com", "SubscriptionType": "EMAIL"}]
    }
  ]'

3.3 AWS Compute Optimizer#

Uses ML to recommend optimal AWS resources:

  • EC2 — Right-size based on utilization patterns
  • Auto Scaling Groups — Optimal instance type and size
  • EBS — Volume type (gp3 vs io2 vs st1)
  • Lambda — Memory size optimization
# Get EC2 recommendations
aws compute-optimizer get-ec2-instance-recommendations \
  --instance-arns arn:aws:ec2:us-east-1:...:instance/i-abc123

3.4 AWS Trusted Advisor#

Automated advisory for cost optimization, performance, security, fault tolerance:

Category Checks Example
Cost Optimization Idle resources, underutilized instances Stop idle RDS
Performance Over-provisioned instances Right-size EC2
Security MFA on root, open ports, S3 public access Close open SG ports
Fault Tolerance Multi-AZ, backups, ASG Enable RDS backup
Service Limits Usage vs limits Request limit increase

4. Real-World Cost Optimization#

Use Case 1: Web Application Cost Reduction#

Strategy Savings
Switch from On-Demand to Compute SP (1-year) ~30%
Right-size from m5.xlarge to m5.large 50%
Use Spot for CI/CD workers 70%
S3 lifecycle to IA/Glacier for old data 60%

Use Case 2: Data Lake Optimization#

Raw data → S3 Standard (30 days)
           → S3 Intelligent-Tiering (90 days)
           → S3 Glacier (1 year)
           → S3 Deep Archive (7 years)
Compress data (Parquet) for Athena queries → 80% less storage

5. ⚡ Exam Tips#

  1. Compute SP — Best for mixed workloads (EC2 + Fargate + Lambda)
  2. EC2 SP — Best for dedicated EC2 fleets
  3. Reserved Instances — Can be shared across accounts via AWS Organizations
  4. Spot Instances — Can’t be used for RDS, persistent storage, stateful apps
  5. Data Transfer — OUT is expensive, IN is free. Keep data in same region
  6. NAT Gateway — Costs $0.045/hr + data processing. Use VPC endpoints for S3/DynamoDB
  7. Trusted Advisor — Basic checks free, full checks with Business/Enterprise support

✅ Chapter Quiz#

  1. Which pricing option provides the highest discount for EC2?

    • A) On-Demand
    • B) Spot
    • C) Reserved (3-year, all upfront)
    • D) Savings Plans
  2. Which tool uses ML to recommend optimal AWS resource configurations?

    • A) Cost Explorer
    • B) Budgets
    • C) Compute Optimizer
    • D) Trusted Advisor
  3. What is the most cost-effective way to access S3 from a private subnet?

    • A) NAT Gateway
    • B) VPC Gateway Endpoint
    • C) Internet Gateway
    • D) VPN
  4. Which tag is commonly used to group costs by department?

    • A) Environment
    • B) Owner
    • C) CostCenter
    • D) Application
  5. Which Trusted Advisor category checks for idle resources?

    • A) Performance
    • B) Cost Optimization
    • C) Security
    • D) Fault Tolerance
  6. Which Savings Plan covers EC2, Fargate, and Lambda usage?

    • A) Compute Savings Plan
    • B) EC2 Savings Plan
    • C) SageMaker Savings Plan
    • D) Machine Learning SP
  7. A company runs a batch processing workload that can tolerate interruptions. Which pricing model is MOST cost-effective?

    • A) On-Demand
    • B) Reserved Instance
    • C) Spot Instance
    • D) Dedicated Host
  8. Which AWS tool provides recommendations to rightsize EC2 instances based on historical utilization?

    • A) Cost Explorer
    • B) Compute Optimizer
    • C) Trusted Advisor
    • D) Budgets
  9. What is the primary advantage of using S3 Intelligent-Tiering?

    • A) Lowest storage cost
    • B) Automatic cost savings by moving data between access tiers
    • C) Fastest retrieval speed
    • D) Built-in encryption
  10. A company wants to get a 30% discount on EC2 without committing to a specific instance family. Which option should they choose?

    • A) Standard Reserved Instance
    • B) Convertible Reserved Instance
    • C) Compute Savings Plan
    • D) EC2 Savings Plan
  11. Which billing option provides the highest discount for Reserved Instances?

    • A) No upfront
    • B) Partial upfront
    • C) All upfront
    • D) Monthly
  12. What is the cost benefit of using VPC Gateway Endpoints instead of NAT Gateway for S3 access?

    • A) Higher bandwidth
    • B) No hourly charges or data processing fees
    • C) Lower latency
    • D) Built-in encryption
  13. A company has predictable steady-state workloads. Which purchasing option is MOST cost-effective?

    • A) On-Demand
    • B) Spot
    • C) Reserved Instance
    • D) Dedicated Host
  14. Which AWS Budget type alerts you when your spending exceeds a threshold?

    • A) Cost Budget
    • B) Usage Budget
    • C) Savings Plans Budget
    • D) Anomaly Budget
  15. What is the benefit of using gp3 EBS volumes over gp2?

    • A) Higher maximum throughput
    • B) Baseline performance independent of volume size
    • C) Lower latency
    • D) Built-in encryption
  16. Which AWS service provides a detailed breakdown of your AWS costs and usage?

    • A) Cost Explorer
    • B) Compute Optimizer
    • C) Trusted Advisor
    • D) Budgets
  17. A company needs to reduce data transfer costs for content served to global users. Which service should they use?

    • A) Direct Connect
    • B) CloudFront
    • C) VPC Peering
    • D) NAT Gateway
  18. What is the pricing model for Amazon DynamoDB On-Demand?

    • A) Pay per provisioned RCU/WCU
    • B) Pay per request (reads and writes)
    • C) Pay per GB stored only
    • D) Free tier only
  19. Which Trusted Advisor category checks if Multi-AZ is enabled on RDS?

    • A) Cost Optimization
    • B) Performance
    • C) Fault Tolerance
    • D) Security
  20. A company wants to automatically stop idle EC2 instances during non-business hours. Which AWS service can help automate this?

    • A) Instance Scheduler
    • B) Auto Scaling
    • C) Compute Optimizer
    • D) Cost Explorer
  21. What is the primary difference between a Standard RI and a Convertible RI?

    • A) Convertible RIs can be sold on the marketplace
    • B) Convertible RIs allow changing instance family but offer a lower discount
    • C) Standard RIs have a shorter term
    • D) Convertible RIs require all upfront payment
  22. Which AWS tool provides automated checks for idle load balancers and unassociated Elastic IPs?

    • A) Compute Optimizer
    • B) Cost Explorer
    • C) Trusted Advisor
    • D) Budgets
  23. A company stores infrequently accessed data that needs retrieval within minutes but wants lower costs than S3 Standard. Which storage class should they use?

    • A) S3 Standard-IA
    • B) S3 One Zone-IA
    • C) S3 Glacier
    • D) S3 Intelligent-Tiering
  24. Which AWS service analyzes cost and usage data to provide Reserved Instance purchase recommendations?

    • A) Compute Optimizer
    • B) Cost Explorer
    • C) Trusted Advisor
    • D) Budgets
  25. A company needs to ensure they receive alerts if AWS spending is forecasted to exceed the monthly budget. Which feature of AWS Budgets should they configure?

    • A) Actual cost alert
    • B) Forecasted cost alert
    • C) Usage alert
    • D) Savings Plans alert
📝 Answer Key
  1. C — 3-year All Upfront Reserved Instance has the highest discount (up to 72%).
  2. C — Compute Optimizer uses ML for resource recommendations.
  3. B — VPC Gateway Endpoint for S3 is free, while NAT Gateway costs per hour + GB.
  4. C — CostCenter tags group costs by department/project.
  5. B — Cost Optimization checks for idle and underutilized resources.
  6. A — Compute Savings Plan covers EC2, Fargate, and Lambda with a $/hr commitment.
  7. C — Spot Instances offer up to 90% discount for fault-tolerant, interruption-tolerant workloads.
  8. B — Compute Optimizer uses ML to analyze utilization and recommend right-sizing.
  9. B — S3 Intelligent-Tiering automatically moves data between access tiers based on usage.
  10. C — Compute Savings Plan offers flexibility across instance families, regions, and compute services.
  11. C — All upfront payment provides the highest discount for Reserved Instances.
  12. B — VPC Gateway Endpoints are free — no hourly or data processing charges.
  13. C — Reserved Instances are most cost-effective for predictable, steady-state workloads.
  14. A — Cost Budget tracks dollar amount spending against a budget threshold.
  15. B — gp3 provides baseline 3,000 IOPS and 125 MB/s regardless of volume size.
  16. A — Cost Explorer visualizes and analyzes AWS costs and usage with custom reports.
  17. B — CloudFront reduces data transfer costs by caching content at edge locations.
  18. B — DynamoDB On-Demand charges per read and write request, not provisioned capacity.
  19. C — Fault Tolerance checks verify Multi-AZ and backup configuration for RDS.
  20. A — Instance Scheduler automates starting and stopping EC2 instances on a schedule.
  21. B — Convertible RIs allow instance family changes but offer lower discounts than Standard RIs.
  22. C — Trusted Advisor includes checks for idle load balancers and unassociated Elastic IPs.
  23. A — S3 Standard-IA provides rapid access for infrequently used data at lower cost.
  24. B — Cost Explorer provides Reserved Instance purchase recommendations based on usage.
  25. B — Forecasted cost alerts trigger when spending is projected to exceed the budget.

📚 Additional Resources#

Next → Exam Preparation Guide