Sub topic 15
A repeatable optimization workflow
Earlier chapters covered individual causes of data transfer cost and a fix for each. This chapter joins them into one loop you can run on any account: measure, attribute, rank, fix, verify, and guard. It assumes you can read a Cost Explorer report, run AWS CLI commands, and understand VPC route tables and Availability Zones (AZs).
Why a workflow beats one-off fixes
Teams often fix the first transfer charge they see, such as a NAT gateway line item, and stop. Three problems follow:
- The biggest cost driver may be somewhere else, such as inter-AZ traffic between a database and its clients.
- Nobody confirms the fix worked, so a misapplied route table change goes unnoticed.
- The cost returns within months because a new service or deployment reintroduces the old pattern.
A workflow fixes these by forcing a measurement before and after each change and by leaving alarms in place afterward.
Step 1: Measure by usage type
Start with Cost Explorer grouped by usage type, not by service. The service view lumps transfer charges into EC2, S3, and others. The usage type view separates the categories that AWS prices differently. These are the names to look for, usually with a region prefix such as USE1-:
| Usage type pattern | What it usually means |
|---|---|
| `DataTransfer-Out-Bytes` | Traffic leaving AWS to the internet |
| `DataTransfer-Regional-Bytes` | Traffic between AZs in one region |
| `*-AWS-Out-Bytes` and `*-AWS-In-Bytes` | Traffic between regions |
| `NatGateway-Bytes` | Data processed by a NAT gateway |
| `NatGateway-Hours` | Fixed hourly NAT gateway charge |
Run the query from earlier in this tutorial over a full month:
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 textSave the output. It is your baseline, and step 5 needs it. Each Cost Explorer API request is billed (about $0.01 at the time of writing), so run scripted queries on a schedule rather than in a tight loop.
Step 2: Attribute cost to resources
Cost Explorer tells you the category, not the culprit. To find the resources behind a category, use the tool that matches it:
- NAT gateway or inter-AZ charges: enable VPC Flow Logs to CloudWatch Logs and look for the largest flows.
- Internet egress: check CloudFront and S3 request metrics, and Application Load Balancer access logs, to see which paths and clients move the most bytes.
- Cross-region charges: check S3 replication rules, database replicas, and any jobs that copy data between regions.
This CloudWatch Logs Insights query works on flow logs in the default format and returns the ten busiest address pairs:
stats sum(bytes) as bytesTransferred by srcAddr, dstAddr
| sort bytesTransferred desc
| limit 10Map each address to a resource by looking it up in the EC2 console or in aws ec2 describe-network-interfaces. If the destination is a public IP, compare it with the published AWS IP ranges to see whether it belongs to S3 or DynamoDB. Traffic to those two services through a NAT gateway is the most common and cheapest problem to fix.
Cost allocation tags help here too, but only for resources that carry them. Data transfer charges are billed against the resource that sends the traffic, so tag EC2 instances, load balancers, and NAT gateways.
Step 3: Rank by savings and effort
You now have a list of candidates. Do not start with the one you find most interesting. Estimate each item's monthly savings and the work to get it, then sort.
| Candidate | Typical effort | Typical risk |
|---|---|---|
| S3 or DynamoDB through NAT | Low: add a gateway endpoint and route | Low, but check endpoint policies |
| Enabling compression | Low to medium | Low |
| CloudFront in front of public content | Medium | Medium: cache behavior and invalidation |
| Reducing inter-AZ chatter | Medium to high: topology or client changes | Medium: lower availability if over-done |
| Cross-region replication changes | Medium | Medium to high: recovery objectives |
| Replacing peering or Transit Gateway paths | High | High |
To estimate savings, multiply the monthly gigabytes from your baseline by the price per gigabyte. For example, 20 TB a month through a NAT gateway at $0.045 per GB is about $920 in data processing charges. Prices vary by region and change over time, so confirm against the current pricing page.
The first row is usually the best opening move. A gateway endpoint for S3 or DynamoDB has no hourly or per-GB charge.
Step 4: Apply one fix at a time
Change one thing per cycle. If you add an endpoint, enable compression, and move a service in the same week, you cannot tell which change moved the bill.
For the common case, create a gateway endpoint and attach it to the route tables of the subnets that need it:
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-0123456789abcdef0Common mistakes at this step:
- Attaching the endpoint to the wrong route table. Only subnets associated with the listed route tables use the endpoint. A private subnet on a different table keeps sending S3 traffic through the NAT gateway.
- Using a region mismatch. A gateway endpoint reaches S3 buckets in its own region only. Traffic to buckets in other regions still goes through the NAT gateway.
- Over-restricting the endpoint policy. A tight policy can block a bucket the application needs, and the failure shows up as access denied errors, not as a cost change.
- Pinning everything to one AZ. Reducing inter-AZ traffic by removing cross-AZ failover trades a small cost for a larger availability risk. Keep redundancy for components whose failure would stop the service.
Step 5: Verify with the same query
After the fix has run for long enough to matter, repeat the exact Step 1 query for the new period and compare. A simple way to keep the comparison fair is to run both periods through the same command:
for period in "2026-09-01 2026-10-01" "2026-10-01 2026-11-01"; do
set -- $period
echo "Period $1 to $2"
aws ce get-cost-and-usage \
--time-period Start=$1,End=$2 \
--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 text
doneRead the result with these points in mind:
- Compare usage quantity as well as dollars, because traffic volume changes with business activity. If
NatGateway-Bytescost fell but total traffic doubled, the fix worked better than the dollar figure suggests. - Cost Explorer data lags by up to a day, and a partial month is not comparable with a full one. Compare complete periods or equal-length windows.
- If the number did not move, check the route table association and rerun the flow log query. Traffic that still appears with the NAT gateway as its path was not redirected.
Step 6: Guard against regression
A fix only lasts if something notices when the pattern comes back. Add three controls:
- A budget scoped to data transfer usage types, with alerts at a threshold below your actual limit.
- A cost anomaly monitor that flags unusual spend without a fixed threshold. This creates one that tracks each AWS service separately:
aws ce create-anomaly-monitor \
--anomaly-monitor '{"MonitorName":"transfer-by-service","MonitorType":"DIMENSIONAL","MonitorDimension":"SERVICE"}'A monitor does nothing until you add an anomaly subscription that sends alerts to an email address or an SNS topic.
- A tagging rule requiring an owner tag on any resource that can generate transfer, so the next spike has a person attached to it.
Also keep the flow log query as a saved query in CloudWatch Logs Insights, so the next investigation starts at Step 2 and not from scratch.
Running the loop in practice
A typical first pass through the workflow takes a week for a mid-sized account: a day to baseline, a few days to attribute, and one change at a time after that. After the first cycle, the later ones are shorter because the saved queries and alarms already exist. Stop a cycle when the next candidate's estimated monthly savings no longer justify the engineering time and risk of the change.
Exercise
Your Step 1 baseline shows NatGateway-Bytes as the largest transfer cost. Flow logs show most of the bytes go to S3 in the same region. You add a gateway endpoint, but next month's NatGateway-Bytes cost is unchanged. Name two things to check and the reason for each.
Show answer
First, check that the gateway endpoint is associated with the route table used by the affected private subnets. Subnets on other route tables keep sending S3 traffic to the NAT gateway. Second, check that the traffic goes to S3 buckets in the same region as the endpoint. A gateway endpoint does not cover buckets in other regions, so that traffic still uses the NAT gateway. Rerunning the flow log query for the new period shows which destinations still pass through it.