Skip to main content
Amazon CloudFront is AWS’s content delivery network. It serves requests from edge locations or forwards them to your origin. Lambda@Edge runs a function, authored in us-east-1, at those edge locations. This integration evaluates inbound request bodies before CloudFront forwards them to an AI endpoint you operate. It does not inspect model responses, individual tool calls inside an agent, or employee access to third-party AI services.

Integration capabilities

Before you start

Start with the policy in Observe mode. Findings appear in Activity without affecting traffic. Switch to Enforce after reviewing the results. See Policies.

1. Create a collector API key

On the CloudFront collector, open Auth and create a key. It is shown once.

2. Write the request handler

The function decodes the exposed body, calls POST /v1/evaluate with the collector key as a bearer token, and returns either a 403 response or the original request:
Returning an object with a status short-circuits the request: CloudFront answers the viewer and your origin is never called. Returning request forwards it. The allow, report, ask, and transform verdicts reach the origin. An ask verdict is recorded and allowed because the edge cannot prompt a user. The key is inline above for readability only. Step 3 replaces it.
This sample does not implement redaction. A transform verdict contains TrustGuard’s { "input": … } payload rather than your origin’s request schema. Applying it requires custom, schema-aware code that reconstructs the complete body. Use Block rules unless you have implemented and tested that conversion.

3. Configure the collector key

Lambda@Edge does not support environment variables, so process.env is not an option. There are two ways to supply the collector key: Either way the key never travels in the request body.

4. Publish the function and deploy it as Lambda@Edge

Author the Node.js function in us-east-1 and publish a numbered version; Lambda@Edge associates a version, never $LATEST. CloudFront then replicates that version to its edge locations. Allow several minutes for changes to propagate.

5. Choose a request trigger and enable Include Body

Attach the function to either the Viewer request or Origin request event of the cache behavior that carries your AI paths, and enable Include Body. Include Body exposes the payload. Without it, request.body is not populated, the handler’s request.body?.data guard is false, and requests are forwarded without evaluation or an error. CloudFront supports this option on both request events. CloudFront first selects the matching cache behavior. The trigger then determines when the function runs and how much of the body it can inspect: Use viewer request when you need to evaluate cache hits. Use origin request when you need the higher body limit and do not need to evaluate cache hits. Scope the association to the behavior that serves AI routes. A default-behavior association also applies to unrelated assets in the distribution.

6. Account for request-body truncation

CloudFront exposes up to 40 KB of the body to a viewer-request function and up to 1 MB to an origin-request function. When a body exceeds the applicable limit, CloudFront sets request.body.inputTruncated to true and provides only the prefix within that limit.
Do not evaluate a truncated prefix as though it were the complete prompt. The sample checks inputTruncated and returns 413 Payload Too Large before calling TrustGuard. If your application must accept larger requests, evaluate them at a point that receives the complete body.

7. Verify

  1. Put the policy in Enforce and POST a prompt that trips a rule to a path on the guarded behavior.
  2. Confirm that the caller receives 403 Forbidden with the body Blocked by TrustGuard.
  3. Confirm the event in TrustGuard Activity, under the consumer_id taken from x-user-id.
Then send a request larger than the selected trigger’s limit: over 40 KB for viewer request or over 1 MB for origin request. Confirm that the function returns 413 and does not forward the truncated request to the origin.

Reference

Coverage

The function evaluates input bodies up to the selected trigger’s limit: 40 KB on viewer request or 1 MB on origin request. It returns 413 when CloudFront marks a body as truncated. It does not inspect responses, expose individual tool calls, or support redaction. Each evaluated request adds one round trip from the edge to {TRUSTGUARD_URL}.

What is evaluated

Every call is POST /v1/evaluate with the collector key as a bearer token, and the policy’s detectors decide the verdict. Verdicts are request-level: a block stops the whole request, never one field of it. The handler sends direction: input only, so Output-phase rules on the policy never evaluate, however many of them you write.

Configuration

Configure the integration across CloudFront, Lambda, and the handler body: The sample does not define failure behavior. Without try/catch, an unreachable or slow TrustGuard call throws and CloudFront returns an error. Wrap the call and choose explicitly: return request to fail open, or return your own 403 to fail closed.

Attributes

  • consumer_id: read from the x-user-id request header, empty string when absent. It is what per-consumer policy routing keys on, so without it every caller shares the collector’s default policy.
  • session_id: read from the x-session-id request header. The sample sends "" when the header is absent. Send a stable, verified conversation ID if you need reliable grouping in Activity.
  • The function reads the header values present at the selected trigger. CloudFront does not derive either value for you. If your front end does not send those headers, map them from another source, such as a session cookie or a claim your edge authentication already validates. For an origin-request trigger, ensure that your cache or origin request policy preserves the identity headers.

Troubleshooting