Turn off AppConfig Feature Flag Automatically
Table of Contents
There can be many different ways to manage feature flags in your applications. AWS AppConfig Feature Flag is a best solution because it is purposely built for this purpose. With the great support of Lambda extensions, Feature Flag is quite easy to use in Serverless applications. In this post, I discuss how we can disable a feature flag that is already enabled based on the activities of the system.
In this application, there is a feature flag defined for a discount code. Initially, the feature flag is on and based on that, there is a banner visible in a web site.
Once users have consumed the discount code up to a predefined count, the feature flag will be auto-deactivated. So that banner will not be visible to the user.
And below services/functionalities are in use in this project:
- AWS AppConfig Feature Flags
- Step functions SDK integrations for AppConfig, DynamoDB and SES.
- Lambda function URLs
- Amazon DynamoDB
- Amazon Event Bridge
- AWS CDK v2 with TypeScript as IAC
Architecture #
How it works #
- To deploy the stack, the discount code needs to be provided as a parameter along with the maximum usage amount possible.
- Then, there will be a AppConfig feature flag created for this discount code and it will be initially enabled. Also, within the DynamoDB table, a record is created for this discount code.
- There is
Check Discount CodeValidity Lambda Function
to check if the feature flag is enabled. Within this Lambda function, using AppConfig Lambda extension it checks the particular feature flag is on. - Lambda Function URL is enabled to trigger this
Check Discount CodeValidity Lambda Function
. - To consume the discount code,
Consume Discount Code Lambda Function
will be used. - Using the Lambda Function URL of
Consume Discount Code Lambda Function
, the user triggers the function. It will publish a message to Event Bridge. - There is an Event Bridge rule to trigger when a discount code is consumed, which initializes Step Function execution.
- Within the Step Function, it first increments the usage count in the DynamoDB table.
- Next, it checks if the discount code has used up to its maximum usage.
- If the maximum amount is not reached, it simply goes into a
Pass
state and ends the execution. - If the maximum amount is reached, first an AppConfig
Hosted Configuration Version is created with feature flag value to
false
. - Next step, it starts deployment to apply the changes.
- Here, on stack creation, there is a Custom Deployment Strategy created to make the changes effected immediately.
- Then, as the last step, an email will be sent to the given
admin email
address using AWS SES.
How to set up #
You can deploy this project in your own AWS account. Please follow the steps below:
Prerequisites #
- AWS CLI and AWS CDK v2 need to be installed.
- Verified from-email address in SES
Steps #
- Clone the repository: https://github.com/pubudusj/feature-flag-auto-off
- Go to the directory
feature-flag-auto-off
. - Update the Makefile with required stack parameters.
- Run
make build
to install required dependencies. - Run
make deploy
to deploy the stack. - Once the project successfully deployed, there will be 2 urls in the output.
FeatureFlagAutoOffStack.checkDiscountCodeValidityApi
- This is to check if the feature flag is enabled or not.FeatureFlagAutoOffStack.consumeDiscountCodeApi
- This is to consume the discount code once.
Testing #
- First, you can check if the feature flag is enabled using the
checkDiscountCodeValidityApi
above. You can see it is enabled. - Then, you can consume the discount code by calling the
consumeDiscountCodeApi
. - When the discount code reaches the maximum no of usage count,
admin email address
will receive a notification. - When you check the feature flag status, you can see it is disabled.
Key points/Lessons learned #
-
AWS AppConfig Extension Layers for Lambda has different ARNs for each region. So, you need to select the correct ARN applicable for your region.
-
There are no official CDK v2 L2 constructs available for AppConfig yet. So, L1 constructs are used in this project.
If you would like to learn more about AWS AppConfig, this workshop will be a great resource: https://catalog.us-east-1.prod.workshops.aws/workshops/2ee2fc71-0618-479c-86dd-1d5fb168eb20/en-US
Please feel free to deploy this to your own AWS environment and share your experience with me. Keep building! Keep sharing!