The Kameleoon AWS Lambda@Edge Starter Kit demonstrates Kameleoon experimentation and feature flags on Amazon Web Services (AWS) Lambda@Edge.
This repository is home to the Kameleoon starter kit for AWS Lambda@Edge. Kameleoon is a powerful experimentation and personalization platform for product teams that enables you to make insightful discoveries by testing the features on your roadmap. Discover more at https://kameleoon.com, or see the developer documentation.
This starter kit provides quick start instructions for developers using the Kameleoon NodeJS SDK with AWS Lambda@Edge.
Make sure you have the following requirements before you get started:
- A Kameleoon user account. Visit kameleoon.com to learn more.
- An AWS account with Lambda@Edge access. For more information, visit the official AWS Lambda@Edge product page.
First, you'll set up a basic AWS Lambda@Edge environment. For this step, follow the official tutorial from AWS to create a simple Lambda@Edge Function.
In the tutorial, complete these sections:
-
Sign up for AWS account - If you don't have an account, please sign up for Amazon Web Services at https://aws.amazon.com.
-
Create a Cloudfront distribution - If you're not familiar with CloudFront, take a few minutes to read a short overview and learn a bit about how CloudFront caches and serves content.
-
Create a Lambda function - In this step, you create a Lambda function, starting with a blueprint template that's provided in the Lambda Console.
-
Add your Cloudfront trigger to Lambda function - Now that you have a Lambda function, configure the CloudFront trigger to run your function.
-
Verify that the function runs - Make sure that your environment is properly configured.
After your Lambda@Edge environment is prepared, install the starter kit:
- Clone this repository to your local development environment.
- In a terminal, navigate to the
aws-lambda-edge-starter-kit/
directory. - Run
npm install
.
The Kameleoon AWS Lambda@Edge Starter Kit uses and extends the Kameleoon NodeJS SDK to provide experimentation and feature flagging on the edge.
In the src
folder of the starter kit, you will find two TypeScript files:
src/handler.ts
contains sample code that initializes and caches the Kameleoon SDK and retrieves the feature flag variation.src/visitorCodeManager.ts
file contains a custom implementation of Kameleoon Visitor Code Manager, which provides an ability to work with KameleoonvisitorCode
at the edge.
You'll use these files to initialize your environment:
-
Navigate to
src/handler.ts
and update theSITE_CODE
,CLIENT_ID
,CLIENT_SECRET
andFEATURE_KEY
with your Kameleoon credentials and feature flag data collected on the Kameleoon Platform. -
Review or adjust the feature flags or experiments you've configured with the Kameleoon NodeJS SDK, and hook into the lifecycle events by adding your own logic in
src/handler.ts
. For example, you can change headers, cookies, and more. -
Run
npm run build:lambda
to bundle the source code into adist/handler.zip
file that you'll import into Lambda. -
Upload the
dist/handler.zip
file into Lambda using one of these options:- GUI: Go to your AWS Lambda console, select the function associated with your Lambda environment, and import the
dist/handler.zip
file. After you upload it, there should now be a minifiedindex.js
file located inside of your Lambda function's Code Source section. - CLI: You can use the AWS CLI to update your AWS Lambda function programmatically. Example command:
aws lambda update-function-code --function-name my-aws-lambda-at-edge-function --zip-file fileb://dist/handler.zip
- GUI: Go to your AWS Lambda console, select the function associated with your Lambda environment, and import the
-
Upload the response
dist/handler.zip
file into Lambda.Lambda Layers: If you need additional libraries, custom runtimes, or configuration files to use alongside your Lambda function, consider using Lambda Layers.
-
Provision the function with Lambda@Edge permissions and associate the function with your CloudFront distribution.
Note: CloudFront triggers are associated with only one specific version of your Lambda function. Remember to update the CloudFront trigger assignment as needed when pushing new versions of your Lambda function. For example, you may need one function that handles receiving viewer requests (viewer request trigger) and one function that handles returning a response to the viewer (viewer response trigger).
-
Test your Lambda@Edge function. It should return a simple home page with the results of your feature flag test and User ID cookie.
You can find details on how the starter kit works in the CloudWatch console under Logs > Log groups > /aws/lambda/<YOUR_LAMBDA_NAME>. Click your Lambda's log group to follow the entire process.
-
Adjust your lambda's configuration as needed. For example, you may need to increase your function's memory, storage, and timeout threshold to accommodate your needs.
You can now use Kameleoon's feature flagging and experimentation as desired. You can modify the cookies and headers based on experimentation results, add hooks to the Origin Request and Origin Response CloudFront triggers to perform origin redirects or dynamic asset manipulation, or add more services to the pipeline including your own logging systems, databases, CDN origins, and more. Keep in mind that Lambda@Edge has some limitations, which you can familiarize yourself with in the Edge Functions Restrictions article.
To avoid uploading your Lambda function to AWS every time you make a change, you can test your Lambda function locally using the AWS SAM
runtime interface client.
- Install the AWS SAM CLI.
- Run
npm run test:viewer_request
to build the lambda and run it locally with theviewer-request
event.
You can also run lambda handler using viewer-response
/origin-request
/origin-response
events by running the related commands.
Additionally you can change the configuration of lambda by providing additional parameters to template.yaml
or by modifying the template event files in the lambda
folder.
The following commands are available in the starter kit:
npm run build
- Builds the TypeScript files in thesrc/
directory creatingdist/handler.js
(ignoressrc/examples
).npm run build:lambda
- Builds the Lambda function and creates adist/handler.zip
file.npm run test:viewer_request
- Runs the Lambda function locally with thelambda/viewer-request.json
event.npm run test:viewer_response
- Runs the Lambda function locally with thelambda/viewer-response.json
event.npm run test:origin_request
- Runs the Lambda function locally with thelambda/origin-request.json
event.npm run test:origin_response
- Runs the Lambda function locally with thelambda/origin-response.json
event.npm run clean
- Removes thedist/
directory.
SDK configuration and collected data is stored in-memory in the Lambda function. In some cases, when the data is too large, the Lambda function may break. To address this, you can cache the data more efficiently using AWS services like S3
, DynamoDB
, or CloudFront
.
You can find basic examples of caching SDK configuration in src/examples/configurationCache.ts
and caching SDK stored data in src/examples/dataCache.ts
. These basic examples store data in AWS Lambda cache for more details on working with AWS service reference official AWS documentation.
Note: Additional caching mechanisms may be available depending on your CloudFront distribution's configuration.
Out of the box, our starter kit uses a basic implementation of KameleoonVisitorCodeManager
found in src/visitorCodeManager.ts
. This implementation allows for basic Kameleoon Visitor Code reads and writes using lambda handler's request
and response
objects making the use of getVisitorCode
method seamless.
Alternatively you can use your own User ID or other identifier as a visitor code.
For more information about AWS Lambda@Edge and Kameleoon, see the following resources: