AWS Builder Center
Find the IP pairs behind your cross-AZ data transfer bill (Part 2): Calculate cost with Amazon Athena

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 as  sql/01-create-flows-view.sql  and  sql/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.
  1. 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 iperf3
iperf3 -s -D
  1. Run it again on VPC B Private AZ2 Demo Client with:
dnf install -y iperf3
for i in $(seq 1 60); do iperf3 -c 10.1.1.100 -n 2G && break; sleep 5; done
Repeat 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.
  1. Open each command → instance → Output. The  sender  line shows the bytes sent.
Systems Manager Run Command output showing a 2 GiB iperf3 transfer




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 .
Successful Glue job run using two G.1X workers and Glue 5.0

6b. Create the view and run the cost query
  1. 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.
  1. On the Editor tab, select database  cross_az_analytics , run the view shown in this section.
  1. In a new query tab (+), check the deduplication on the lab flows:
  1. 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.
Athena query showing cross-AZ and Transit Gateway traffic cost estimates



srcaddr dstaddr traffic_path observers gib
10.1.3.100 10.1.1.100 Cross-AZ (same VPC) 2 2.012
10.0.3.100 10.1.1.100 Inter-VPC (Transit Gateway) 4 1.006
10.0.1.100 10.1.1.100 Inter-VPC (Transit Gateway) 4 0.503
The 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 install  packages 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:
  1. VPC: delete the  cross-az-analytics  flow logs on each VPC (Flow logs tab → Actions → Delete flow logs).
  1. Glue: delete the schedule  cross-az-flowlogs-hourly  (job → Schedules tab), the job  cross-az-flowlogs-enrichment  and the database  cross_az_analytics , which removes the table and the view.
  1. 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.
  1. Lambda and Parameter Store: delete  cross-az-subnet-mapper ,  /cross-az/subnet-map  and the log group  /aws/lambda/cross-az-subnet-mapper .
  1. IAM: delete the roles  CrossAzGlueJobRole  and  cross-az-subnet-mapper-role-... . The console removes their inline policies with them; the  AWSLambdaBasicExecutionRole-...  policy that Lambda created stays under Policies until you delete it.
  1. S3: empty and delete the  cross-az-analytics-...  bucket, and the job script under  aws-glue-assets-.../scripts/ .
  1. Lab only: delete the stacks  CrossAzDemoClients ,  NetworkingWorkshopMultiVPCandTGW  and  NetworkingWorkshopPrerequisites , 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

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article