Flagship + AWS Lambda Integration
Overview
This guide shows how to:
Initialize the Flagship SDK in an AWS Lambda function
Configure the SDK for optimal performance in serverless environments
Create a visitor object with context data from API Gateway events
Fetch feature flags assigned to this visitor
Retrieve specific flag values for use in the application
Send analytics data back to Flagship
Ensure analytics are sent before the Lambda function terminates
Prerequisites
Node.js (v22 or later)
Docker (for local testing)
An AWS account
A Flagship account with API credentials
Setup
Clone the example repository:
Install dependencies:
Configure your Flagship credentials in the template.yaml file:
Build and deploy your application:
Initialize the Flagship SDK in an AWS Lambda Function
When working with AWS Lambda, it's important to initialize the Flagship SDK outside the handler function to leverage container reuse, which improves performance for subsequent invocations.
Configuration Options
decisionMode:
BUCKETINGmakes decisions locally using pre-fetched campaigns (recommended for Lambda)DECISION_APIcalls Flagship servers for each decision (more network overhead)
fetchNow:
falsedefers fetching campaign data until explicitly needed, improving cold start times
pollingInterval:
0disables automatic polling since Lambda functions are ephemeral
trackingManagerConfig.batchIntervals:
0prevents Flagship from using background timers for analytics, which is critical because Lambda may terminate after function completion
batchActivateHits:
trueimproves performance by sending multiple activate flags in a single request
Create a visitor object with context from API Gateway events
The visitor object represents a user of your application. In AWS Lambda, you can extract context data from the API Gateway event:
You can include any information in the context object that might be useful for targeting. API Gateway events provide access to HTTP headers, query parameters, and path information. Common examples include:
Demographics: location, language preferences
Technical: device type, browser, operating system
Behavioral: account type, subscription status
Custom: any application-specific attributes
Fetch feature flags assigned to this visitor
Once you have a visitor object, you need to fetch the feature flags assigned to them based on targeting rules:
This operation evaluates all campaign rules against the visitor's context and assigns flag variations accordingly.
Retrieve specific flag values for use in the application
After fetching flags, you can retrieve specific flag values to control your application's behavior:
Always provide a default value that matches the expected type. This ensures your application works even if the flag isn't defined or there's an issue fetching flags.
Note: Calling
getValueautomatically activates the flag, meaning it will be counted in reporting.
Send analytics data back to Flagship
To measure the impact of your feature flags, send analytics data back to Flagship:
Analytics data is crucial for measuring the impact of your feature flags. You can track page views, events, transactions, and more to understand how different flag variations affect user behavior.
Ensure analytics are sent before the Lambda function terminates
AWS Lambda functions can terminate immediately after completion, potentially losing unsent analytics data. To prevent this, call Flagship.close() before returning:
This ensures that all pending analytics are sent before the Lambda function terminates, giving you accurate reporting data.
Production Considerations
When deploying this integration in production, consider the following best practices:
Security
Environment Variables: Store sensitive information like API keys or ENV ID in environment variables or AWS Secrets Manager.
Cold Starts and Warm Containers
AWS Lambda has the concept of "cold starts" and "warm containers":
Cold start: First invocation after deployment or when scaling up
Warm container: Subsequent invocations reusing the same container
To optimize performance:
Initialize the Flagship SDK outside the handler function
Use the Bucketing decision mode to minimize network calls
Set
fetchNow: falseto defer campaign fetching until neededConsider using Provisioned Concurrency for critical Lambda functions
Learn More
Last updated
Was this helpful?

