AWS CloudFront Functions
Deploy Centinel Analytica on a CloudFront distribution with two CloudFront Functions and no Lambda.
Overview
This guide installs Centinel Analytica with CloudFront Functions. Two functions run at every edge location:
- The viewer-request function sends each request to the Centinel validator. It forwards the request, or it serves the block page or the interstitial page.
- The viewer-response function sets the session cookie on allowed responses.
You do not deploy a Lambda function. You do not use the us-east-1 region.
For the Lambda@Edge integration, see AWS CloudFront Lambda@Edge.
Prerequisites
- Centinel secret key
- CloudFront distribution you can edit, on a paid pricing plan
- AWS Console access with CloudFront permissions
The functions send a request to the validator. CloudFront does not allow these requests on the free pricing plan, on multi-tenant distributions, or on continuous-deployment distributions. On these distributions, the function allows every request without validation.
Install
- Open CloudFront → Distributions and select the distribution to protect.
- In the Origins tab, choose Create origin.
- Set Origin domain to
validator.centinelanalytica.com. - Set Name to
centinel-validator. - Set Protocol to HTTPS only and Minimum origin SSL protocol to TLSv1.2.
- Set Origin HTTP version to HTTP/2. HTTP/2 keeps the connection to the validator open. This makes validator requests faster.
- Under Add custom header, add the header
x-api-keywith your secret key as the value. - Save the origin.
CloudFront adds the custom header to each request that the function sends. The secret key is not in the function code.
- Download
centinel-cloudfront-functions-viewer-request.js. - Go to CloudFront → Functions → Create function.
- Name the function (for example
centinel-viewer-request) and select the runtime cloudfront-js-2.0. - Paste the file content into the code editor and save it.
- In the Publish tab, publish the function.
- Download
centinel-cloudfront-functions-viewer-response.js. - Create a second function (for example
centinel-viewer-response) with the runtime cloudfront-js-2.0. - Paste the file content, save it, and publish it.
- Open the distribution and edit the cache behavior to protect.
- In Function associations, set Viewer request to CloudFront Functions and select the viewer-request function.
- Set Viewer response to CloudFront Functions and select the viewer-response function.
- Save the behavior.
Without the viewer-response function, allowed visitors do not get a session cookie. Centinel then validates each of their requests again.
- Edit the origin request policy of the cache behavior.
- Add the headers
CloudFront-Viewer-JA3-FingerprintandCloudFront-Viewer-JA4-Fingerprint. - Save the policy.
The viewer-request function sends each request header that it receives to the validator.
Configure
Edit these constants at the top of the viewer-request function before you publish it.
| Setting | Default | Purpose |
|---|---|---|
CENTINEL_ORIGIN_ID | centinel-validator | Name of the validator origin. |
CENTINEL_PROTECTED_PATHS | empty (all paths) | Paths to protect. * matches one path segment. |
CENTINEL_UNPROTECTED_PATHS | 34 static extensions | Paths to skip. *.ext matches at any depth. |
CENTINEL_LOG_ENABLED | true | Logs the reason when a request is not validated. |
CloudFront Functions have limits that change how the integration behaves:
- Fails open. A timeout, an error response, or a reply that CloudFront cannot read allows the request. The validator request times out after 1 second. You cannot change this limit.
- 10 KB request body. When a request is larger, the function sends only the
_centinelcookie in the Cookie header. Then it removes the largest headers until the request fits. - 10 KB validator reply. A block page or interstitial page larger than approximately 7 KB of HTML does not fit. The request then goes to the origin. Keep custom pages under 7 KB of HTML.
- 10 KB function size. Keep the function under 10 KB.
- No cookie on error responses. CloudFront does not run the viewer-response function when the origin returns status 400 or higher.
The viewer-request function adds the x-centinel-set-cookie request header to allowed requests. This header also goes to your origin. It does not change how your origin responds.
Verify
- Wait until the distribution status is Deployed.
- Open the distribution domain and make sure that pages load.
- In the browser developer tools, make sure that the response sets the
_centinelcookie. - To troubleshoot, find the log lines that start with
[Centinel]in CloudWatch Logs, in the log group/aws/cloudfront/function/<function-name>in us-east-1.
The console Test tab cannot send network requests, so test the function on a deployed distribution.
Changelog
- v1.0.0 — Initial release