Module 11 of 13 · AWS Fundamentals · Beginner

Cost Management

Duration: 40 min

AWS offers flexible pricing, but costs can grow quickly without proper management. This module covers the free tier, pricing models, Cost Explorer, budgets, and reserved instances to help you optimize spending.

AWS Free Tier

The AWS Free Tier provides free access to many services for 12 months after account creation. It includes:

After 12 months or usage limits, you pay standard rates. Always monitor usage to avoid unexpected charges.

Pricing Models

On-Demand pricing charges per hour/second of usage. No upfront commitment. Best for variable workloads.

Reserved Instances (RIs) offer discounts (up to 72%) for 1 or 3-year commitments. Best for predictable, steady-state workloads.

Spot Instances offer up to 90% discounts for spare capacity. Can be interrupted with 2-minute notice. Best for fault-tolerant, flexible workloads.

Savings Plans provide discounts on compute usage across EC2, Lambda, and Fargate. More flexible than RIs.

Cost Explorer

Cost Explorer visualizes your AWS spending. You can filter by service, region, or tag. It shows trends and forecasts future costs.

Use Cost Explorer to identify expensive services and optimize spending.

Budgets

AWS Budgets lets you set spending limits and receive alerts when approaching or exceeding them. You can set budgets by service, region, or tag.

Hands-On: Monitor Costs

View billing dashboard:

aws ce get-cost-and-usage --time-period Start=2024-01-01,End=2024-01-31 \
  --granularity MONTHLY --metrics "UnblendedCost" --group-by Type=DIMENSION,Key=SERVICE

Create a budget:

aws budgets create-budget --account-id ACCOUNT_ID \
  --budget '{
    "BudgetName": "monthly-budget",
    "BudgetLimit": {"Amount": "100", "Unit": "USD"},
    "TimeUnit": "MONTHLY",
    "BudgetType": "COST"
  }'

Create budget notification:

aws budgets create-notification --account-id ACCOUNT_ID \
  --budget-name monthly-budget \
  --notification '{
    "NotificationType": "ACTUAL",
    "ComparisonOperator": "GREATER_THAN",
    "Threshold": 80,
    "ThresholdType": "PERCENTAGE"
  }' \
  --subscribers '[{"SubscriptionType": "EMAIL", "Address": "user@example.com"}]'

Get cost and usage:

aws ce get-cost-and-usage --time-period Start=2024-01-01,End=2024-01-31 \
  --granularity DAILY --metrics "UnblendedCost"

Python Boto3 Example

import boto3
from datetime import datetime, timedelta

ce = boto3.client('ce')

# Get cost and usage
end_date = datetime.now().date()
start_date = end_date - timedelta(days=30)

response = ce.get_cost_and_usage(
    TimePeriod={
        'Start': start_date.isoformat(),
        'End': end_date.isoformat()
    },
    Granularity='DAILY',
    Metrics=['UnblendedCost'],
    GroupBy=[
        {'Type': 'DIMENSION', 'Key': 'SERVICE'}
    ]
)

for result in response['ResultsByTime']:
    print(f"Date: {result['TimePeriod']['Start']}")
    for group in result['Groups']:
        service = group['Keys'][0]
        cost = group['Metrics']['UnblendedCost']['Amount']
        print(f"  {service}: ${cost}")

Reserved Instances

Purchase RIs for predictable workloads:

aws ec2 describe-reserved-instances-offerings \
  --instance-type t3.micro --filters Name=scope,Values=us-east-1

Spot Instances

Launch Spot instances for cost savings:

aws ec2 request-spot-instances --spot-price 0.05 \
  --instance-count 1 --type one-time \
  --launch-specification '{
    "ImageId": "ami-0c55b159cbfafe1f0",
    "InstanceType": "t3.micro",
    "KeyName": "my-key"
  }'

Terraform Example

resource "aws_instance" "on_demand" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"

  tags = {
    Name = "on-demand"
  }
}

resource "aws_instance" "spot" {
  ami                    = "ami-0c55b159cbfafe1f0"
  instance_type          = "t3.micro"
  spot_price             = "0.05"
  instance_interruption_behavior = "terminate"

  tags = {
    Name = "spot"
  }
}

resource "aws_ec2_reserved_instance" "example" {
  instance_type           = "t3.micro"
  offering_class          = "standard"
  offering_type           = "ALL_UPFRONT"
  purchase_term           = "ONE_YEAR"
  availability_zone       = "us-east-1a"
  instance_count          = 1
}

Cost Optimization Tips

  1. Use the free tier for learning and development
  2. Right-size instances (don't over-provision)
  3. Use Reserved Instances for predictable workloads
  4. Use Spot Instances for fault-tolerant workloads
  5. Enable S3 lifecycle policies to move old data to cheaper storage
  6. Delete unused resources (EBS volumes, snapshots, etc.)
  7. Use CloudWatch to monitor resource usage
  8. Set up budgets and alerts

Quiz 1

❓ What is the AWS Free Tier?

Quiz 2

❓ What are Reserved Instances?

Quiz 3

❓ What are Spot Instances?

Quiz 4

❓ What is Cost Explorer?

Quiz 5

❓ What are AWS Budgets?

← Previous Continue interactively → Next →

Related Courses