Sub topic 4
Tracing traffic with VPC Flow Logs
Cost Explorer tells you which usage type is expensive. It does not tell you which instance, subnet, or remote address caused the traffic. VPC Flow Logs fill that gap. They record the traffic crossing your network interfaces, so you can rank sources and destinations by bytes and connect each large flow to a billing category.
This chapter assumes you have read the earlier chapters on transfer categories and Cost Explorer usage types. You should also know how to read a CloudWatch Logs query and understand what an elastic network interface (ENI) is.
What a flow log record contains
A flow log is a feature you attach to a VPC, a subnet, or a single ENI. It collects the IP traffic seen there, groups packets into flows, and writes one record per flow per aggregation interval. The interval is 10 minutes by default, and you can set it to 1 minute.
A record does not contain packet contents. It holds metadata only. The default format has 14 fields, and these are the ones that matter for cost work:
| Field | Meaning | Why it matters for cost |
|---|---|---|
| `srcaddr`, `dstaddr` | Source and destination IP as seen at the interface | Identify the two ends of a flow |
| `srcport`, `dstport`, `protocol` | Transport details | Show which service generates the traffic, such as 443 for HTTPS |
| `packets`, `bytes` | Volume in the interval | The quantity you sum |
| `action` | `ACCEPT` or `REJECT` | Rejected traffic is not delivered, so filter to `ACCEPT` |
| `interface-id` | The ENI that recorded the flow | Ties a flow to an instance, NAT gateway, or load balancer |
You can also define a custom format that adds fields. The following are the most useful for cost analysis:
pkt-srcaddrandpkt-dstaddrhold the original packet addresses. Behind a NAT gateway,srcaddrshows the gateway's address, whilepkt-srcaddrshows the real instance.flow-directionshows whether the flow was ingress or egress at the interface.az-id,subnet-id, andvpc-idshow where the interface lives.pkt-dst-aws-servicenames the AWS service for traffic going to a public AWS address range, such as S3.traffic-pathrecords how egress traffic left the VPC, for example through an internet gateway, a peering connection, or a gateway endpoint.
Where to publish the logs
You choose a destination when you create the flow log.
- CloudWatch Logs. You can query with Logs Insights right away, but ingestion is priced per GB and queries are priced per GB scanned.
- Amazon S3. Delivery costs less per GB. You query with Athena and pay per data scanned, which suits high-volume VPCs and long retention.
- Kinesis Data Firehose. Use this when you need to forward records to another system.
The logs themselves can become a line item. A busy NAT gateway produces many records. Scope the flow log narrowly during an investigation, and remove it or lower its volume when you finish.
Enabling a flow log on a NAT gateway interface
NAT gateway traffic is the most common suspect, so this is a good first target. Find the gateway's ENI ID in the NAT gateway's details, then create a flow log with the fields you need. This example assumes the log group and the IAM role already exist.
aws ec2 create-flow-logs \
--resource-type NetworkInterface \
--resource-ids eni-0123456789abcdef0 \
--traffic-type ALL \
--log-destination-type cloud-watch-logs \
--log-group-name vpc-flow-nat \
--deliver-logs-permission-arn arn:aws:iam::123456789012:role/flow-logs-role \
--max-aggregation-interval 60 \
--log-format '${srcaddr} ${dstaddr} ${pkt-srcaddr} ${pkt-dstaddr} ${bytes}'The single quotes stop the shell from expanding ${...}. The format string is positional: records contain exactly these five values in this order.
Finding the top talkers
A top talker is a source and destination pair that moves the most bytes. If you used the default format, Logs Insights discovers the field names for you. This query ranks pairs by total bytes:
stats sum(bytes) as bytesTransferred by srcAddr, dstAddr
| sort bytesTransferred desc
| limit 10Run it over a representative period, such as a full business day. A one-hour sample may miss a nightly batch job that dominates the bill.
With the custom format above, Insights does not know the field order, so you parse the message yourself. This query shows which instance behind the NAT gateway talks to which remote address:
parse @message "* * * * *" as srcaddr, dstaddr, pktSrc, pktDst, bytes
| stats sum(bytes) as total by pktSrc, pktDst
| sort total desc
| limit 10The five * wildcards match the five fields in the order you defined them. If you add a field to the format, add a matching wildcard and name.
Reading the result correctly
A ranked list of IP pairs is a starting point. To turn it into a cost finding, work through these steps.
- Identify the internal address. Match the private IP to an instance, pod, or load balancer using the console or
aws ec2 describe-network-interfaces. - Classify the remote address. A public address might belong to S3, DynamoDB, or another AWS service. The
pkt-dst-aws-servicefield names it directly. You can also compare against the published AWS IP ranges. A private address in another subnet may be inter-AZ traffic. - Check direction. Egress to the internet is billed. Ingress from the internet usually is not. Through a NAT gateway, however, both directions count toward data processing.
- Pick the category. Decide whether the flow is internet egress, inter-AZ, inter-region, or NAT processing, then look up its rate.
For inter-AZ analysis, the log records only the AZ of the interface that captured the flow. Flow logs do not record the AZ of the remote end. To find cross-AZ pairs, map each IP to a subnet and therefore to an AZ. The Logs Insights function isIpv4InSubnet(address, "10.0.1.0/24") lets you do this with your subnet CIDR ranges. In Athena you can join against a table of subnet ranges.
Turning bytes into dollars
Flow logs give you bytes, and the bill is in dollars. Multiply the byte total for a flow by the rate for its category. For example, 2 TB of NAT gateway traffic at a data processing rate of about $0.045 per GB is roughly $92, before any data transfer charge on top. Check current rates on the AWS pricing pages for your Region, because they vary.
Compare your flow log totals with Cost Explorer for the same period. They should agree roughly. If Cost Explorer shows far more NAT bytes than you found, your flow log may be too narrow, or you may be looking at the wrong gateway. If it shows far less, check that you are not counting both the inbound and outbound record of the same exchange.
Gotchas and limits
- Not everything is logged. Flow logs omit traffic to the Amazon DNS server, instance metadata requests, DHCP, and a few other infrastructure addresses. These are not a meaningful cost source.
- Aggregation hides detail. A flow is summarized per interval, so you cannot see individual requests or bursts shorter than the interval.
- Both ends can log. If you log both the sender's ENI and the receiver's ENI, the same bytes appear twice. Avoid double counting by analyzing one vantage point at a time.
- NAT hides the origin. On the NAT gateway's own interface, the default
srcaddranddstaddrdo not reveal the instance behind the gateway. You needpkt-srcaddrandpkt-dstaddr, or a flow log on the workload's interface instead. - Rejected traffic is not billed transfer. Add
filter action = "ACCEPT"to default-format queries so blocked scans do not distort the ranking.
Exercise
A flow log on a NAT gateway's ENI uses the default format. The top talker is the gateway's own private IP talking to an S3 address range, but you cannot tell which instance is responsible. What do you change, and what fix does the finding point to?
Show answer
Recreate the flow log with a custom format that includes pkt-srcaddr and pkt-dstaddr (and optionally pkt-dst-aws-service), then group by pkt-srcaddr to find the instance behind the gateway. Because the destination is S3, the fix is a gateway VPC endpoint on the route table of that instance's subnet, which removes S3 traffic from the NAT gateway and its data processing charge.