Turn off AppConfig Feature Flag Automatically
Tags: aws, serverless, appconfig, showdev
Published: 2022-08-29 17:21:50

There can be many different ways to manage feature flags in your applications. [AWS AppConfig](https://docs.aws.amazon.com/appconfig/latest/userguide/what-is-appconfig.html) Feature Flag is a best solution because it is purposely built for this purpose. With the great support of [Lambda extensions](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-lambda-extensions.html), 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](https://aws.amazon.com/blogs/mt/using-aws-appconfig-feature-flags/)
2. Step functions [SDK integrations](https://docs.aws.amazon.com/step-functions/latest/dg/supported-services-awssdk.html) for AppConfig, DynamoDB and SES.
3. Lambda [function URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html)
4. [Amazon DynamoDB](https://aws.amazon.com/dynamodb/)
5. [Amazon Event Bridge](https://aws.amazon.com/eventbridge/)
6. [AWS CDK v2](https://docs.aws.amazon.com/cdk/v2/guide/home.html) with TypeScript as IAC

## High level system architecture

![High level system architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyda7ql6205ae3w9dxf1.png)

_Step Function State Machine:_
![State machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dv8x48s4eme7gqpbg4t9.png)

## 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](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-lambda-extensions.html) 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](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-appconfig-hostedconfigurationversion.html) 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](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-creating-deployment-strategy.html) 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](https://aws.amazon.com/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](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](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-lambda-extensions-versions.html).
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](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 <a href="https://unsplash.com/@twinsfisch?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Isabella and Zsa Fischer</a> on <a href="https://unsplash.com/s/photos/switch?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>