VPC Flow Logs and Network Monitoring
Overview
VPC Flow Logs record metadata about IP traffic going to and from network interfaces: source and destination addresses, ports, protocol, bytes and whether the traffic was ACCEPTED or REJECTED. They are your CCTV for the network – not the audio (packet contents), but a log of every comer and goer.
Flow logs can be created for a VPC, subnet or individual ENI and delivered to CloudWatch Logs, S3 or Kinesis Data Firehose. They answer questions such as "who is hitting this port?", "which rule is rejecting my traffic?" and "which instance transfers the most data?".
Alongside Flow Logs, AWS provides Traffic Mirroring for full packet capture, Network Access Analyzer for unintended access, CloudWatch Network Monitor / Internet Monitor for performance, and load balancer access logs.
Key Concepts
- Flow log records are not real-time: capture windows are 1 or 10 minutes (aggregation interval); they do not capture packet contents.
- Flow logs do not capture traffic to the Amazon DNS server, DHCP, instance metadata (169.254.169.254), Windows license activation or Time Sync.
- Fields: version, account-id, interface-id, srcaddr, dstaddr, srcport, dstport, protocol, packets, bytes, start, end, action, log-status (plus custom fields: vpc-id, subnet-id, pkt-srcaddr, flow-direction, tcp-flags).
- REJECT indicates a security group or NACL denial; because SGs are stateful, a REJECT on return traffic hints at NACL.
- Destinations: CloudWatch Logs (Insights queries, metric filters), S3 (Athena, cheaper at scale), Firehose (streaming).
- A flow log cannot be modified after creation (create a new one); it applies to existing and new interfaces in the resource.
- Traffic Mirroring copies packets from ENIs (Nitro instances) to NLB/ENI targets for IDS and forensic analysis.
- Route 53 Resolver query logs, ALB access logs and API Gateway logs complement flow logs.
Detailed Notes
Creating flow logs
aws ec2 create-flow-logs --resource-type VPC --resource-ids vpc-0abc \
--traffic-type ALL --log-destination-type s3 \
--log-destination arn:aws:s3:::flowlogs-bucket/prod/ \
--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} ${pkt-srcaddr} ${pkt-dstaddr} ${flow-direction}'
For CloudWatch Logs delivery you must supply an IAM role (--deliver-logs-permission-arn). For S3, the bucket policy must allow delivery.logs.amazonaws.com.
Reading a record
2 111122223333 eni-0a1b2c3d 198.51.100.7 10.0.1.25 44321 22 6 20 4249 1700000000 1700000060 REJECT OK
This reads: TCP (protocol 6) from 198.51.100.7 to port 22 on 10.0.1.25, 20 packets, REJECTed.
Analysis recipes
| Question | Approach |
|---|---|
| Top talkers | Athena / Logs Insights: sum bytes by srcaddr |
| Who is being rejected on SSH? | Filter dstport=22 and action=REJECT |
| Is traffic crossing AZs (cost)? | Use subnet-id/az-id fields with pkt-src/dst |
| NAT gateway cost spike | Log NAT ENI, group by destination |
| Confirm NACL vs SG block | Check whether inbound ACCEPT and outbound REJECT |
CloudWatch Logs Insights example:
fields @timestamp, srcAddr, dstAddr, dstPort, action
| filter action = "REJECT" and dstPort = 22
| stats count(*) as attempts by srcAddr
| sort attempts desc
| limit 10
Athena over S3:
SELECT srcaddr, dstaddr, dstport, SUM(bytes) AS total_bytes
FROM vpc_flow_logs
WHERE action = 'ACCEPT' AND day = '2025-03-01'
GROUP BY srcaddr, dstaddr, dstport
ORDER BY total_bytes DESC LIMIT 20;
Other monitoring tools
| Tool | Use |
|---|---|
| Traffic Mirroring | Packet-level inspection (IDS/IPS, troubleshooting protocols) |
| Network Access Analyzer | Find unintended paths (e.g. internet reachable DB) from scope definitions |
| CloudWatch Internet Monitor | Internet performance/availability between users and your apps |
| CloudWatch Network Monitor | Synthetic probes from VPC to on-prem over DX/VPN |
| Transit Gateway Network Manager | Global topology and event monitoring for TGW/SD-WAN/DX |
| ELB access logs | Per-request logs (client IP, latency, status) in S3 |
| Route 53 Resolver query logs | DNS queries from VPC resources |
Key CloudWatch network metrics
| Resource | Metrics |
|---|---|
| EC2 | NetworkIn/Out, NetworkPacketsIn/Out, ENA allowance exceeded (bw_in_allowance_exceeded) via ethtool/agent |
| NAT GW | ErrorPortAllocation, PacketsDropCount, BytesOutToDestination |
| VPN | TunnelState, TunnelDataIn/Out |
| Direct Connect | ConnectionState, ConnectionBpsEgress, ConnectionLightLevelTx |
| ALB | ActiveConnectionCount, TargetResponseTime, HTTPCode_Target_5XX_Count |
Limitations and gotchas
- Flow logs for a peered VPC only show traffic if you own the peer VPC; you see only your interfaces.
- Not all records appear immediately; missing data in CloudWatch may be due to IAM role or log group issues.
- Higher volume = higher cost; filter with traffic-type REJECT for security use or sample at subnet level.
- Use Parquet + Hive-compatible partitions in S3 for cheaper Athena queries.### Flow Log fields, Athena analysis and cost-aware egress
Default flow log records contain version, account, interface, srcaddr, dstaddr, ports, protocol, packets, bytes, start, end, action and log-status. Custom formats add vpc-id, subnet-id, pkt-srcaddr, flow-direction and tcp-flags. Flow logs do not capture DHCP, Amazon DNS server traffic, metadata (169.254.169.254) or license activation traffic.
| Destination | Best for |
|---|---|
| CloudWatch Logs | Metric filters and alerting |
| S3 | Cheap long-term storage plus Athena queries (Parquet saves cost) |
| Firehose | Streaming to analytics tools |
SELECT dstaddr, SUM(bytes) AS total
FROM vpc_flow_logs
WHERE action = 'ACCEPT' AND day = '2025/01/15'
GROUP BY dstaddr ORDER BY total DESC LIMIT 10;
| Egress pattern | Cost note |
|---|---|
| NAT gateway to S3/DynamoDB | Replace with gateway endpoints (free) |
| Cross-AZ traffic | Charged per GB; use one NAT per AZ and AZ-aware routing |
| Interface endpoints | Hourly plus per-GB charge, still cheaper than NAT for heavy AWS-API traffic |
| Data out to internet | Tiered per-GB; CloudFront often cheaper |
Flow logs can be created at VPC, subnet or ENI level and cannot be changed after creation (delete and recreate). Use REJECT filtered logs to find blocked traffic and verify security group versus NACL causes with the action and direction.
Real World Example
Scenario – unexpected NAT bill. Finance reports NAT data processing charges tripled. The engineer enables flow logs on the NAT gateway's ENI delivering to S3 and queries with Athena grouped by destination. 80% of traffic goes to S3 public endpoints from batch jobs in private subnets.
They add an S3 gateway endpoint, eliminating NAT processing for that traffic, and create a CloudWatch alarm on NAT BytesOutToDestination to detect regressions. A second flow log with REJECT only on the DMZ subnet feeds a metric filter that alerts on port scanning.
Architecture Diagram
- 1Flow logs capture metadata for the chosen VPC, subnet or ENI.
- 2Records are published every 1 or 10 minutes to CloudWatch Logs or S3.
- 3Insights queries and metric filters support near-real-time detection.
- 4Athena over S3 provides cost-effective historical analysis.
Best Practices
- ✓Enable flow logs at VPC level with the extended format; send long-term data to S3 (Parquet).
- ✓Alarm on REJECT spikes and unusual outbound volumes.
- ✓Use Traffic Mirroring only when packet payloads are needed; it is costlier.
- ✓Combine flow logs with Reachability Analyzer and Network Access Analyzer in runbooks.
- ✓Apply S3 lifecycle rules to flow log buckets to manage cost.
Common Interview Questions
Exam Tips
SOA-C03 focus
- Metadata about traffic with ACCEPT/REJECT -> VPC Flow Logs; packet capture -> Traffic Mirroring.
- Flow logs cannot be edited after creation; create a replacement.
- Cheap long-term network analytics -> S3 + Athena.
- Per-request web logs -> ALB access logs (S3); DNS query visibility -> Resolver query logging.
Finished this lesson?
0 of 46 lessons done · 0% of the course

