The goal of this project is to help you get started using LaunchDarkly feature flags within a serverless application using AWS Lambda. The project includes everything you'll need to get started integrating feature flags into your Lambdas using the Node.js runtime.
Adding LaunchDarkly feature flags to any Lambda always begins with installing the appropriate SDK for the Lambda runtime you are using. You can find a full list of server-side SDKs here. For the Node.js runtime, that would mean using NPM.
npm install launchdarkly-node-server-sdk
If you would like to use DynamoDB as a persistent feature store, you'll need to also install those dependencies.
npm install launchdarkly-node-server-sdk-dynamodb
This works well for a single Lambda, but if you are using feature flags across a number of Lambdas, the easiest method to maintain the depencies across all of them is using a Lambda Layer. The code provided here will assist you in creating the Layer.
The necessary dependencies are already included in this project under /cdk/resources/layers
. It contains both the Node.js SDK and the DynamoDB extension. In order to ensure that this asset has the latest versions of these SDKs, we've included a script that will download the latest versions and archive them into the proper folder. It is recommended that you run this before deploying any of the assets using the provided CDK scripts. All you need to do is:
cd create-layer
npm run build
We've provided three example Lambdas to demonstrates the basics of deploying LaunchDarkly within AWS serverless.
- getFlagsStarter – This example uses the LaunchDarkly Node.js server SDK to demonstrate how to initialize the SDK client, get a flag value and, as a base use case, establish separate code paths within your Lambda.
- getFlagsWithDynamo – This example uses DynamoDB as a persistent feature store for LaunchDarkly feature flags enabling the SDK client to evaluate flag results based upon the data stored in DynamoDB. It is configured to enable daemon mode, which means that it will not call out to LaunchDarkly for these initial values, which can decrease initialization cost and reduce external requests. To disable daemon mode, remove the
useLdd
flag from the configuration. - syncFlagsToDynamo – This is a simple Lambda designed to force cache flag values into DynamoDB enabling the use of daemon mode. While this solution works, it requires the function to be called (manually or programmatically) to start the initialization. The ideal solution for this problem is to use LauchDarkly's Relay Proxy.
For a complete guide to using LaunchDarkly in AWS serverless, including how to set up the Relay Proxy within your AWS environment and how to ensure that all events are captured by the SDKs within an ephemeral Lambda, visit this guide.
You'll need to install the CDK. For more details about the AWS CDK and options for installing it, please visit the AWS docs. Once the CDK is installed, follow these commands to bootstrap the deployment and then deploy it to your configured AWS account.
cd cdk
cdk bootstrap
cdk deploy
This will deploy all of the assets discussed above including the Lambda Layer with the LaunchDarkly SDKs and the three example Lambdas, with the Layer as a dependency.