Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 70 ยท Monitoring, operations and cost

Cost Management and Optimization

Understand your AWS bill: Cost Explorer, budgets, tags, right-sizing and the egress and NAT surprises, with a runnable monthly bill estimator.

The cloud bills for what you use, by the hour, the gigabyte and the request. That is a gift when you are small and a trap when you are careless: a forgotten test server, a NAT gateway moving terabytes, or an S3 bucket that logs into itself can turn a ten-dollar month into a four-figure one. Cost management is not a finance activity done after the fact. It is a design habit, as important as security.

This lesson shows how a bill is built, how to watch it (Cost Explorer, Budgets, tags), what usually causes surprises, and how to estimate a bill before you deploy. Earlier lessons covered individual pricing models: EC2 purchasing options and S3 storage classes explain how to pay less for compute and storage. Here we look at the whole picture.

The prices in this lesson are rounded, illustrative figures roughly like US East (N. Virginia) list prices at the time of writing. Prices differ by Region and change over time, so treat the numbers as teaching values and check the official pricing pages and the AWS Pricing Calculator before you decide anything. The billing console and CLI cannot run on this site, so those parts are read-only. The runnable examples are Python estimators.

How a bill is built

Almost every AWS price is the product of a unit price and a quantity. Four kinds of quantity cover most of a bill:

  • Time. Instances, load balancers, NAT gateways, databases and public IPv4 addresses are billed per hour (or per second) while they exist, whether or not they do useful work.
  • Capacity. Storage is billed per gigabyte-month: EBS volumes (even unattached ones), snapshots, S3 objects, database storage, log storage.
  • Requests and invocations. Lambda, API Gateway, SQS, S3 GET and PUT calls, DynamoDB on-demand reads and writes.
  • Data movement. Data into AWS is generally free. Data out to the internet, between Regions and (often forgotten) between Availability Zones is charged per gigabyte.

The first 100 GB per month of data transfer out to the internet is free (aggregated across services), and traffic inside the same AZ over private addresses is free. After that it adds up quickly: roughly 9 cents per gigabyte for the first tier of internet egress, about 1 cent per gigabyte in each direction between AZs, and 2 cents between Regions.

Watching spending: Cost Explorer, Budgets and anomalies

Four tools are worth turning on from the first day (see Creating and Securing an Account for the first billing alert):

  • Cost Explorer is the graphing tool. It groups costs by service, Region, account, tag, usage type and more, shows up to about 13 months of history and can forecast the next period. A typical first question is "group by service, last 3 months, daily". An hourly and resource-level view can be enabled when you need to find one expensive resource.
  • AWS Budgets lets you set a monthly amount (or a usage amount) and get notified, for example at 80 percent of actual spend and 100 percent of forecasted spend. Budgets can also run actions, such as attaching a restrictive policy when a threshold is crossed. A budget alerts you; it does not stop spending by itself unless you add an action.
  • Cost Anomaly Detection uses machine learning to flag unusual spending patterns, such as a service whose daily cost doubled, and can notify you.
  • Cost and Usage Reports (Data Exports) deliver the full line-item detail to S3, where you can query it with Athena. This is the data source for serious analysis.
Terminal
# Last month's cost grouped by service (read-only here)
aws ce get-cost-and-usage \
  --time-period Start=2026-04-01,End=2026-05-01 \
  --granularity MONTHLY \
  --metrics UnblendedCost \
  --group-by Type=DIMENSION,Key=SERVICE

Remember that billing data is delayed: the numbers for today are incomplete and can lag by hours.

Tags and cost allocation

When one account hosts several teams, products or environments, a bill grouped by service is not enough. You need to know what the checkout team costs. The answer is tags: key/value labels such as team=checkout, env=prod, project=shop.

To use tags in cost reports you must activate them as cost allocation tags in the billing console. They only apply to costs from then on (not retroactively), and it can take up to a day to show. Some tags, such as aws:createdBy, are generated by AWS.

Tagging only works if it is consistent. Practical rules:

  • Agree on a small set of required tags (for example team, env, owner), and write down the exact spelling. Env and env are different keys.
  • Enforce the tags: IAM conditions, Service Control Policies or Config rules (see CloudTrail, Config and X-Ray) can block or flag untagged resources, and infrastructure as code can apply tags to everything it creates.
  • Remember that some costs are not taggable, such as data transfer between services or support fees. For multiple teams, separate AWS accounts under an Organization give the cleanest split (see Multi-Account Strategy and Control Tower).

The usual surprises

Most unexpected bills come from a short list:

Surprise Why it happens Fix
NAT gateway data processing A NAT gateway charges per hour and per gigabyte processed, including traffic to S3 or DynamoDB from private subnets Use gateway endpoints for S3 and DynamoDB (free), see NAT Gateways and VPC Endpoints
Public IPv4 addresses Every public IPv4 address, even an idle one, has an hourly charge Release unused Elastic IPs, use private subnets and load balancers, plan for IPv6
Data transfer out Serving large files straight from EC2 or S3 to the internet Put CloudFront in front (see CloudFront CDN)
Cross-AZ traffic Chatty services spread over AZs pay per gigabyte each way Keep tight call paths in one AZ where availability allows
Forgotten resources Stopped instances still pay for their EBS volumes, unattached volumes and old snapshots remain, idle load balancers bill hourly Tag owners, review regularly, delete test environments
Logs CloudWatch log ingestion is charged per gigabyte, and groups never expire by default Log less at debug level, set retention, see CloudWatch Metrics, Logs and Alarms
Unbounded serverless A loop that calls Lambda or a queue that retries forever Set concurrency limits, DLQs and budgets

Estimating a bill

Before you deploy anything, estimate it. The AWS Pricing Calculator is the official tool. The stand-in below shows the same idea in code so you can see where the money goes: a list of line items, each with a quantity and a unit price, and the free allowances applied. The prices are the illustrative numbers from the note above.

Python A monthly bill estimator for a small web app

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result is about 360 dollars a month. The two instances are the largest line, but the internet egress (45 dollars) and the NAT gateway (about 51 dollars with its data processing) together cost more than the database. That is a typical surprise: people size the instances carefully and forget the traffic.

Going deeper: filters, CUR and commitment reports

Cost Explorer answers most questions, and the Cost and Usage Report answers the rest. Both start from the same idea: every charge is a line item with a service, usage type, Region, account, tags and a cost.

Cost Explorer: filters, groups and cost types

In Cost Explorer you combine two controls. A filter narrows the data (service is EC2, Region is eu-west-1, tag team is checkout), and group by splits what remains into series (by service, linked account, usage type, tag). "Filter to EC2, group by usage type" quickly shows whether the money goes to instance hours, EBS or data transfer. You can also choose the cost type:

  • Unblended: what each charge costs on the day it happened. The default.
  • Amortized: spreads upfront payments (Reserved Instance or Savings Plan fees) over the term, so a month with a big prepayment does not look odd. Best for comparing teams.
  • Net variants subtract discounts such as credits or private pricing.

Cost Explorer can forecast the rest of the month from past trends, and its Savings Plans and Reserved Instance reports show two numbers that you should watch:

  • Utilization: how much of your commitment was used. 100% is ideal, and 70% means you pay for capacity you did not use.
  • Coverage: what share of eligible usage the commitment covered. Low coverage with high utilization means there is room to commit more. See EC2 purchasing options.

Cost and Usage Reports (CUR) and Data Exports

The Cost and Usage Report is the most detailed billing data AWS gives you: one row per line item, per hour or day, with resource IDs and tags. You configure it once in the billing console (or with Data Exports), and AWS delivers new files to an S3 bucket several times a day, usually as Parquet. Because it is just data in S3, you query it with Athena with plain SQL (read-only):

SQL
SELECT
  line_item_product_code                  AS service,
  resource_tags_user_team                 AS team,
  ROUND(SUM(line_item_unblended_cost), 2) AS cost
FROM cur_database.cur_table
WHERE year = '2026' AND month = '11'
  AND line_item_line_item_type = 'Usage'
GROUP BY 1, 2
ORDER BY cost DESC
LIMIT 20;

Column names follow the pattern line_item_*, product_*, pricing_*, reservation_*, savings_plan_* and resource_tags_user_<tag>. The report is also where you see commitment details such as the effective cost of a Savings Plan.

Reasons to use it: cost per resource ID, per tag and per hour, showback to teams, finding the exact NAT gateway that moved 40 TB, and feeding a BI dashboard. The cost is the S3 storage and the Athena scans, both small if you use Parquet and partitions.

Reading a bill

A monthly invoice has a predictable shape. Read it from the top:

  1. Totals and credits: what you owe after Free Tier, promotional credits and discounts.
  2. By service: sort by size. The top three services usually explain 80 percent of the bill.
  3. By Region: a charge in a Region you never use is a warning sign (a forgotten resource, or a leaked key).
  4. Usage types inside a service: BoxUsage is instance hours, EBS:VolumeUsage is volumes, DataTransfer-Out-Bytes is egress, NatGateway-Bytes is NAT processing.
  5. Taxes and support: listed separately.

The stand-in mimics a tiny CUR and applies a filter and a group, like Cost Explorer, then calculates Savings Plan utilization and coverage for the same usage.

Python Filter, group and measure a Savings Plan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reducing the bill

Optimization follows a simple order: remove waste, right-size, commit, then redesign.

  1. Remove waste. Delete unattached volumes and old snapshots, release unused Elastic IPs, stop development environments overnight, and shorten log retention.
  2. Right-size. Many instances run at 10 percent CPU. AWS Compute Optimizer and Cost Explorer's right-sizing recommendations look at utilization and suggest smaller instance types. Check memory too (it needs the CloudWatch agent). Moving from m5.large to m5.medium-class capacity halves that line.
  3. Commit. For steady load, Savings Plans and Reserved Instances can reduce compute cost substantially. Use Spot for interruptible work. Do this after right-sizing, otherwise you commit to waste. Details are in EC2 purchasing options.
  4. Redesign. Gateway endpoints instead of NAT for S3, CloudFront for static delivery, S3 lifecycle rules to cheaper storage, Graviton (Arm) instances for a better price per performance, serverless for spiky traffic.

The next stand-in compares the NAT gateway and the free S3 gateway endpoint for different amounts of S3 traffic from a private subnet, which is one of the most common and cheapest optimizations.

Python NAT gateway versus gateway endpoint for S3 traffic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At 10 GB the endpoint saves only 45 cents. At 5 TB it saves hundreds of dollars a month, with one route table entry. The lesson generalizes: look for the big, per-gigabyte line on the bill first.

flowchart TD
    A["Bill is too high"] --> B["Cost Explorer: group by service,<br/>then by usage type or tag"]
    B --> C{"Is it waste?"}
    C -- Yes --> D["Delete or stop it"]
    C -- No --> E{"Is it oversized?"}
    E -- Yes --> F["Right-size"]
    E -- No --> G{"Is the usage steady?"}
    G -- Yes --> H["Commit: Savings Plans"]
    G -- No --> I["Redesign: endpoints, CDN,<br/>serverless, lifecycle rules"]

A practical order of attack when the bill grows.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which of these is generally free of charge?

    Inbound data is generally free. Outbound internet traffic (beyond the free allowance), inter-Region traffic and NAT gateway processing are charged per gigabyte.
  2. 2. You activated the cost allocation tag team today. When will costs show the tag?

    Cost allocation tags apply from the moment they are activated, not retroactively, and can take up to about 24 hours to show in reports.
  3. 3. What does AWS Budgets do by default when your actual cost crosses a threshold?

    A budget is an alert system. You can attach actions (for example applying a restrictive policy), but nothing stops by default.
  4. 4. A private-subnet application downloads 3 TB per month from S3 through a NAT gateway. What is the cheapest fix?

    A gateway endpoint carries S3 traffic privately with no hourly or per-gigabyte charge, so the data stops flowing through the NAT gateway's per-gigabyte meter.
  5. 5. Why should you right-size before buying Savings Plans?

    A commitment locks in spending for a year or more. If you commit before removing waste, you lock in the waste as well.

Summary

  • A bill is unit prices multiplied by time, capacity, requests and data movement. Data in is free; data out, cross-Region and cross-AZ traffic is not.
  • Use Cost Explorer to analyze, Budgets to alert (and act), Cost Anomaly Detection to spot surprises and Cost and Usage Reports for detail.
  • Activate cost allocation tags and enforce a small set of required tags; separate accounts give the cleanest split.
  • The usual surprises are NAT data processing, public IPv4 addresses, egress, cross-AZ traffic, forgotten resources and unlimited logs.
  • Optimize in order: remove waste, right-size, commit, redesign. Estimate before you deploy.

Next: Encryption and KMS