
Keep your CDN: add CloudFront edge compute, WAF, and AI monetization
Use CloudFront edge compute, AWS WAF, and AI monetization alongside an existing CDN. Compare Redirect, Direct, Loop, and Gate, choose partial or full traffic coverage, and explore automatic Markdown delivery for AI agents.
October 1, 2026: This post was updated to add Akamai to the CDNs whose client IP AWS WAF Bot Control and Anti-DDoS recognize automatically.
Applications using Amazon CloudFront as their content delivery network (CDN) can add edge compute, AWS WAF protection , and AI traffic monetization directly to their delivery path. If you use another CDN, you can also add those capabilities while retaining your existing setup. For example, route selected AI-agent requests to CloudFront to serve Markdown, apply security rules, or require payment.
This post compares four HTTP integration patterns: Redirect, Direct, Loop, and Gate. In each, the existing CDN hands selected requests to CloudFront for processing at the AWS edge. This HTTP overlay extends your delivery setup. It does not select alternative CDN providers for availability, performance, or cost, as a multi-CDN delivery strategy does. The patterns provide neither DNS routing between CDNs nor automatic provider failover.
CloudFront's origin can be almost any publicly reachable HTTP(S) endpoint, whether on AWS, another cloud provider, another CDN, or your own infrastructure. Ports are typically 80 or 443, subject to CloudFront's origin requirements .
Choose the traffic scope first
Each pattern can cover selected traffic or all application traffic. Use the existing CDN's routing and edge logic to choose which requests enter the AWS path:
- Selected traffic: route specific hostnames, paths such as
/api/*, or requests identified by bot detection or business rules. Other traffic stays on the existing delivery path, without this layer's AWS WAF evaluation or CloudFront compute.
- All traffic: include every viewer request for the application. Required WAF checks must cover requests that the existing CDN could otherwise serve from cache.
Bot detection can stay on the existing CDN. AWS WAF's
Monetize action is independent of Bot Control: your CDN can identify which traffic must pay, and AWS WAF can handle monetization alone. Alternatively, use AWS WAF for classification and security as well.For all traffic, CloudFront as the primary CDN is usually simpler: viewer → CloudFront with edge compute and AWS WAF → origin. This removes the extra handoff and reduces routing, caching, and operational complexity. Retain an overlay for all traffic when you have a concrete reason, such as a required origin integration or a staged migration.
Use CloudFront's compute capabilities
CloudFront's edge compute capabilities operate on the requests, responses, and connections that reach it. Match the runtime and event to the work:
Capability | Examples in this design |
|---|---|
CloudFront Functions, with CloudFront KeyValueStore when needed | URL rewrites and redirects, header changes, cache-key normalization, token checks, and request-based origin selection |
Lambda@Edge | Viewer and origin event handlers for more complex logic, third-party libraries, and network calls to other services |
CloudFront Connection Functions | Custom certificate checks and revocation rules on the CDN-to-CloudFront connection, if your CDN supports mutual TLS (mTLS) to CloudFront |
CloudFront Functions has no request-body access or general network access. Function-association restrictions also mean the runtimes are not interchangeable at every event. CloudFront Functions and Lambda@Edge can act on classification headers inserted by AWS WAF.
The four patterns differ in who fetches content and who returns it to the viewer. Each architecture diagram uses solid arrows for requests and dashed arrows for responses. Dashed lines without arrowheads connect the WAF web access control list (web ACL) to CloudFront inside the purple AWS Edge group.
Each CloudFront component can run configured edge functions. The numbered sequences show one successful request, including optional payment processing. A WAF block stops processing. When monetization applies, an unpaid request first receives an HTTP 402 challenge, and the client retries with payment authorization.
1. Redirect: send the client to CloudFront
The existing CDN returns an HTTP 302 redirect. The client follows it to CloudFront, where AWS WAF applies your policy and CloudFront fetches and delivers the content directly.
Architecture

Request sequence

Pros
- CloudFront receives the client's own connection, including its TLS fingerprint.
- The existing CDN handles only the initial redirect on this path.
- No proxy callback or restart is needed.
Cons
- The client sees a different hostname and must support redirects. A 302 can change POST to GET, so define method handling explicitly.
- Cookies, authorization, and signed requests need care across hostnames. Negotiate payment for the final resource URL.
- CloudFront needs access to the origin.
Choose Redirect when clients can follow the handoff and you want a direct client connection to CloudFront. A dedicated first-party hostname can make that endpoint easier to recognize.
2. Direct: proxy through CloudFront to the origin
The client keeps using the existing CDN hostname. That CDN proxies selected requests to CloudFront, which applies WAF policy and fetches content directly from the origin. CloudFront returns the response to the existing CDN, which delivers it to the client.
Architecture

Request sequence

Pros
- The public hostname and client flow stay the same.
- The content path needs no callback into the existing CDN.
- Works for WAF protection, monetization only, or both.
Cons
- The origin must accept an additional delivery path from CloudFront.
- Allowed content passes through both CDNs, adding request and transfer costs.
- CloudFront sees the upstream CDN's TLS connection, not the client's original handshake.
Choose Direct when you want to retain the existing CDN and public hostname, and CloudFront can reach the origin. This is the default starting point for an overlay: validate one selected path before adding more routing logic.
3. Loop: return through the existing CDN for content
The existing CDN forwards the request to CloudFront. After WAF allows it, CloudFront fetches content through an authenticated return path on the same CDN. The existing CDN makes the origin request and returns the content to CloudFront. CloudFront then sends it back to the CDN's public route for delivery to the client.
Architecture

Request sequence

Pros
- The origin remains behind the existing CDN.
- Its origin routing and integration logic stay in place.
- The client keeps its current hostname.
Cons
- The extra CDN pass adds latency, processing, and transfer costs.
- The return path needs authentication and a loop guard.
- Cache behavior and response transforms must work correctly across both passes.
Choose Loop when preserving the existing CDN's origin integration matters more than minimizing hops. The return-path credential proves admission through CloudFront. It is not proof of settled payment, which happens after the successful content response.
4. Gate: validate with WAF, then fetch content separately
The existing CDN sends a separate request carrying the context AWS WAF needs to evaluate your policy. This gate request goes to a CloudFront endpoint, where the policy can use bot detection, other AWS Managed Rules, custom rules, and optional payments. If the checks allow it, a synthetic origin returns success without real content. The CDN must authenticate the gate endpoint and tie its result to the current request before fetching and serving the real content itself. A block or unresolved challenge stops the content fetch.
When monetization is enabled, WAF settles payment against the synthetic response before the real content fetch. Without monetization, Gate requires no payment exchange.
CloudFront compute operates on the gate exchange. It cannot adapt the real content response, which stays on the existing CDN's path.
Architecture

Request sequence

Pros
- Content responses stay on the existing CDN's delivery path.
- The existing CDN retains the real origin fetch.
- WAF can enforce security rules, payment requirements, or both without carrying the content response through CloudFront.
Cons
- Requires a subrequest or restart, trusted per-request state, and propagation of blocks and challenges.
- WAF evaluates the gate request. Preserve the method, path, query, headers, cookies, and body needed by your rules, within WAF inspection limits. Later CDN rewrites are outside that evaluation.
- With monetization, payment can settle even if the later real content fetch fails. This variant needs compensation and retry handling. An arbitrary 200 or viewer-supplied “paid” flag is not proof of payment.
Choose Gate when you want WAF to validate requests while the existing CDN retains content delivery. Validate each rule's inspection context and any Challenge or CAPTCHA token flow through your adapter. Treat the monetized variant as experimental until it handles payment settlement followed by delivery failure.
Monetization alone does not require Gate. Redirect, Direct, and Loop also support it while keeping the real content response in the payment flow.
Example: deliver Markdown to selected AI agents
Content adaptation shows what these patterns can do beyond enforcing policy. The AWS sample Automated Format Conversion for AI Agents using Amazon CloudFront and AWS WAF combines bot classification with edge compute:
- Classify and negotiate: AWS WAF Bot Control identifies eligible AI-bot requests that accept Markdown.
- Choose the content: a CloudFront Function reads KeyValueStore and routes to a generated Markdown object when available.
- Generate missing formats: serve the original HTML while an asynchronous pipeline runs a regional AWS Lambda function to create Markdown. The pipeline stores it in Amazon S3 and updates the markers used by subsequent requests.
The sample places CloudFront in front of an existing CDN. To adapt it to this overlay, route selected AI-agent requests from the existing CDN to the sample's CloudFront endpoint. Preserve
Accept and the classification context. Redirect, Direct, and Loop can return adapted content to the client, whereas Gate's separate policy exchange does not carry the real response. The sample is a reference implementation, not a validated adapter for every upstream CDN.Keep HTML and Markdown cache entries separate at both CDNs, using the inputs that determine eligibility and format. The sample uses a header inserted by WAF in CloudFront's cache key and sends
Vary: Accept . Configure the upstream CDN's cache rules too. Content adaptation can run independently of monetization. To combine them, validate resource identity and payment handling.Choose a pattern
Your main requirement | Start with | Why |
|---|---|---|
Use CloudFront compute, AWS WAF, or both for all traffic, with no requirement to retain the existing CDN | CloudFront as the primary CDN | Viewers connect straight to CloudFront without an extra CDN handoff |
Keep the existing CDN at the current hostname, with an origin CloudFront can access | Direct | A transparent path without a return pass |
Deliver adapted content, such as Markdown, while retaining the existing CDN | Direct, or Loop if its origin integration must stay | The CloudFront content path can select and deliver the adapted representation |
Have clients connect directly to CloudFront | Redirect | Client TLS connection and direct content delivery |
Preserve the existing CDN's origin integration | Loop | CloudFront retrieves content through that integration |
Apply WAF checks while keeping content delivery on the existing CDN | Gate | Separate policy and content exchanges, with optional payment |
Start with the constraint that matters to your application, then measure latency and total request and transfer costs. Include edge-function invocations, KeyValueStore, and any content-generation pipeline. Each selected path depends on the components it uses, so two CDNs do not inherently add redundancy. Any alternate paths in a separate multi-CDN strategy must preserve your security and payment policy.
Include requests blocked by WAF in the cost comparison. CloudFront charges no request fees or data transfer charges for requests blocked by AWS WAF , effective October 25, 2024. This applies to all four patterns when WAF blocks the CloudFront request. Under pay-as-you-go pricing, WAF still bills for evaluation and blocking, and the existing CDN's charges remain separate. Allowed traffic follows the applicable pricing. An HTTP error status alone does not qualify for the waiver. On CloudFront flat-rate plans , requests blocked by WAF also do not count against the usage allowance.
Implement the handoff on your CDN
Use your CDN's origin-routing or outgoing-request features to send selected traffic to CloudFront. The public documentation below describes these building blocks. Gate also needs logic to validate the policy response before fetching content separately.
Existing CDN | Origin routing and outgoing requests | Optional mTLS to CloudFront |
|---|---|---|
Akamai | Configure an Origin Server , or use EdgeWorkers httpRequest() for a subrequest | mTLS Edge Server to Origin |
Fastly | Define a VCL backend and select it with req.backend | Backend API fields ssl_client_cert and ssl_client_key |
Cloudflare | The Workers Modify request property example constructs an outgoing request and sends it with fetch() | Workers mTLS bindings , using the binding's fetch() |
bunny.net | The Edge Scripting Fetch URL example sends an HTTP request. Edge Rules can change the selected origin | Client Certificates (mTLS) , using Deno.createHttpClient with fetch() |
Check when and where the CDN runs your logic. Akamai's
httpRequest() requires a target hostname served by Akamai. Configure a delivery property for that hostname with CloudFront as its origin. bunny.net scripts run after cache by default. Enable before-cache execution when selected requests need checks even on cache hits. Preserve the method, query, body, and headers required by your policy when adapting any example.The mTLS links describe the CDN-to-CloudFront connection. To use mTLS, configure CloudFront to require and validate the client certificate, adding Connection Functions if you need custom validation. Cloudflare's Workers mTLS binding cannot target a destination proxied by Cloudflare, so use the CloudFront endpoint or a hostname that resolves directly to it. Validate each complete adapter for its selected pattern.
Validate the handoff before expanding traffic
1. Enforce policy before serving cached content. Send every request in the chosen scope through the required checks, including cache hits and payment retries. Full WAF protection of a path requires every request on that path to reach WAF. Full application coverage requires every viewer request. For monetization alone, the existing CDN can select requests using its bot detection and pricing policy.
2. Preserve the client IP through proxies. Bot Control and Anti-DDoS automatically recognize traffic from Amazon CloudFront, Akamai, Cloudflare, and Fastly IP ranges. They use the standard upstream client-IP header, such as
Fastly-Client-IP , without forwarded-IP configuration on those managed rule groups. Custom IP-set, geo, ASN, and rate-based rules still need that configuration. Trust only the intended upstream's assertions. Recovering the client IP does not recover the original TLS handshake.3. Test payment verification, settlement, and delivery. AWS documents verification → successful content fetch → settlement → delivery . Origin 4xx or 5xx responses skip settlement, and settlement failure withholds content. In a monetized Gate flow, a successful synthetic response triggers settlement, so a later delivery failure needs separate handling. Start with test-mode payments , preserve challenges and receipts, and verify that a later unpaid request cannot reuse a paid response.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article