AWS Lambda with DevCycle Feature Flags
DevCycle is purpose-built to work at edge, and just because you're building in serverless environments does not mean you need to stop using feature flags!
If you're a team on AWS and utilizing AWS Lambda, this document will outline exactly how you can get started with Feature Flags without any difficulty.
Example Project for AWS Lambda & DevCycle
To get you up and started, we've put together an example repository to be used to follow along with in this guide
Setting Up Feature Flags for the Example
Create a new feature on the DevCycle Dashboard
Choose a Release type, and as per our example code we’ll name this feature
By default, features will all have a boolean flag. Remove the default boolean flag by clicking the edit button next to it and “Delete” in the popped up modal
“Add Variable” with
Stringtype and put in the string as below
Scroll down to “Users & Targeting” for whichever environment's server key you selected
Change the targeting definition to say:
test_1and click “Save”
Now that the feature is set up, we’ll test it out in Lambda!
Setting Up AWS Lambda
- Clone the devcycle-lambda-example repo
- In this example, we are using a server key. Copy your server key as the
DVC.initializecall. (You can get your Server KE the server-side SDK key from the DevCycle dashboard)
const dvcClient = await DVC.initialize('<SERVER_TOKEN>').onClientInitialized()
- Create a new bucket for deployment artifacts by executing
- Build a Lambda layer that contains the function's runtime dependencies by executing
2-build-layer.sh. (Packaging dependencies in a layer reduces the size of the deployment package that you upload when you modify your code.)
- Deploy this application by executing
added 16 packages from 18 contributors and audited 18 packages in 0.926s
added 17 packages from 19 contributors and audited 19 packages in 0.916s
Uploading to e678bc216e6a0d510d661ca9ae2fd941 2737254 / 2737254.0 (100.00%)
Successfully packaged artifacts and wrote output template to file out.yml.
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - devcycle-lambda-example
- To invoke the function, execute
The output should show two events:
The first event is for the user with
user_id: test_1. This event should have its type changed to
my-new-event, as the user_id matches the targeting rule, resulting in the variation being ON.
Let the script invoke the function a few times and then press
To delete the application, you can run
5-cleanup.sh. The cleanup script deletes the application stack, which includes the function and execution role, and local build artifacts. You can choose to delete the bucket and function logs as well.
Deleted devcycle-lambda-example stack.
Delete deployment artifacts and bucket (lambda-artifacts-4475xmpl08ba7f8d)?y
Delete function logs? (log group /aws/lambda/devcycle-lambda-example-function-1RQTXMPLR0YSO)y