Skip to content

AWS WAF with CloudFront: best practices to protect web applications

How to position the WAF at the edge, tune rules without blocking legitimate users, and keep the origin from being bypassed.

Reading time
7 min
Published

Any application exposed to the internet receives automated traffic all the time (scans, exploitation attempts, abuse). AWS WAF, placed together with CloudFront, filters that traffic at the edge, before it reaches your origin. Turning on the WAF is easy. The real work is tuning it to stop real attacks without blocking legitimate customers.

With WAF and CloudFront together, that tuning comes down to a few practical decisions: which rules to use, how to reduce false positives with scope-down statements, when Bot Control is worth it, and how to introduce blocking without knocking anyone offline. You also need to protect the origin against bypass and use the logs for ongoing tuning.

Managed rules and count mode

Start with the AWS managed rule groups, which cover common categories such as malicious input, IP reputation, and OWASP patterns. Before blocking anything, keep the rules in count mode and measure the impact on your real traffic. This step avoids the classic mistake of deploying aggressive blocks and locking out legitimate users on day one.

With the counts in hand, promote the rules with a low false positive rate to block. Those that catch legitimate traffic stay in count mode or get adjusted. Plan for this adjustment (the tuning) as a recurring task that repeats for as long as the application is online.

Sometimes a managed rule catches the attack but also hits a legitimate route. In that case, avoid turning it off entirely. Use a scope-down statement to apply it only where it makes sense, or disable just the specific rule inside the group, by its label. That way you fix the problem on that path without giving up protection on the rest of the application.

Rate limiting and login protection

Rate-based rules limit how many requests a single source can make within a time window, which holds back brute force and abuse. On authentication pages, combine rate limiting with stricter rules and, where available, with challenges. Always watch the effect on real users, because in a company many people reach the internet through the same corporate IP.

Geoblocking can reduce noise when your audience is regional, but use it carefully, because legitimate users travel and use VPNs. Think of geoblocking as a way to reduce the surface, not as a definitive security control.

Bots and credential abuse

For more sophisticated automated traffic, AWS WAF offers paid managed rule groups. Bot Control sorts bots by category, and the account takeover prevention group watches login and sign-up patterns to stop credential stuffing. They pay off for applications that suffer authentication abuse, but they are billed by volume and need careful calibration. Here too, it is worth starting in count mode and measuring the effect on real users before blocking.

Origin protection and logs

Filtering at the edge is pointless if the origin accepts direct connections that go around CloudFront and the WAF. With an S3 origin, use Origin Access Control: the bucket stays private and only accepts access through the distribution. With an ALB or your own server as the origin, validate at the origin a secret header that only CloudFront sends, and restrict the security group to the CloudFront managed prefix list. Without that, an attacker only has to discover the origin address to skip the edge.

Enable WAF logging and send the logs for analysis. They show which rules are acting, which requests were blocked, and where false positives occur. You tune the rules based on them, and they are what helps the investigation when an attack gets through.

Practical checklist

  • Associate the WAF with the CloudFront distribution in front of the application
  • Enable the managed rules that fit the application stack
  • Deploy new rules in count mode first and measure the impact
  • Promote to block only the rules with few false positives
  • Apply rate limiting to sensitive endpoints and login endpoints
  • Reduce false positives with scope-down statements and label-based tuning instead of turning off entire rules
  • Evaluate Bot Control and account takeover prevention where there is authentication abuse
  • Evaluate geoblocking according to your audience, without treating it as the only control
  • Lock down the origin with OAC on S3, or with a secret header and the prefix list on the ALB
  • Enable WAF logs and send them for analysis
  • Review the rules and the false positives from time to time
  • Document exceptions and custom rules

Best practices

  • Treat WAF tuning as an ongoing process that repeats for as long as the application is online
  • Use count mode before any new block
  • Protect the origin against direct access that skips the edge
  • Combine rate limiting with specific rules on the login page
  • Track blocking metrics to detect attack campaigns
  • Remember that the WAF mitigates attacks, but the flaw in the application stays there until it is fixed

Common mistakes

Deploying blocks without a count phase
Legitimate users get blocked and the WAF loses credibility inside the company.
Origin that accepts direct connections
The attacker bypasses CloudFront and the WAF and goes straight for the origin.
Geoblocking treated as definitive security
It gives a false sense of protection and blocks legitimate users who are traveling or behind a VPN.
WAF without logs
There is no way to tune it or to investigate what was blocked and what got through.
Trusting the WAF and ignoring the application
Application flaws stay exploitable whenever a request gets past the edge.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

Does AWS WAF replace fixing vulnerabilities in the application?
No. The WAF mitigates attacks at the edge and reduces noise, but if the flaw exists, the application is still vulnerable. All it takes is one malicious request that matches no rule for it to go straight through. The WAF adds to application fixes and penetration testing, and neither of them becomes unnecessary because of it.
Why use the WAF with CloudFront instead of only at the origin?
On CloudFront, the WAF filters at the edge of the AWS network, closer to the user, and malicious traffic is blocked before it reaches your infrastructure. That reduces the load on the origin and improves latency. The one precaution you cannot skip is making the origin accept only traffic that comes from CloudFront, or the edge can be bypassed.
How do I avoid blocking legitimate users?
Deploy every new rule in count mode, analyze the logs to measure the impact on real traffic, and promote to block only what has a low false positive rate. On corporate endpoints, remember that many users go out through the same IP, and that affects rate-based rules.
Is geoblocking a good security strategy?
It helps reduce the surface when your audience is regional, but it does not work as a definitive control. Legitimate users travel and use VPNs, and attackers can use infrastructure in the allowed country. Treat geoblocking as one more layer, alongside content rules and rate limiting.
What is the difference between AWS WAF and AWS Shield?
They work at different layers. The WAF filters at the application layer and inspects the content of each HTTP request. Shield protects against denial of service attacks, mainly at the network and transport layers. Shield Standard comes included and mitigates the most common volumetric attacks, while Shield Advanced adds protection and support for larger scenarios. For an exposed application, it makes sense to have both.
Is AWS WAF Bot Control worth it?
It is worth it when the application suffers automated abuse that basic rules and rate limiting cannot hold back, such as aggressive scraping or credential stuffing. It is a paid rule group, billed by volume, so compare the cost with the size of the real problem. Start in count mode to see how much traffic it would classify as bots. This tells you, before anything is blocked, whether it will stop legitimate integrations or search engines.

When to bring in specialist help

Tuning the WAF and edge protection without blocking real users requires measuring the traffic and calibrating the rules methodically. GUARDIASEC does this work in its WAF and edge protection service: rules go into observation first, tuning is based on measured traffic, and the origin is protected against direct access.