Restricting access to CloudFront by IP
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The standard way to restrict access to a CloudFront distribution by IP is to attach AWS WAF to the distribution and use an IP set rule. That approach is simpler and safer than trying to build the logic into your origin because CloudFront can reject requests before they reach the backend.
Use AWS WAF as the Primary Control
CloudFront itself does not have a first-class "allow only these IPs" setting in the distribution behavior UI. The normal solution is:
- create an IP set in AWS WAF
- create a web ACL that uses that IP set
- associate the web ACL with the CloudFront distribution
You can use this for either:
- an allowlist
- a blocklist
For security-sensitive distributions, an allowlist is usually easier to reason about.
Example WAF Rule in CloudFormation
With DefaultAction set to block, only the IPs in the set are allowed.
Associate the ACL with CloudFront
Once the web ACL exists, attach it to the distribution. In infrastructure-as-code, keep that association in the same stack or a clearly related stack so the security boundary is not managed manually.
The key operational point is that WAF evaluates the viewer IP before the request reaches the origin. That reduces unnecessary load and keeps the access rule close to the edge.
When CloudFront Functions or Lambda at Edge Make Sense
You can implement IP checks in CloudFront Functions or Lambda at Edge, but that should usually be the fallback, not the first option. WAF is better when the rule is simply "allow or block by IP range."
Custom code is more appropriate when:
- the decision depends on headers and IP together
- the rule must transform the response
- you need custom logging or special challenge behavior
For plain IP restriction, WAF is less error-prone.
Be Careful with Proxies and Corporate Networks
An IP allowlist is only as stable as the client network. This works well for office networks, VPN egress ranges, or fixed partner infrastructure. It works poorly for users on consumer mobile networks or dynamic residential IPs.
Before adopting IP restriction, confirm:
- which IP ranges are actually stable
- how often they change
- who owns updates when the ranges change
If that ownership is unclear, the control will drift and break access unexpectedly.
Test with a Block Default
A good rollout pattern is:
- create the WAF rule in count or monitor mode if needed
- verify sampled requests and logs
- switch to allowlist enforcement
For a production cutover, test from:
- an allowed IP
- a blocked IP
- a CDN cache hit path
- a cache miss path
That gives confidence that the restriction is applied consistently.
Common Pitfalls
The biggest mistake is implementing IP filtering only at the origin and forgetting that CloudFront still accepts and forwards unwanted traffic. WAF is a better place for edge access control.
Another issue is allowlisting unstable client IPs. That makes the system look randomly broken from the user's point of view.
A third problem is using custom edge code for simple allowlist logic that WAF already handles more cleanly.
Summary
- Use AWS WAF with an IP set to restrict CloudFront access by IP.
- Attach the WAF web ACL directly to the distribution.
- Prefer an allowlist when access should be limited to known networks.
- Use edge code only when the decision logic is more complex than IP matching.
- Make sure the allowed IP ranges are actually stable and operationally maintained.
Related reading
- Retrieve all items from DynamoDB using query?
- Retrieve multiple messages from SQS
- Retrieve S3 file as Object instead of downloading to absolute system path
- Retrieve the full name of a service in Kubernetes
- Return collection as read-only
- Rights to read /dev/tty0 from pod
- Retrieving All items in a table with DynamoDB
- Retrieving subfolders names in S3 bucket from Boto3

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.