Sub topic 11
Observing traffic with VPC Flow Logs
This chapter assumes you know how security groups and network ACLs filter traffic, and that you can run AWS CLI commands. It also assumes the VPC_ID variable from earlier chapters is still set.
What flow logs capture
VPC Flow Logs record metadata about IP traffic going to and from network interfaces in your VPC. They do not record packet contents. Each record summarizes one flow, meaning traffic with the same source, destination, ports, and protocol during a capture window (by default about 10 minutes of aggregation).
You can enable flow logs at three levels:
- A VPC, which covers every network interface in it.
- A subnet, which covers the interfaces in that subnet.
- A single network interface.
Records can be delivered to CloudWatch Logs, S3, or Amazon Data Firehose. Enabling flow logs does not affect network throughput or latency.
Enabling flow logs
This command turns on flow logs for the whole VPC. It records both accepted and rejected traffic and delivers the records 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-bucketThe --traffic-type option accepts ACCEPT, REJECT, or ALL. Use REJECT alone if you only want to troubleshoot blocked traffic and keep log volume low.
Reading a record
With the default format, each record is one line of space-separated fields:
2 123456789012 eni-0a1b2c3d4e5f67890 203.0.113.12 10.0.1.25 44321 22 6 20 4249 1700000000 1700000060 REJECT OK| Field | Example | Meaning |
|---|---|---|
| version | 2 | Flow log format version |
| account-id | 123456789012 | Owner of the interface |
| interface-id | eni-0a1b... | Network interface that saw the traffic |
| srcaddr / dstaddr | 203.0.113.12 / 10.0.1.25 | Source and destination IP addresses |
| srcport / dstport | 44321 / 22 | Source and destination ports |
| protocol | 6 | IANA protocol number (6 is TCP, 17 is UDP, 1 is ICMP) |
| packets / bytes | 20 / 4249 | Volume in the capture window |
| start / end | 1700000000 / 1700000060 | Window times in Unix seconds |
| action | REJECT | ACCEPT or REJECT |
| log-status | OK | OK, NODATA, or SKIPDATA |
This example shows an outside host trying to reach port 22 on an instance and being rejected.
Using flow logs to troubleshoot
The ACCEPT and REJECT action tells you that traffic was allowed or blocked, but not which control blocked it. Combine the record with what you know about the stateful and stateless rules:
- Find the record for the failing connection by interface, address, and port.
- If the request shows
REJECT, a security group or network ACL denied it. - If the request shows
ACCEPTbut the reply showsREJECT, suspect a network ACL. Security groups are stateful and allow replies automatically, while network ACLs are stateless and need an explicit rule for return traffic. - If you see no record at all, the traffic never reached the interface. Check route tables and gateways.
Common pitfalls
- Missing traffic. Flow logs do not capture DNS queries to the Amazon-provided resolver, DHCP traffic, requests to the instance metadata service, or traffic to the reserved addresses of the subnet.
- Cost. Logging
ALLtraffic for a busy VPC produces a lot of data. Start withREJECTor a narrower scope, and set an S3 lifecycle rule. - Bucket permissions. The destination must allow delivery. If no records arrive, check the bucket policy.
- Hard to read. Raw S3 files are awkward to scan. Query them with Amazon Athena, or use CloudWatch Logs Insights when you deliver to CloudWatch Logs.
Exercise
A client reports that connections to your web server on port 443 hang. The flow log shows an ACCEPT record for the inbound request, followed by a REJECT record for the outbound reply on a high-numbered port. Which control is the most likely cause, and why?
Show answer
A network ACL on the subnet. Security groups are stateful, so they allow reply traffic automatically. Network ACLs are stateless, so the outbound rule must explicitly allow the ephemeral port range used by the reply. The REJECT on the reply points to a missing or too-narrow outbound NACL rule.
Exercise
You want to troubleshoot blocked connections in a busy VPC while keeping log volume and cost low. Which option do you change in the create-flow-logs command, and to what value?
Show answer
Set --traffic-type to REJECT, so only rejected traffic is recorded. You can also narrow the scope by enabling the flow log on a single subnet or network interface instead of the whole VPC.