Overview
Integrating CloudFront with llm-optimizer allows Salespeak to automatically deploy Lambda@Edge functions and manage caching rules for your domain. This ensures your website can serve optimized pages and monitor LLM activity seamlessly—without manual setup.
The CloudFront integration automatically:
- Detects AI Crawlers: Identifies ChatGPT, Claude, Perplexity, Google-Extended, BingPreview, GPTBot, and other AI visitors
- Injects Optimized Content: Automatically serves AI-optimized content from your alternate origin
- Logs AI Visits: Tracks AI crawler visits to your analytics dashboard
- Manages Caching: Configures cache bypass rules for AI visitors to ensure fresh content
How It Works
The integration uses AWS Lambda@Edge functions that run at CloudFront edge locations:
-
Viewer Request Handler: Detects AI visitors by analyzing
User-Agentheaders and logs visits to your API. - Origin Response Handler: Fetches optimized content from your alternate origin and injects it into HTML responses.
This architecture ensures:
- Low Latency: Functions run at edge locations closest to your visitors
- Automatic Scaling: AWS handles all scaling automatically
- High Availability: Built on AWS's global infrastructure
- Zero Downtime: Updates don't require CloudFront distribution recreation
Prerequisites
Before setting up the CloudFront integration, ensure you have:
- ✅ Deployment Package: Zipped
lambda-edge-ai-optimizer.zippackage from Salespeak (containsdeploy.sh,index.mjs,cloudformation.yaml, and other required files) - ✅ Salespeak Account with organization ID
- ✅ AWS CLI configured with appropriate permissions (
aws configureoraws sso login)
Required AWS Permissions
Your AWS credentials need the following permissions:
lambda:CreateFunctionlambda:UpdateFunctionCodelambda:PublishVersionlambda:GetFunctionlambda:UpdateFunctionConfigurationcloudformation:CreateStackcloudformation:UpdateStackcloudformation:DescribeStacksiam:CreateRoleiam:AttachRolePolicyiam:PassRolecloudfront:GetDistributioncloudfront:UpdateDistributionlogs:CreateLogGrouplogs:CreateLogStreamlogs:PutLogEvents
Quick Start
Automated Deployment (Recommended)
The easiest way to set up CloudFront integration is using the automated deployment package provided by Salespeak.
Step 1: Download and Extract the Deployment Package
You'll receive a zipped package (lambda-edge-ai-optimizer.zip) containing all necessary files:
-
deploy.sh— automated deployment script -
index.mjs— Lambda@Edge function code -
cloudformation.yaml— CloudFormation template for infrastructure -
README.md— additional documentation
Extract the package:
# Extract the zipped deployment package unzip lambda-edge-ai-optimizer.zip cd lambda-edge-ai-optimizer # Make the deploy script executable chmod +x deploy.sh
Step 2: Configure Environment Variables
Set your organization-specific configuration:
# Set your organization ID (required) export ORGANIZATION_ID="your-organization-id"
Step 3: Run the Deployment Script
Execute the deployment script:
./deploy.sh
Cloudfront distribution:
Use the output ARN from the deployment (2 lambda@edge functions)
-lambda-edge-ai-optimizer-v2-viewer-request
-lambda-edge-ai-optimizer-v2-origin-response
ARN looks like
arn:aws:lambda:us-east-1:<aws account id>:function:lambda-edge-ai-optimizer-v2-viewer-request:1
arn:aws:lambda:us-east-1:<aws account id>:function:lambda-edge-ai-optimizer-v2-origin-response:1
Updating Viewer Request function: lambda-edge-ai-optimizer-viewer-request
✓ Viewer Request function updated
Updating Origin Response function: lambda-edge-ai-optimizer-origin-response
✓ Origin Response function updated
Publishing Lambda function versions...
✓ Viewer Request version published: 14
ARN: arn:aws:lambda:us-east-1:984577054033:function:lambda-edge-ai-optimizer-v2-viewer-request:1
✓ Origin Response version published: 23
ARN: arn:aws:lambda:us-east-1:984577054033:function:lambda-edge-ai-optimizer-v2-origin-response:1
✓ ARNs saved to lambda-arns.txt
Attach the functions to your CloudFront distribution
Attach the functions to your CloudFront distribution
1. Go to the "Behaviors" tab and edit the behavior that serves the pages you want optimized (usually "Default (*)").
2. Scroll to "Function associations".
3. Under "Viewer request" → Function type: Lambda@Edge → paste the VERSIONED ARN from lambda-arns.txt (must end in :1 or a specific number, NOT $LATEST).
4. Under "Origin response" → Function type: Lambda@Edge → paste the VERSIONED origin-response ARN. Leave "Include body" unchecked (not needed for origin-response).
5. Save the behavior.
6. Wait 5–15 minutes for CloudFront to deploy the association globally. Status "Deployed" means it's ready. "Behaviors" tab and edit the behavior that serves the pages you want optimized (usually "Default (*)").
Edit the viewer request to use that ARN (viewer-request)
Edit the origin response with the ARN (origin-response)