|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from botocore.config import Config |
| 4 | +from jmespath.functions import Functions, signature |
| 5 | + |
| 6 | +from aws_lambda_powertools.utilities.feature_flags import AppConfigStore, FeatureFlags |
| 7 | +from aws_lambda_powertools.utilities.typing import LambdaContext |
| 8 | + |
| 9 | +boto_config = Config(read_timeout=10, retries={"total_max_attempts": 2}) |
| 10 | + |
| 11 | + |
| 12 | +# Custom JMESPath functions |
| 13 | +class CustomFunctions(Functions): |
| 14 | + @signature({"types": ["object"]}) |
| 15 | + def _func_special_decoder(self, features): |
| 16 | + # You can add some logic here |
| 17 | + return features |
| 18 | + |
| 19 | + |
| 20 | +custom_jmespath_options = {"custom_functions": CustomFunctions()} |
| 21 | + |
| 22 | + |
| 23 | +app_config = AppConfigStore( |
| 24 | + environment="dev", |
| 25 | + application="product-catalogue", |
| 26 | + name="features", |
| 27 | + max_age=120, |
| 28 | + envelope="special_decoder(features)", # using a custom function defined in CustomFunctions Class |
| 29 | + sdk_config=boto_config, |
| 30 | + jmespath_options=custom_jmespath_options, |
| 31 | +) |
| 32 | + |
| 33 | +feature_flags = FeatureFlags(store=app_config) |
| 34 | + |
| 35 | + |
| 36 | +def lambda_handler(event: dict, context: LambdaContext): |
| 37 | + apply_discount: Any = feature_flags.evaluate(name="ten_percent_off_campaign", default=False) |
| 38 | + |
| 39 | + price: Any = event.get("price") |
| 40 | + |
| 41 | + if apply_discount: |
| 42 | + # apply 10% discount to product |
| 43 | + price = price * 0.9 |
| 44 | + |
| 45 | + return {"price": price} |
0 commit comments