Sub topic 4
Tracing traffic with VPC Flow Logs
Cost Explorer shows which usage type is expensive. It does not show which workloads cause it. VPC Flow Logs fill that gap by recording the network flows between addresses. This chapter shows how to find the top talkers, meaning the address pairs that move the most bytes.
This chapter assumes you have read the previous chapter on Cost Explorer. It also assumes you can read an IP address, a CIDR range, and a route table, and that you can run AWS CLI commands.
What a flow log record contains
A flow log is a set of records, each summarizing traffic between two endpoints during an aggregation interval of 1 or 10 minutes. The default format has these fields:
srcAddranddstAddr: the source and destination IP addresses.srcPort,dstPort, andprotocol: which service was used.packetsandbytes: the volume in the interval.interfaceId: the network interface that saw the traffic.action:ACCEPTorREJECT.
Bytes are the useful field for cost work, because AWS bills transfer by the gigabyte.
Turn on flow logs
Publish to CloudWatch Logs so you can query with Logs Insights. Attach the flow log to the VPC, or to one subnet or interface if you want a narrower and cheaper capture.
aws ec2 create-flow-logs \
--resource-type VPC \
--resource-ids vpc-0123456789abcdef0 \
--traffic-type ALL \
--log-destination-type cloud-watch-logs \
--log-group-name /vpc/flow-logs \
--deliver-logs-permission-arn arn:aws:iam::111122223333:role/flow-logs-role \
--max-aggregation-interval 60The IAM role must allow the flow logs service to write to the log group. Flow logs only record traffic from the moment you create them, so allow a few hours or a full business day before you analyze.
Find the top talkers
Open the log group in CloudWatch Logs Insights, pick a time range, and run this query:
stats sum(bytes) as bytesTransferred by srcAddr, dstAddr
| sort bytesTransferred desc
| limit 10The result is the ten address pairs that moved the most bytes. Convert bytes to gigabytes and multiply by the per-GB rate for the matching usage type to estimate the cost of each pair.
Read the results
An address pair alone does not say what it costs. Classify each pair:
| Pattern | Likely meaning | Next step |
|---|---|---|
| Private to private, different subnets | Possible inter-AZ traffic | Compare subnet AZs |
| Private source to a public address, seen on the NAT gateway interface | NAT data processing | Look for S3 or DynamoDB destinations |
| Instance to a public address, seen on the instance interface | Internet egress | Consider CloudFront or compression |
To attribute NAT traffic, filter on the NAT gateway's network interface. Then look up the private source addresses to find which instances are responsible.
filter interfaceId = "eni-0123456789abcdef0"
| stats sum(bytes) as bytesTransferred by srcAddr, dstAddr
| sort bytesTransferred desc
| limit 10Limits to keep in mind
- Flow logs cost money too. CloudWatch Logs charges for ingestion, so enable them on the VPC or subnet you are investigating, then remove them when done. Sending logs to S3 is usually cheaper for long-term retention.
- Flow logs show IP addresses, not service names. You must map addresses to instances, load balancers, or AWS service ranges yourself.
- Some traffic is not recorded, such as DHCP traffic and requests to the Amazon DNS server.
- A single time window can mislead. Run the query over a representative period, such as a full week, before you act.
Exercise
A Logs Insights query on the NAT gateway interface shows one private instance sending many gigabytes to a public address. What should you check next to decide whether a gateway VPC endpoint could remove that cost?
Show answer
Check whether the destination addresses belong to Amazon S3 or DynamoDB, for example by comparing them with the published AWS IP ranges. If they do, a gateway endpoint in the instance's route table sends that traffic around the NAT gateway, so it incurs no NAT data processing charge.