Skip to Content

Blocking an IP address

Where to block a client IP address on AWS - network ACLs, security groups, firewall software, an ALB or NLB, AWS WAF IP sets and CloudFront geo restriction - and why a NACL stops helping behind CloudFront.

The available layers

Where to block an IP address: AWS WAF IP set rules and CloudFront geo restriction act at the edge; in the VPC, the network ACL of the public subnet can deny an IP, but behind CloudFront it only sees CloudFront IPs; the ALB has its own security group, and the EC2 instances in the private subnet allow only the ALB security group Where to block an IP address: AWS WAF IP set rules and CloudFront geo restriction act at the edge; in the VPC, the network ACL of the public subnet can deny an IP, but behind CloudFront it only sees CloudFront IPs; the ALB has its own security group, and the EC2 instances in the private subnet allow only the ALB security group

Block the client where its address is still visible.

Start with a client reaching a public EC2 instance directly, then add each layer:

  1. Instance only.
    • The network ACL of the subnet can deny the IP: NACLs have deny rules.
    • The security group can't: it only has allow rules. It can allow-list a known set of IPs, but not block one.
    • Optional firewall software on the instance can also block it (costs CPU).
  2. With an ALB. The ALB terminates the connection, so the instances (now private, with an SG allowing only the ALB's SG) only see the ALB. Block the IP with the NACL of the ALB's subnets. The client's IP is in the X-Forwarded-For header for the app.
  3. With an NLB. Traffic passes through with the client IP preserved. Block with the NACL; NLBs also support security groups now.
  4. ALB + AWS WAF. Attach a web ACL to the ALB with an IP set rule (and rate-based or geo rules). WAF costs a little but filters at layer 7 with much more flexibility.
  5. CloudFront + WAF. With CloudFront in front, the ALB sees CloudFront's IPs, so a NACL can't block the client: it's not helpful here. Instead:
    • attach WAF to the CloudFront distribution with an IP set rule;
    • use CloudFront geo restriction to block whole countries;
    • let the ALB security group allow only CloudFront (the AWS-managed prefix list for CloudFront origin-facing IPs).

Updated since the slides

CloudFront VPC origins let the ALB, NLB or EC2 instances sit in private subnets with no public entry at all, so CloudFront (with WAF) becomes the only way in. See CloudFront VPC origins.

Real-world examples

  • A ticket site blocks a scraper's IP range with a WAF IP set on its CloudFront distribution and adds a rate-based rule of 2,000 requests per 5 minutes per IP.

Exam tips

  • "Block a specific IP address at the subnet level" → NACL deny rule.
  • "Security groups can deny an IP" → false: SGs only allow.
  • "Block IPs with CloudFront in front" → WAF on CloudFront (IP set); NACLs only see CloudFront IPs.
  • "Block a country" → CloudFront geo restriction or a WAF geo match rule.

Common traps

  • "Add the attacker's IP to the ALB's NACL when CloudFront is in front." The NACL sees CloudFront edge IPs, not the client. Use WAF on CloudFront.
  • "Remove the IP from the security group to block it." If the SG allows 0.0.0.0/0, everyone is allowed; SGs can't express "everyone but this IP".

Check yourself

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt