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!
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 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.
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.