Sub topic 15
A repeatable optimization workflow
This chapter assumes you know the earlier material: the transfer categories AWS prices differently, Cost Explorer usage types, VPC Flow Logs, NAT gateways, VPC endpoints, and CloudFront. Here you combine them into one loop you can run every month.
Why a workflow
One-off fixes drift. A new service gets deployed in the wrong AZ, a team adds a NAT-routed S3 job, and the savings disappear. A fixed sequence keeps you working on the largest cost first and stops you from guessing.
Step 1: Measure
Group last month's cost by usage type and keep only the transfer lines.
aws ce get-cost-and-usage \
--time-period Start=2026-10-01,End=2026-11-01 \
--granularity MONTHLY \
--metrics UnblendedCost \
--group-by Type=DIMENSION,Key=USAGE_TYPE \
--query "ResultsByTime[].Groups[?contains(Keys[0], 'DataTransfer') || contains(Keys[0], 'NatGateway-Bytes')].[Keys[0], Metrics.UnblendedCost.Amount]" \
--output textSort the result by cost and pick the top one or two usage types. Ignore the rest for now.
Step 2: Locate
The usage type tells you the category, such as inter-AZ, internet egress, or NAT processing. It does not tell you which workload caused it. Run a Logs Insights query against your VPC Flow Logs to find the busiest address pairs.
stats sum(bytes) as bytesTransferred by srcAddr, dstAddr
| sort bytesTransferred desc
| limit 10Map those addresses to instances, load balancers, or NAT gateway network interfaces, then to the owning team or service.
Step 3: Pick a fix
Match the cost category to the standard remedy.
| Top cost driver | First fix to try |
|---|---|
| NAT gateway bytes to S3 or DynamoDB | Gateway VPC endpoint |
| NAT gateway bytes to other AWS services | Interface endpoint, if the volume justifies its hourly cost |
| Inter-AZ transfer | Keep chatty tiers in one AZ, or use AZ-aware routing |
| Internet egress | CloudFront caching and compression |
| Cross-region transfer | Replicate less data, or move the consumer to the data |
For the most common case, an S3 gateway endpoint takes one command:
aws ec2 create-vpc-endpoint \
--vpc-id vpc-0123456789abcdef0 \
--vpc-endpoint-type Gateway \
--service-name com.amazonaws.us-east-1.s3 \
--route-table-ids rtb-0123456789abcdef0Step 4: Verify
After the change, confirm it worked at two levels. First, check that the flow log traffic through the NAT gateway interface dropped. Second, wait for the next billing data and compare the same usage type against the baseline from Step 1. Cost Explorer data lags, so record the baseline and the date of the change in your ticket.
Step 5: Monitor
Fixes regress unless something watches them. Set a budget on the transfer usage types and an anomaly monitor for the services involved. Enforce cost allocation tags on the resources you found, so the next investigation starts from an owner instead of an IP address.
Common mistakes
- Starting with the cheapest fix to implement instead of the most expensive line item.
- Judging success from a few days of data, when billing lags and traffic varies by week.
- Adding interface endpoints everywhere. Each one has an hourly charge per AZ, so it only pays off when it replaces enough NAT traffic.
Exercise
Your Cost Explorer query shows NatGateway-Bytes as the largest transfer line. Flow Logs show most of the bytes go to S3 addresses. Name the fix, and name two ways to verify it worked.
Show answer
Create an S3 gateway VPC endpoint and add it to the private subnets' route tables. Verify that (1) flow log bytes on the NAT gateway's network interface drop, and (2) the NatGateway-Bytes usage type falls against the baseline in the next billing data.