Secure a load balancer: WAF vs custom header

Hello, the awesome community!

I’m working on securing my production services hidden behind public ALB. I want to make them available only for my CloudFront distribution. I see two options: applying a custom secret header and using WAF. What option would you choose and why? What works the best from your experience?

Any ideas and advice are appreciated! Thank you in advance!

So the ALB is public but should not be used unless from the CloudFront?

Yes, exactly. Cloudfront is forwarding /api routes to the ALB which has to be public. But I don’t want it to be accessible by the direct link. This makes sense, right?

If ALB is an origin to cloudfront then 100% do the custom header and use WAF to block all traffic without the header. You can also set up automating rotating the header with lambda.

Not sure why you put the cloudfront before the ALB though

this is a more complex solution with security groups: https://aws.amazon.com/blogs/security/automatically-update-security-groups-for-amazon-cloudfront-ip-ranges-using-aws-lambda/

but i think the secret header is probably the easiest solution. not sure about waf havent tried that yet.

https://aws.amazon.com/blogs/security/automatically-update-security-groups-for-amazon-cloudfront-ip-ranges-using-aws-lambda/

This looks like a solid solution, thanks for sharing. But I think it’s too complicated for me on the current stage.

We have quite a heavy routing on the CDN level, so it’s just simply convenient to not link services directly to the ALB

Why make it publically accessible? Curious. By direct link I’m assuming you mean the public IP address?

Thank you for the reply. But I assume that I can block the traffic that doesn’t contain the header even without WAF? Just by applying a rule to the ALB listener. From what I see I should either use WAF to restrict the access (not by the header) or use the ALB listener rule to allow only traffic with the header. Here is the example https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/restrict-access-to-load-balancer.html#restrict-alb-route-based-on-header

Yeah, this is the way to go.

When you use origin access identities with S3 it is exactly this.

Either way, there are official docs in exactly what you’re after, which you linked above. Just follow that.

Yeah you can do it with routing rules as well.

We have quite a heavy routing on the CDN level

Not sure what you mean there? If you want certain resources from the application to be stored in the CDN (e.g. static assets) you can also point your CDN to the ALB for those paths only, people go to the ALB but the website that is delivered points to the CDN. Or are you caching Responses from the ALB, so lots of responses that are not targeted at specific users but general and therefore cacheable?

In order to make the ALB accessible from CloudFront it must be public (all the CloudFront origins have to be public). And it’s not necessarily an IP address. In my case I have a domain in a public Route 53 hosted zone, which forwards requests to the ALB’s domain, which is also publicly available.

No, I’m not really caching any responses from ALB. I have some CDN paths pointing to the public domain, which is forwarding requests to the ALB’s domain. My goal is to make paths like `api/*` routing to the instance behind the ALB. This is works already, I just want to secure this a bit and prevent direct access to the ALB’s domain.

The alb itself also supports path based routing, so you could achieve that there as well. Essentially if you’re not caching anything I’m not sure putting a CloudFront in front of the ALB is the optimal solution as the routing layer should be achievable in ALB itself as well.

But for securing it as others have pointed out a combo of a header and WAF is probably the best idea. You can add the listener rule with the header of course as well, but the first two are imho more important.

I’m with you on this. I wish something other than CF had stronger routing capabilities when you separate your server from your static assets.

Putting CF in front of an ALB can offer significant performance improvements for users who are in a different region than the ALB, especially if you tweak the CloudFront idle timeout settings, even when not caching anything.

In the case of a DDoS/attack, WAF attached to CloudFront will be better able to absorb that traffic than an ALB could (though an ALB is still vulnerable if the attacker hits the ALB IPs/DNS name directly). When we spoke to AWS, they recommended CF in front of our ALBs, with a “secret” header, for this reason.

Having CloudFront functions/Lambda@Edge can also be handy.

It can improve performance by terminating tls at the edge location instead of at the alb. Also, the the new changes to data transfer pricing, you get 1tb of data out through cloud front vs only 100gb through alb.

This is the aws supported method and is robust once set up don’t be shy of it.