- Newest
- Most votes
- Most comments
Your approach of configuring WAF rules on the ALB when using CloudFront is not the most effective strategy. When you use CloudFront in front of your ALB, all traffic to the ALB will indeed appear to come from CloudFront's IP addresses. This means that any IP whitelisting rules you try to implement on the ALB level will not work as expected, as they will only see CloudFront's IPs rather than the original client IPs.
The recommended approach is to configure AWS WAF on CloudFront instead of the ALB. This is because CloudFront acts as the first point of contact for incoming requests and has access to the original client IP addresses. By setting up WAF rules, including IP whitelisting, on CloudFront, you can effectively control and filter traffic before it reaches your ALB.
CloudFront integrates tightly with AWS WAF, allowing you to create a web ACL (Access Control List) and associate it with your CloudFront distribution. This setup enables you to implement various security rules, including IP whitelisting, at the edge locations. CloudFront will then evaluate every HTTP request against your configured WAF rules within milliseconds, as it runs on the same physical edge hosts.
To implement this:
- Create a web ACL in AWS WAF.
- Configure your desired rules, including IP whitelisting, in the web ACL.
- Associate the web ACL with your CloudFront distribution.
This approach not only provides more accurate and effective traffic filtering but also helps reduce the load on your ALB by blocking unwanted traffic at the edge. Additionally, you can still maintain other security measures at the ALB level if needed, but for IP-based rules and general web application firewall functionality, implementing WAF at the CloudFront level is the most efficient and recommended solution.
Sources
Amazon CloudFront, AWS WAF, and ALB - Prebid Server Deployment on AWS
Introducing CloudFront Security Dashboard, a Unified CDN and Security Experience | Networking & Content Delivery
Accelerate and protect your websites using Amazon CloudFront and AWS WAF | Networking & Content Delivery
Using AWS WAF with Amazon CloudFront - AWS WAF, AWS Firewall Manager, and AWS Shield Advanced
In addition to the AI-generated comments, you should additionally restrict access to your ALB to ensure all inbound requests to come via CloudFront, and are therefore subject to your WAF WebACL.
I recommend using the managed IP Prefix List in a security group to achieve this, as outlined in this blog post: https://aws.amazon.com/blogs/networking-and-content-delivery/limit-access-to-your-origins-using-the-aws-managed-prefix-list-for-amazon-cloudfront/
That will limit access to your ALB to only CloudFront IP addresses. However, you may wish to go a step further and restrict access to only your CloudFront distributions. For this, you would add a secret header, as per the documentation here: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/restrict-access-to-load-balancer.html
Hello,
IP whitelisting rules on your Application Load Balancer (ALB) are not working when CloudFront is in front of it.
The simplest solution is:
Configure WAF on CloudFront instead of ALB.
Here's why this works:
- CloudFront is your first point of contact for incoming traffic.
- WAF on CloudFront can see the original client IPs, unlike the ALB which only sees CloudFront IPs.
How to implement this:
- Remove the WAF association from your ALB.
- Create a new WAF web ACL for CloudFront.
- Add your IP whitelisting rules to this new web ACL.
- Associate the web ACL with your CloudFront distribution.
For a step-by-step guide, refer to AWS documentation: "Adding a Web ACL to a CloudFront distribution":
The best solution in your case is to configure the security group of the ALB to permit only tcp/443 from the managed prefix list that VPC contains for the "origin-facing" servers of CloudFront. https://aws.amazon.com/blogs/networking-and-content-delivery/limit-access-to-your-origins-using-the-aws-managed-prefix-list-for-amazon-cloudfront/
If you open the VPC console in the same region where your ALB resides and click "Managed prefix lists", you'll find an AWS-provided prefix list there with the name com.amazonaws.global.cloudfront.origin-facing. Permit traffic to the ALB only from this prefix list, and apply the IP-based access restrictions in the WAF web ACL attached to your CloudFront distribution.
You can further enhance the isolation by having CloudFront add a secret, random HTTP header to the requests it sends to your ALB origin, and configuring the ALB's listener rules to allow access only when the request arrives with that custom header. This prevents others from pointing their CloudFront distributions to your ALB, although there are other defences against that, such as CloudFront having to have a public TLS certificate attached that matches the origin's domain name. Specifics on adding the custom header in CloudFront are here: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/add-origin-custom-headers.html
Relevant content
asked 3 years ago
asked 4 years ago

As our ALB is still public, anyone can call the ALB and bypass the WAF rules in Cloudfront