AWS CloudFront — Forward AI traffic
CloudFront Function
In the CloudFront console → Functions → Create function → JavaScript runtime 2.0:
function handler(event) {
var ua = event.request.headers['user-agent'] && event.request.headers['user-agent'].value || '';
var bots = /ChatGPT-User|PerplexityBot|Perplexity-User|GPTBot|OAI-SearchBot|anthropic-ai|Claude-Web|ClaudeBot|Claude-User|Google-Extended|Gemini|Applebot-Extended|meta-externalagent|meta-externalfetcher|CCBot|cohere-ai|YouBot|Diffbot|Timpibot|Amazonbot|Bytespider/i;
if (bots.test(ua)) {
return {
statusCode: 302,
statusDescription: 'Found',
headers: {
location: { value: 'https://oasy.<your-domain>' },
'cache-control': { value: 'no-store' }
}
};
}
return event.request;
}Replace <your-domain>. Publish the function, then attach it to your
distribution:
- Your distribution → Behaviors → Edit the default (or per-path) behavior.
- Function associations → Viewer request → CloudFront Functions →
pick the function you just published. - Save changes (deploys in 1–2 minutes).
Verify
Back on the MCP page, click Check forwarding.
Notes
- CloudFront Functions run at every edge POP — fast, cheap, no Lambda cold
starts. - The 1 MB / 1 ms limits don't apply here — UA matching is trivial.
- If you need this on a path-specific behavior (e.g. only
/articles/*),
attach the function to that behavior instead of the default.