Skip to content

Bot protection on AWS CloudFront

Run Centinel at CloudFront edge locations with two CloudFront Functions or one Lambda@Edge function while keeping your distributions, origins and AWS WAF web ACLs.

How it fits into CloudFront

Two runtimes
CloudFront Functions on cloudfront-js-2.0 with no Lambda or us-east-1 deployment, or Lambda@Edge on Node.js in us-east-1.
Two events per behavior
Viewer request makes the decision. Viewer response writes the _centinel session cookie.
Timeouts and failure mode
CloudFront Functions wait a fixed 1 s. Lambda@Edge waits 300 ms by default (CENTINEL_TIMEOUT) and backs off up to 5 minutes. Both fail open.
TLS fingerprints
Add CloudFront-Viewer-JA3-Fingerprint and CloudFront-Viewer-JA4-Fingerprint to the origin request policy so the validator sees them.

How a request moves through CloudFront

Both runtimes split the work across two events on the same cache behavior, because one viewer-request pass cannot both forward a request and set a cookie.

  1. Viewer request

    The function skips 34 static extensions by default and sends the request headers to the validator. CloudFront Functions call the centinel-validator origin, which adds the x-api-key header for you.

  2. Timeout

    CloudFront Functions give up after 1 s, a limit you cannot change. Lambda@Edge gives up after 300 ms and stops calling the validator for a while after a failure.

  3. Decision

    Block and challenge return the validator's page from the viewer-request event. Allow forwards the request to your origin with an x-centinel-set-cookie header.

  4. Viewer response

    The viewer-response function writes _centinel from that header. CloudFront skips this event when your origin returns 400 or higher.

Minimal configuration and a live check

Scope routes in the viewer-request function

centinel-cloudfront-functions-viewer-request.js
var CENTINEL_ORIGIN_ID = 'centinel-validator';

// Empty validates every non-static path. '*' matches one path segment.
var CENTINEL_PROTECTED_PATHS = ['/api/*', '/search'];

// Leave default 34 static-asset extensions skipped in CENTINEL_UNPROTECTED_PATHS.
var CENTINEL_LOG_ENABLED = true;

Keep your secret key out of the function code. Add it as an x-api-key custom header on the centinel-validator HTTPS origin so CloudFront attaches it to every call.

Check the session cookie on a deployed distribution

Verify
curl -s -o /dev/null -D - https://d111111abcdef8.cloudfront.net/ | grep -i set-cookie

Expect Set-Cookie: _centinel=... on a 200 response. No cookie usually means the viewer-response association is missing; the [Centinel] lines in CloudWatch give the reason a request was not validated.

Deployment options

  • CloudFront Functions (paid plans)

    Add validator.centinelanalytica.com as an HTTPS-only, HTTP/2 origin with your x-api-key custom header, then associate the viewer-request and viewer-response functions with the behavior.

    CloudFront Functions guide (opens in a new tab)
  • Lambda@Edge (Node.js)

    Publish one Lambda version in us-east-1 with edgelambda.amazonaws.com and lambda.amazonaws.com in the execution role trust policy, then bind the versioned ARN (not $LATEST) to Viewer request and Viewer response.

    Lambda@Edge guide (opens in a new tab)

What to know before deploying

  • CloudFront Functions need a paid plan

    Outbound requests from functions do not work on the free plan, multi-tenant distributions or continuous-deployment distributions. There, every request is allowed without validation.

  • Keep custom pages under 7 KB of HTML

    CloudFront Functions cap the validator reply at 10 KB, so a larger block or challenge page goes to the origin instead. On Lambda@Edge an oversized page becomes a 502.

  • The console Test tab cannot reach the validator

    It cannot send network requests. Test on a deployed distribution after its status reads Deployed, which takes 15 to 20 minutes for Lambda@Edge.

  • Logs land in different regions

    CloudFront Functions log to /aws/cloudfront/function/<name> in us-east-1. Lambda@Edge logs to /aws/lambda/us-east-1.<name> in the region nearest the viewer.

  • Your CloudFront distributions, origins and S3 or ALB backends
  • Your AWS WAF web ACLs and AWS Shield protection
  • Cache behaviors you do not associate the functions with
  • Your application authentication and backend code

CloudFront integration questions

Walk through your CloudFront setup

Bring one route bots are hitting. We will map the right connector, timeouts, and log-only rollout for your CloudFront stack.