The Bot Mitigation Dilemma: Detect in App, Block at Edge
Bot mitigation presents a fundamental challenge: detection is easiest within the application, but enforcement is most efficient at the network edge. Applications, by their nature, possess the deep context needed to identify sophisticated malicious activity. Think of it like a security guard at a building's entrance (the edge) versus a detective inside the building (the application). The guard can stop anyone from entering, but the detective can piece together who is casing the joint based on their movements and interactions within the premises.
For example, an application can readily spot patterns like 30 failed login attempts within five minutes or API call sequences that no human user would logically follow. This level of granular insight is unavailable at the edge, which primarily sees network-level traffic. However, allowing malicious requests to reach the application wastes valuable compute resources, generates unnecessary logs, and increases the attack surface. Conversely, blocking at the edge, without application context, risks legitimate users being blocked by overly broad rules.
AWS is addressing this asymmetry with a new pattern that connects application detection directly to edge enforcement. By leveraging AWS WAF Dynamic Label Interpolation and Amazon CloudFront KeyValueStore (KVS), developers can create a powerful feedback loop. This system allows the application to signal malicious activity, and the edge (CloudFront) to act upon that signal by blocking future requests from the identified bad actors.
This approach effectively brings the concept of "fail2ban at the edge" to the AWS ecosystem, but with the added intelligence derived from application-level understanding. The goal is to drop malicious requests before they ever hit the origin servers, thus optimizing resource utilization and enhancing security posture.

Implementing the Feedback Loop: WAF Labels and KVS
The core of this solution lies in two AWS services: AWS Web Application Firewall (WAF) and Amazon CloudFront KeyValueStore (KVS). AWS WAF is a web application firewall that helps protect your web applications or APIs against common web exploits that could affect availability, compromise security, or consume excessive resources. CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency and high transfer speeds.
The key innovation here is AWS WAF's Dynamic Label Interpolation feature. This allows WAF rules to inject custom labels into requests that meet certain criteria. These labels act as signals. When a WAF rule detects suspicious activity within the application's context—perhaps through custom rules that inspect request bodies or headers indicating abuse—it can append a dynamic label like `aws:label:suspicious-actor` to the request.
These labels are then made available to CloudFront. CloudFront can be configured to read these dynamic labels. The critical step is using Amazon CloudFront KeyValueStore (KVS). KVS is a feature of CloudFront that allows you to store and retrieve key-value pairs at the edge. This means you can maintain a list of identifiers, such as IP addresses or user agent strings, associated with malicious activity directly within CloudFront's edge locations.
The feedback loop works as follows:
- Application Detects Malice: A WAF rule, potentially configured to inspect application-specific logic (e.g., login attempts, API call patterns), identifies a request as malicious.
- WAF Adds Dynamic Label: The WAF rule appends a dynamic label to the request, signaling the detected threat.
- CloudFront Reads Label: CloudFront receives the request and can inspect the dynamic labels added by WAF.
- KVS Update: If a specific label indicating a malicious actor is present, CloudFront can be configured to take an action. This action involves adding an identifier associated with the malicious request (e.g., the source IP address) to a KVS. This KVS effectively becomes a dynamic blocklist managed at the edge.
- Edge Enforcement: On subsequent requests, CloudFront checks the incoming request's identifier against the blocklist stored in KVS. If a match is found, CloudFront immediately blocks the request, preventing it from reaching the origin application.
Benefits of the Application-Edge Feedback Loop
This architecture offers several significant advantages:
- Resource Optimization: By blocking malicious requests at the edge, applications avoid processing them. This conserves compute resources, reduces database load, and minimizes log generation costs associated with handling unwanted traffic.
- Improved Security Posture: The ability to rapidly update blocklists based on real-time application insights means that emerging threats can be mitigated much faster than with traditional, manually updated rulesets.
- Reduced Latency for Legitimate Users: Malicious traffic is filtered out closer to the source, ensuring that legitimate user requests experience optimal performance without being slowed down by the processing of harmful requests.
- Scalability: Both AWS WAF and CloudFront are highly scalable services, designed to handle massive traffic volumes. This feedback loop inherits that scalability, making it suitable for applications with global user bases and high traffic demands.
- Operational Simplicity: While the initial setup requires careful configuration, the ongoing management of the blocklist is largely automated by the feedback loop, reducing the manual burden on security and operations teams.
The surprising detail here is not the existence of WAF or KVS, but how their integration, specifically through dynamic labels, finally bridges the gap between deep application context and immediate edge enforcement. It’s a sophisticated mechanism that transforms the application from a passive observer into an active participant in edge security.
Considerations for Implementation
Implementing this solution requires careful planning. Developers must first identify the specific application-level indicators of malicious behavior that warrant blocking. This might involve custom WAF rules that inspect request payloads, headers, or even look for specific sequences of API calls. The choice of identifier to store in KVS is also critical; IP addresses are common but can be spoofed or shared (e.g., via NAT or proxies), while other identifiers might be more specific but less universally applicable.
The integration between WAF and CloudFront needs to be correctly configured. This includes ensuring WAF is associated with the CloudFront distribution and that CloudFront is set up to inspect dynamic labels and query the KVS. The size limits and performance characteristics of KVS should also be considered, particularly for very large blocklists.
What nobody has addressed yet is the potential for this system to be manipulated. If an attacker can poison the KVS by triggering false positives in the application detection layer, they could potentially block legitimate users or even disrupt service by targeting shared IP ranges. Robustness of the application-level detection logic is paramount to prevent such abuse.
The Future of Edge Security
This application-to-edge feedback loop represents a significant evolution in how distributed applications can defend themselves against automated threats. By empowering the application to directly influence edge security policies, AWS provides a more intelligent, efficient, and responsive approach to bot mitigation. If you run an application on AWS that faces bot traffic, particularly around sensitive operations like authentication or API access, exploring this pattern is a logical next step to fortify your defenses.
