DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Turn off AppConfig Feature Flag Automatically

Turn off AppConfig Feature Flag Automatically

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:

  1. AWS AppConfig Feature Flags
  2. Step functions SDK integrations for AppConfig, DynamoDB and SES.
  3. Lambda function URLs
  4. Amazon DynamoDB
  5. Amazon Event Bridge
  6. AWS CDK v2 with TypeScript as IAC

High level system architecture

High level system architecture

Step Function State Machine:
State machine

How it works

  1. To deploy the stack, the discount code needs to be provided as a parameter along with the maximum usage amount possible.
  2. 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.
  3. 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.
  4. Lambda Function URL is enabled to trigger this Check Discount CodeValidity Lambda Function.
  5. To consume the discount code, Consume Discount Code Lambda Function will be used.
  6. Using the Lambda Function URL of Consume Discount Code Lambda Function, the user triggers the function. It will publish a message to Event Bridge.
  7. There is an Event Bridge rule to trigger when a discount code is consumed, which initializes Step Function execution.
  8. Within the Step Function, it first increments the usage count in the DynamoDB table.
  9. Next, it checks if the discount code has used up to its maximum usage.
  10. If the maximum amount is not reached, it simply goes into a Pass state and ends the execution.
  11. If the maximum amount is reached, first an AppConfig Hosted Configuration Version is created with feature flag value to false.
  12. Next step, it starts deployment to apply the changes.
  13. Here, on stack creation, there is a Custom Deployment Strategy created to make the changes effected immediately.
  14. 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:

Pre-requisites
  1. AWS CLI and AWS CDK v2 need to be installed.
  2. Verified from-email address in SES
Steps
  1. Clone the repository: https://github.com/pubudusj/feature-flag-auto-off
  2. Go to the directory feature-flag-auto-off.
  3. Update the Makefile with required stack parameters.
  4. Run make build to install required dependencies.
  5. Run make deploy to deploy the stack.
  6. 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

  1. First, you can check if the feature flag is enabled using the checkDiscountCodeValidityApi above. You can see it is enabled.
  2. Then, you can consume the discount code by calling the consumeDiscountCodeApi.
  3. When the discount code reaches the maximum no of usage count, admin email address will receive a notification.
  4. When you check the feature flag status, you can see it is disabled.

Key points/Lessons learned

  1. AWS AppConfig Extension Layers for Lambda has different ARNs for each region. So, you need to select the correct ARN applicable for your region.
  2. 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

Feedback are welcome

Please feel free to deploy this to your own AWS environment and share your experience with me. And you can connect with me in LinkedIn: https://www.linkedin.com/in/pubudusj and Twitter https://twitter.com/pubudusj

Keep building! Keep sharing!

Photo by Isabella and Zsa Fischer on Unsplash

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.