
Find the IP pairs behind your cross-AZ data transfer bill (Part 2): Calculate cost with Amazon Athena
Generate predictable test traffic, process enriched VPC Flow Logs, deduplicate observations in Athena, and calculate cross-AZ and Transit Gateway cost per IP pair.
In Part 1 , you created the VPC Flow Logs pipeline: an S3 destination, the required flow-log subscriptions, a subnet map and the Glue enrichment job. This part generates recognizable lab traffic, processes the delivered logs, and uses Athena to count each flow once and calculate its estimated cost.
Required source: Summary tabs are explanations, not executable code. Open Complete SQL and copy all three queries before following either Console or CLI.
Prerequisites
- Complete the Part 1 pipeline through the Glue job and its hourly schedule.
- Wait for flow-log delivery: Parquet files normally reach the
flow-logs/AWSLogs/prefix every few minutes.
- If you are analysing your own VPCs, skip the lab traffic section and use traffic from your workloads.
- For the CLI alternatives, the same shell setup as Part 1 (
AWS_REGION,ACCOUNT_ID,BUCKET) and the SQL files saved assql/01-create-flows-view.sqlandsql/02-cross-az-cost-by-ip-pair.sql.
Generate test traffic (lab path only)
Use this section only with the disposable lab from Part 1. If you are analysing your own VPCs, skip to the next section and let normal workload traffic generate the flow logs. You send three transfers of different sizes to server B (10.1.1.100, VPC B, AZ1), so you can recognize them in the results:
# | From | Path | Size | Expected charge |
|---|---|---|---|---|
① | Demo client B, 10.1.3.100 (VPC B, AZ2) | Same VPC, cross-AZ | 2 GiB | Cross-AZ $0.02/GB |
② | Demo client A, 10.0.3.100 (VPC A, AZ2) | Transit Gateway, cross-AZ | 1 GiB | TGW processing $0.02/GB, no cross-AZ |
③ | Server A, 10.0.1.100 (VPC A, AZ1) | Transit Gateway, same AZ | 512 MiB | TGW processing $0.02/GB |
Systems Manager → Run Command → Run command. No SSH or bastion host needed.
- Search for and select the document AWS-RunShellScript. Paste the commands below into Commands, choose Choose instances manually, select VPC B Private AZ1 Server → Run:
dnf install -y iperf3iperf3 -s -D- Run it again on VPC B Private AZ2 Demo Client with:
dnf install -y iperf3for i in $(seq 1 60); do iperf3 -c 10.1.1.100 -n 2G && break; sleep 5; doneRepeat with
-n 1G on VPC A Private AZ2 Demo Client and -n 512M on VPC A Private AZ1 Server. iperf3 serves one client at a time, so the loop retries until the server is free.- Open each command → instance → Output. The
senderline shows the bytes sent.

Process the logs and calculate cost with Athena
6a. Run the job
Flow logs reach S3 every few minutes. About 10 minutes after the transfers, check that
.log.parquet files appear under flow-logs/AWSLogs/ in your bucket.Run the job again right away and nothing changes: thanks to the job bookmark, it finds no new files. If a manual run overlaps the hourly schedule, the scheduled run fails with
Max concurrent runs exceeded , and nothing is processed twice.6b. Create the view and run the cost query
The three queries create the view, verify the lab flows, and calculate cost.
Select the Complete SQL tab to view and copy the full SQL.
6a. Run the job
The hourly schedule picks them up. To skip the wait, choose Run on the job. Runs shows Succeeded after about 2 minutes (11 seconds of startup), and Data Catalog → Tables now lists
flow_logs_enriched .
6b. Create the view and run the cost query
- Athena → Query editor. If Athena has no result location yet, open Query settings → Manage, set Location of query result to
s3://BUCKET/athena-results/→ Save.
- On the Editor tab, select database
cross_az_analytics, run the view shown in this section.
- In a new query tab (+), check the deduplication on the lab flows:
- In another new tab, run the cost query. Then open the tab's ⋮ menu → Save as, name it
cross-az-cost-by-ip-pair→ Save query. It's now under Saved queries.

srcaddr dstaddr traffic_path observers gib10.1.3.100 10.1.1.100 Cross-AZ (same VPC) 2 2.01210.0.3.100 10.1.1.100 Inter-VPC (Transit Gateway) 4 1.00610.0.1.100 10.1.1.100 Inter-VPC (Transit Gateway) 4 0.503The same-VPC flow was logged by the client and the server; the Transit Gateway flows also by the Transit Gateway ENI in each VPC. Each flow is counted once. (In our first run the client ENI of flow ② logged nothing, so it showed 3 observers and still 1.006 GiB.) Flow logs count IP and TCP headers too, which explains the extra ~0.6% over iperf3's numbers.
How to read it:
- Flow ① is the only lab transfer with cross-AZ cost: 2.012 GB × $0.02 = $0.04, that is $0.01/GB on the sending side plus $0.01/GB on the receiving side.
- Flow ② also crosses AZs, but through the Transit Gateway, so its cross-AZ cost is $0 and it pays $0.02 Transit Gateway processing instead. A naive "source AZ ≠ destination AZ" query would have added a cross-AZ charge on top.
- NAT gateway rows. VPC B's single NAT gateway in AZ1 delivers the
dnf installpackages to the client in AZ2: 0.3 GB of cross-AZ traffic on a fresh lab, a few MB once iperf3 is installed. A single NAT gateway shared by several AZs is one of the most common sources of cross-AZ cost. NAT gateway processing ($0.045/GB) comes on top and isn't estimated here.
- Small reverse rows such as 10.1.1.100 → 10.1.3.100 are TCP acknowledgements. They're billed too.
- The rates are us-east-1 list prices. Adjust them for your Region.
Cost of the solution
us-east-1 list prices at the time of writing:
Component | Pricing | Small environment |
|---|---|---|
VPC Flow Logs to S3 | Vended logs per GB delivered, plus Parquet conversion | Usually the largest item for busy VPCs. Use the 9 required fields to reduce it. |
Amazon S3 | Standard storage | Raw logs expire after 30 days |
AWS Glue | $0.44 per DPU-hour, per second, 1-minute minimum | 0.06 DPU-hours per run ≈ $0.03, so about $19/month hourly. Run less often, or use Flex execution, to reduce it. |
Amazon Athena | $5 per TB scanned | Cents |
Lambda, Parameter Store | Negligible | |
Clean up
If you continue with Part 3, keep everything running. Otherwise, in the console:
- VPC: delete the
cross-az-analyticsflow logs on each VPC (Flow logs tab → Actions → Delete flow logs).
- Glue: delete the schedule
cross-az-flowlogs-hourly(job → Schedules tab), the jobcross-az-flowlogs-enrichmentand the databasecross_az_analytics, which removes the table and the view.
- Athena: delete the saved query
cross-az-cost-by-ip-pair. If you set the query result location in Step 6, point it somewhere else before the bucket goes.
- Lambda and Parameter Store: delete
cross-az-subnet-mapper,/cross-az/subnet-mapand the log group/aws/lambda/cross-az-subnet-mapper.
- IAM: delete the roles
CrossAzGlueJobRoleandcross-az-subnet-mapper-role-.... The console removes their inline policies with them; theAWSLambdaBasicExecutionRole-...policy that Lambda created stays under Policies until you delete it.
- S3: empty and delete the
cross-az-analytics-...bucket, and the job script underaws-glue-assets-.../scripts/.
- Lab only: delete the stacks
CrossAzDemoClients,NetworkingWorkshopMultiVPCandTGWandNetworkingWorkshopPrerequisites, in that order.
Next: Part 3
Continue with Part 3: Amazon Quick Sight dashboard, fixes and production use . It builds the dashboard, shows which fixes remove cross-AZ cost, covers what changes in production, and deploys the whole pipeline as code from the companion repository.
Resources
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article