AWS Lambda Smart Feature Flags — Now with Time Based Conditions
Tags: aws, devops, appconfig, serverless
Published: 2023-01-31 16:36:44


![Time conditions](https://cdn-images-1.medium.com/max/3196/1*5GJtJC1M8IDGbNIleXyigA.png)

Feature flags change service behavior in runtime without redeploying the service code. Feature flags can improve your CI/CD process by enabling capabilities otherwise not possible, thus making them an enabler of DevOps and a crucial part of continuous integration. However, that requires a flexible and easy-to-use feature flags implementation such as AWS Lambda Powertools feature flags utility and AWS AppConfig.

This blog covers the newly added time conditions to the AWS Lambda Powertools feature flags utility open-source I contributed that leverages AWS AppConfig. You will learn how to define time-based feature flags that will change the behavior of your service at different times, hours and weekdays.



![[https://www.ranthebuilder.cloud/](https://www.ranthebuilder.cloud/)](https://cdn-images-1.medium.com/max/2000/0*cHbR4fu4aAHsI5b3.png)

**This blog was originally published on my website [“Ran The Builder”.](https://www.ranthebuilder.cloud/)**

## Re: cap

In my previous [post](https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-part-6-feature-flags-configuration-best-practices), I presented the added value of using feature flags as part of your CI/CD process and showcased a feature flags rule-based open-source SDK I contributed to AWS Lambda Powertools that leverages AWS AppConfig.

This post assumes you are familiar with the AWS Lambda Powertools feature flags utility and can deploy JSON configuration to AWS AppConfig.

In case you are not, feel free to watch my Conf42 DesSecOps 2022 conference talk in the video below:

{% embed https://www.youtube.com/watch?v=3IT4UzN9Jds %}

## Time-Based Feature Flags Use Case

Prior to this feature, you could define rules with 12 possible [conditions](https://awslabs.github.io/aws-lambda-powertools-python/2.6.0/utilities/feature_flags/#conditions) options.

You could enable features for users that are part of a specific tier, customer, region, and any string comparison or numeric comparison condition.

However, none of them included any time conditions.

I’ve been working with the AWS Lambda Powertools on this [feature](https://github.com/awslabs/aws-lambda-powertools-python/pull/1666) for some time to get it right and up to standards, and it finally got released in [v2.7.0](https://github.com/awslabs/aws-lambda-powertools-python/releases/tag/v2.7.0) and documented [here](https://awslabs.github.io/aws-lambda-powertools-python/2.7.0/utilities/feature_flags/#time-based-feature-flags).

There are three time-based conditions, and each has its use case:

 1. Datetime range condition

 2. Weekdays condition

 3. Hour ranges condition

You can also mix and match them and use any other condition out of the previous 12 conditions to create even more complex rules and use cases.

## Datetime Range Condition Use case

Let’s assume you want to enable a feature flag starting a precise date and turn it off automatically at a precise date; for example, you wish to enable a Christmas discount feature only during the Christmas days sale.

The following configuration provides this behavior:
{% gist https://gist.github.com/ran-isenberg/eee09e5b580aa1b0e41d4e79656abfce.js %}>  *If you don’t specify the timezone, UTC is assumed. If you wish to specify a timezone, you can use any [IANA](https://www.iana.org/time-zones) time zone (as originally specified in [PEP 615](https://peps.python.org/pep-0615/) ) as part of your rules definition. Powertools takes care of converting and calculate the correct timestamps for you.*

The Lambda function handler code looks like this:
{% gist https://gist.github.com/ran-isenberg/c92591f7472a0b41bc6786675236473a.js %}
AWS Lambda Powertools feature flags will check the current Lambda function time against the START and END conditions you defined to evaluate the rule.

## Week Days Condition Use Case

Let’s assume you want to enable a feature flag during specific weekdays; for example, you wish to provide a weekend discount for a recurring event. You can define specific days (not ranges) to enable the feature, in this case, during Saturday and Sunday only.

The following configuration provides this behavior:
{% gist https://gist.github.com/ran-isenberg/ab7ef73cc18e0efd3ccc9395f2cdde47.js %}
The Lambda function handler code looks like this:
{% gist https://gist.github.com/ran-isenberg/e87d97af8af7ac97f55d73dfd8b2880f.js %}
AWS Lambda Powertools feature flags will check the current Lambda function day against the START and END conditions you defined to evaluate the rule.

The same timezone IANA format applies here too.

## Hour Ranges Condition Use Case

Let’s assume you want to enable a feature flag during specific hours every day, for example, you wish to enable a happy hour feature between 17:00 to 19:00 every day as a recurring event.

The following configuration provides this behavior:
{% gist https://gist.github.com/ran-isenberg/d5f3d28c54ca704c922586e594dfb817.js %}
Let’s assume you want to enable a feature flag during specific hours every day; for example, you wish to enable a happy hour feature between 17:00 to 19:00 every day as a recurring event. You can add another condition, for the weekend days. The rule will match only if both conditions are matched, i.e, only when on a Saturday or Sunday AND the hour is 17:00 to 19:00 New York time. The order of the conditions does not matter.

The following configuration provides this behavior:
{% gist https://gist.github.com/ran-isenberg/1ce8ca5692b338691b9ddf98bcc6e53f.js %}
And in both cases, the Lambda function handler code looks like this:
{% gist https://gist.github.com/ran-isenberg/a64fa1f3cb3cd79002b93b9d51a51003.js %}
The same IANA timezone format applies here too.