Sub topic 11
Observing traffic with VPC Flow Logs
This chapter assumes you can read a basic security group and route table setup from earlier chapters. You should also be comfortable with TCP ports and running AWS CLI commands. By the end you can enable flow logs, read a record, and use the logs to tell a security group block from a network ACL block.
What flow logs record
VPC Flow Logs capture metadata about IP traffic going to and from network interfaces (ENIs) in your VPC. A record describes a flow: source and destination addresses and ports, protocol, packet and byte counts, a time window, and whether the traffic was accepted or rejected. Flow logs never contain packet payloads. For payload inspection you need Traffic Mirroring or a host-level tool.
You can create a flow log at three scopes:
- A VPC, which covers every ENI in every subnet.
- A subnet, which covers every ENI in that subnet.
- A single ENI.
Creating a flow log at the VPC level is the simplest way to get full coverage, and new ENIs are included automatically.
Flow logs are collected outside the data path. They do not add latency and do not reduce network throughput. Records are not real time: the log is aggregated over an interval, 10 minutes by default or 1 minute if you ask for it, and then published.
Choosing a destination
| Destination | Good for | Trade-off |
|---|---|---|
| Amazon S3 | Long-term retention, Athena queries, low storage cost | Not searchable until you query it with another tool |
| CloudWatch Logs | Interactive searching, metric filters, alarms | Higher ingestion and storage cost at volume |
| Data Firehose | Streaming to other systems or a SIEM | You operate the downstream pipeline |
For most teams, S3 plus Athena is the cheapest option for audit data. CloudWatch Logs suits a smaller set of high-value flow logs that you want to alarm on.
Enabling flow logs
This command turns on flow logs for the whole VPC and delivers records for accepted and rejected traffic to an S3 bucket you own.
aws ec2 create-flow-logs \
--resource-type VPC \
--resource-ids "$VPC_ID" \
--traffic-type ALL \
--log-destination-type s3 \
--log-destination arn:aws:s3:::my-flow-log-bucket--traffic-type accepts ACCEPT, REJECT, or ALL. Logging only REJECT is cheaper and is often enough for security monitoring. It hides the normal traffic you need when you troubleshoot a connection that is working only partly.
Reading a record
With the default format, each record is one line of space-separated fields:
2 123456789012 eni-0a1b2c3d4e5f6a7b8 203.0.113.12 10.0.1.25 44321 22 6 10 840 1764181800 1764181860 REJECT OK| Position | Field | Value above |
|---|---|---|
| 1 | version | 2 |
| 2 | account-id | 123456789012 |
| 3 | interface-id | eni-0a1b2c3d4e5f6a7b8 |
| 4-5 | srcaddr, dstaddr | 203.0.113.12 to 10.0.1.25 |
| 6-7 | srcport, dstport | 44321 to 22 |
| 8 | protocol | 6 (TCP; 17 is UDP, 1 is ICMP) |
| 9-10 | packets, bytes | 10, 840 |
| 11-12 | start, end | Unix timestamps of the window |
| 13 | action | REJECT |
| 14 | log-status | OK |
This record says that during a one-minute window, a host at 203.0.113.12 sent 10 packets to port 22 on an instance in your VPC, and they were rejected. That is typical internet SSH scanning against an instance that should not allow SSH from the internet.
log-status is OK, NODATA (no traffic in the interval), or SKIPDATA (some records were lost, for example from an internal capacity limit).
Custom formats
The default format has no VPC, subnet, or instance IDs, so you have to map ENIs back to resources yourself. A custom format adds them. It also adds fields that matter when a NAT gateway is involved.
aws ec2 create-flow-logs \
--resource-type VPC \
--resource-ids "$VPC_ID" \
--traffic-type ALL \
--log-destination-type s3 \
--log-destination arn:aws:s3:::my-flow-log-bucket \
--max-aggregation-interval 60 \
--log-format '${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${srcport} ${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status} ${vpc-id} ${subnet-id} ${instance-id} ${pkt-srcaddr} ${pkt-dstaddr} ${flow-direction} ${traffic-path}'The single quotes stop the shell from expanding ${...}. Three of these fields deserve explanation:
pkt-srcaddrandpkt-dstaddrhold the original packet addresses. On a NAT gateway ENI,srcaddris the gateway's own address whilepkt-srcaddrshows which private instance sent the traffic. Without them you cannot tell which instance is generating NAT traffic.flow-directionisingressoregressfrom the ENI's point of view.traffic-pathshows how egress traffic left the VPC, such as through an internet gateway, a gateway endpoint, or a peering connection.
You cannot edit a flow log after you create it. To change the format, create a new flow log and delete the old one. Both can exist for a while, and during that time you pay for both.
Using the logs to troubleshoot
Once logs are in S3, objects are compressed under a path like AWSLogs/<account-id>/vpcflowlogs/<region>/<yyyy>/<mm>/<dd>/. For a quick look at a downloaded default-format file, awk can filter on the action field:
gunzip -c flow-log-file.log.gz | awk '$13 == "REJECT" && $7 == 22 {print $4, $5, $7}'For CloudWatch Logs, this Logs Insights query lists the top sources of rejected traffic:
fields @timestamp, srcAddr, dstAddr, dstPort, action
| filter action = "REJECT"
| stats count(*) as attempts by srcAddr, dstPort
| sort attempts desc
| limit 10Telling a security group block from a NACL block
A REJECT record does not say which control rejected the traffic. You infer it from the pattern:
- Security groups are stateful. If a security group allows the inbound request, the reply is allowed automatically, so you will not see a
REJECTfor the reply. - Network ACLs are stateless. If the inbound request is
ACCEPTbut the reply, going the other way on the ephemeral port, isREJECT, a NACL is missing an outbound rule. - If you see only a
REJECTfor the inbound request and nothing else, either control could be responsible. Check the security group first, then the NACL.
Checking a path is working
A successful TCP connection shows ACCEPT records in both directions. If you see the inbound ACCEPT but no return flow at all, the problem is likely routing or the application, not a firewall. Also check that asymmetric routing is not sending replies out a different interface.
Limits and gotchas
Not all traffic is logged. These are excluded:
- DNS queries to the Amazon-provided resolver. Queries to your own DNS server are logged.
- DHCP traffic.
- Instance metadata requests to 169.254.169.254.
- Amazon Time Sync Service traffic.
- Traffic to the reserved address of the VPC router.
Other points to remember:
- Because of aggregation, a long-lived connection can appear as several records, one per interval. Sum bytes by connection when you need totals.
- Flow logs describe what happened at the ENI. A
REJECTrecord means the traffic reached the ENI boundary and was blocked, and it does not prove a client tried only once. - Volume grows quickly in busy VPCs. Cost comes from log ingestion, storage, and queries, so consider
REJECT-only logs, a longer aggregation interval, or flow logs on specific subnets for noisy workloads. - Flow logs on a peered VPC capture only traffic for ENIs in your own account's VPC. You cannot see ENIs in the peer's account.
Exercise
A flow log shows an inbound record from 198.51.100.7 to port 443 on your web instance with ACCEPT, followed by a record from the instance to 198.51.100.7 on port 51200 with REJECT. The security group allows inbound 443. What is the most likely cause, and why?
Show answer
A network ACL on the subnet is blocking the outbound reply. Security groups are stateful and would allow the reply automatically, while NACLs are stateless and need an explicit outbound rule for the ephemeral port range (1024-65535). The inbound ACCEPT shows the request got through, and the outbound REJECT shows the response was blocked.
Exercise
You enable flow logs on a NAT gateway's subnet and see thousands of records with the NAT gateway's private IP as srcaddr. How do you find out which instance in the private subnet is generating that traffic?
Show answer
Use a custom log format that includes pkt-srcaddr and pkt-dstaddr. On the NAT gateway ENI, pkt-srcaddr holds the original private instance address, so you can group by it to find the source.