Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/minimal_schema.json +9 -0
- testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/timebased_feature_event.json +5 -0
- testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/timebased_happyhour_feature.py +35 -0
- testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/timebased_happyhour_features.json +21 -0
- testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/working_with_own_s3_store_provider.py +22 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/arm64/amplify.txt +21 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/arm64/sam.yaml +12 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/arm64/serverless.yml +13 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/arm64/terraform.tf +41 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/cdk_sar.py +39 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/sam.yaml +19 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/scoped_down_iam.yaml +55 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/serverless.yml +20 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/terraform.tf +41 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/amplify.txt +21 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/cdk_x86.py +22 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/pulumi_x86.py +34 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/sam.yaml +11 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/serverless.yml +13 -0
- testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/terraform.tf +40 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/bring_your_own_persistent_store.py +125 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/customize_persistence_layer.py +23 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/customize_persistence_layer_redis.py +24 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/getting_started_with_idempotency.py +39 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/getting_started_with_idempotency_payload.json +4 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/integrate_idempotency_with_batch_processor.py +33 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/integrate_idempotency_with_batch_processor_payload.json +26 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/integrate_idempotency_with_validator_payload.json +69 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_composite_key.py +16 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_composite_key_payload.json +3 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_custom_config.py +23 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_custom_config_payload.json +3 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_custom_session.py +23 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_dataclass_explicitly_output_serializer.py +50 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_exceptions.py +38 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_idempotency_key_required.py +20 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_idempotency_key_required_payload_error.json +7 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_idempotency_key_required_payload_success.json +7 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_idempotent_function_custom_output_serializer.py +64 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_local_cache_payload.json +3 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_payload_subset_payload.json +30 -0
- testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_response_hook.py +56 -0
- testbed/aws-powertools__powertools-lambda-python/examples/logger/sam/template.yaml +24 -0
- testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_and_remove_keys.py +12 -0
- testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_and_remove_keys_output.json +20 -0
- testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys.py +15 -0
- testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys_extra_output.json +8 -0
- testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys_kwargs_output.json +8 -0
- testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys_vs_extra.py +28 -0
- testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys_vs_extra_output.json +21 -0
testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/minimal_schema.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"global_feature": {
|
| 3 |
+
"default": true
|
| 4 |
+
},
|
| 5 |
+
"non_boolean_global_feature": {
|
| 6 |
+
"default": {"group": "read-only"},
|
| 7 |
+
"boolean_type": false
|
| 8 |
+
}
|
| 9 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/timebased_feature_event.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"username": "rubefons",
|
| 3 |
+
"tier": "premium",
|
| 4 |
+
"basked_id": "random_id"
|
| 5 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/timebased_happyhour_feature.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from aws_lambda_powertools.utilities.feature_flags import AppConfigStore, FeatureFlags
|
| 2 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 3 |
+
|
| 4 |
+
app_config = AppConfigStore(environment="dev", application="product-catalogue", name="features")
|
| 5 |
+
|
| 6 |
+
feature_flags = FeatureFlags(store=app_config)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def lambda_handler(event: dict, context: LambdaContext):
|
| 10 |
+
"""
|
| 11 |
+
This feature flag is enabled under the following conditions:
|
| 12 |
+
- Every day between 17:00 to 19:00 in Europe/Copenhagen timezone
|
| 13 |
+
|
| 14 |
+
Rule condition to be evaluated:
|
| 15 |
+
"conditions": [
|
| 16 |
+
{
|
| 17 |
+
"action": "SCHEDULE_BETWEEN_TIME_RANGE",
|
| 18 |
+
"key": "CURRENT_TIME",
|
| 19 |
+
"value": {
|
| 20 |
+
"START": "17:00",
|
| 21 |
+
"END": "19:00",
|
| 22 |
+
"TIMEZONE": "Europe/Copenhagen"
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
]
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
# Checking if the happy hour discount is enable
|
| 29 |
+
is_happy_hour = feature_flags.evaluate(name="happy_hour", default=False)
|
| 30 |
+
|
| 31 |
+
if is_happy_hour:
|
| 32 |
+
# Enable special discount on happy hour:
|
| 33 |
+
return {"message": "The happy hour discount is enabled."}
|
| 34 |
+
|
| 35 |
+
return {"message": "The happy hour discount is not enabled."}
|
testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/timebased_happyhour_features.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"happy_hour": {
|
| 3 |
+
"default": false,
|
| 4 |
+
"rules": {
|
| 5 |
+
"is happy hour": {
|
| 6 |
+
"when_match": true,
|
| 7 |
+
"conditions": [
|
| 8 |
+
{
|
| 9 |
+
"action": "SCHEDULE_BETWEEN_TIME_RANGE",
|
| 10 |
+
"key": "CURRENT_TIME",
|
| 11 |
+
"value": {
|
| 12 |
+
"START": "17:00",
|
| 13 |
+
"END": "19:00",
|
| 14 |
+
"TIMEZONE": "Europe/Copenhagen"
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
]
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/feature_flags/src/working_with_own_s3_store_provider.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
+
|
| 3 |
+
from custom_s3_store_provider import S3StoreProvider
|
| 4 |
+
|
| 5 |
+
from aws_lambda_powertools.utilities.feature_flags import FeatureFlags
|
| 6 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 7 |
+
|
| 8 |
+
s3_config_store = S3StoreProvider("your-bucket-name", "working_with_own_s3_store_provider_features.json")
|
| 9 |
+
|
| 10 |
+
feature_flags = FeatureFlags(store=s3_config_store)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def lambda_handler(event: dict, context: LambdaContext):
|
| 14 |
+
apply_discount: Any = feature_flags.evaluate(name="ten_percent_off_campaign", default=False)
|
| 15 |
+
|
| 16 |
+
price: Any = event.get("price")
|
| 17 |
+
|
| 18 |
+
if apply_discount:
|
| 19 |
+
# apply 10% discount to product
|
| 20 |
+
price = price * 0.9
|
| 21 |
+
|
| 22 |
+
return {"price": price}
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/arm64/amplify.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Create a new one with the layer
|
| 2 |
+
❯ amplify add function
|
| 3 |
+
? Select which capability you want to add: Lambda function (serverless function)
|
| 4 |
+
? Provide an AWS Lambda function name: <NAME-OF-FUNCTION>
|
| 5 |
+
? Choose the runtime that you want to use: Python
|
| 6 |
+
? Do you want to configure advanced settings? Yes
|
| 7 |
+
...
|
| 8 |
+
? Do you want to enable Lambda layers for this function? Yes
|
| 9 |
+
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-arm64:4
|
| 10 |
+
❯ amplify push -y
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Updating an existing function and add the layer
|
| 14 |
+
❯ amplify update function
|
| 15 |
+
? Select the Lambda function you want to update test2
|
| 16 |
+
General information
|
| 17 |
+
- Name: <NAME-OF-FUNCTION>
|
| 18 |
+
? Which setting do you want to update? Lambda layers configuration
|
| 19 |
+
? Do you want to enable Lambda layers for this function? Yes
|
| 20 |
+
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-arm64:4
|
| 21 |
+
? Do you want to edit the local lambda function now? No
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/arm64/sam.yaml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
AWSTemplateFormatVersion: '2010-09-09'
|
| 2 |
+
Transform: AWS::Serverless-2016-10-31
|
| 3 |
+
|
| 4 |
+
Resources:
|
| 5 |
+
MyLambdaFunction:
|
| 6 |
+
Type: AWS::Serverless::Function
|
| 7 |
+
Properties:
|
| 8 |
+
Architectures: [arm64]
|
| 9 |
+
Runtime: python3.12
|
| 10 |
+
Handler: app.lambda_handler
|
| 11 |
+
Layers:
|
| 12 |
+
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-arm64:4
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/arm64/serverless.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
service: powertools-lambda
|
| 2 |
+
|
| 3 |
+
provider:
|
| 4 |
+
name: aws
|
| 5 |
+
runtime: python3.12
|
| 6 |
+
region: us-east-1
|
| 7 |
+
|
| 8 |
+
functions:
|
| 9 |
+
powertools:
|
| 10 |
+
handler: lambda_function.lambda_handler
|
| 11 |
+
architecture: arm64
|
| 12 |
+
layers:
|
| 13 |
+
- arn:aws:lambda:${aws:region}:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-arm64:4
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/arm64/terraform.tf
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
terraform {
|
| 2 |
+
required_version = "~> 1.0.5"
|
| 3 |
+
required_providers {
|
| 4 |
+
aws = "~> 3.50.0"
|
| 5 |
+
}
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
provider "aws" {
|
| 9 |
+
region = "{region}"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
resource "aws_iam_role" "iam_for_lambda" {
|
| 13 |
+
name = "iam_for_lambda"
|
| 14 |
+
|
| 15 |
+
assume_role_policy = <<EOF
|
| 16 |
+
{
|
| 17 |
+
"Version": "2012-10-17",
|
| 18 |
+
"Statement": [
|
| 19 |
+
{
|
| 20 |
+
"Action": "sts:AssumeRole",
|
| 21 |
+
"Principal": {
|
| 22 |
+
"Service": "lambda.amazonaws.com"
|
| 23 |
+
},
|
| 24 |
+
"Effect": "Allow"
|
| 25 |
+
}
|
| 26 |
+
]
|
| 27 |
+
}
|
| 28 |
+
EOF
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
resource "aws_lambda_function" "test_lambda" {
|
| 32 |
+
filename = "lambda_function_payload.zip"
|
| 33 |
+
function_name = "lambda_function_name"
|
| 34 |
+
role = aws_iam_role.iam_for_lambda.arn
|
| 35 |
+
handler = "index.test"
|
| 36 |
+
runtime = "python3.12"
|
| 37 |
+
layers = ["arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-arm64:4"]
|
| 38 |
+
architectures = ["arm64"]
|
| 39 |
+
|
| 40 |
+
source_code_hash = filebase64sha256("lambda_function_payload.zip")
|
| 41 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/cdk_sar.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from aws_cdk import Stack, aws_lambda, aws_sam
|
| 2 |
+
from constructs import Construct
|
| 3 |
+
|
| 4 |
+
POWERTOOLS_BASE_NAME = "AWSLambdaPowertools"
|
| 5 |
+
# Find latest from github.com/aws-powertools/powertools-lambda-python/releases
|
| 6 |
+
POWERTOOLS_VER = "2.43.1"
|
| 7 |
+
POWERTOOLS_ARN = (
|
| 8 |
+
"arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class SampleApp(Stack):
|
| 13 |
+
|
| 14 |
+
def __init__(self, scope: Construct, id_: str) -> None:
|
| 15 |
+
super().__init__(scope, id_)
|
| 16 |
+
|
| 17 |
+
# Launches SAR App as CloudFormation nested stack and return Lambda Layer
|
| 18 |
+
powertools_app = aws_sam.CfnApplication(
|
| 19 |
+
self,
|
| 20 |
+
f"{POWERTOOLS_BASE_NAME}Application",
|
| 21 |
+
location={"applicationId": POWERTOOLS_ARN, "semanticVersion": POWERTOOLS_VER},
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
powertools_layer_arn = powertools_app.get_att("Outputs.LayerVersionArn").to_string()
|
| 25 |
+
powertools_layer_version = aws_lambda.LayerVersion.from_layer_version_arn(
|
| 26 |
+
self,
|
| 27 |
+
f"{POWERTOOLS_BASE_NAME}",
|
| 28 |
+
powertools_layer_arn,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
aws_lambda.Function(
|
| 32 |
+
self,
|
| 33 |
+
"sample-app-lambda",
|
| 34 |
+
runtime=aws_lambda.Runtime.PYTHON_3_12,
|
| 35 |
+
function_name="sample-lambda",
|
| 36 |
+
code=aws_lambda.Code.from_asset("lambda"),
|
| 37 |
+
handler="hello.handler",
|
| 38 |
+
layers=[powertools_layer_version],
|
| 39 |
+
)
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/sam.yaml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
AWSTemplateFormatVersion: '2010-09-09'
|
| 2 |
+
Transform: AWS::Serverless-2016-10-31
|
| 3 |
+
|
| 4 |
+
Resources:
|
| 5 |
+
AwsLambdaPowertoolsPythonLayer:
|
| 6 |
+
Type: AWS::Serverless::Application
|
| 7 |
+
Properties:
|
| 8 |
+
Location:
|
| 9 |
+
ApplicationId: arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer
|
| 10 |
+
SemanticVersion: 2.43.1 # change to latest semantic version available in SAR
|
| 11 |
+
|
| 12 |
+
MyLambdaFunction:
|
| 13 |
+
Type: AWS::Serverless::Function
|
| 14 |
+
Properties:
|
| 15 |
+
Runtime: python3.12
|
| 16 |
+
Handler: app.lambda_handler
|
| 17 |
+
Layers:
|
| 18 |
+
# fetch Layer ARN from SAR App stack output
|
| 19 |
+
- !GetAtt AwsLambdaPowertoolsPythonLayer.Outputs.LayerVersionArn
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/scoped_down_iam.yaml
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
AWSTemplateFormatVersion: "2010-09-09"
|
| 2 |
+
Resources:
|
| 3 |
+
PowertoolsLayerIamRole:
|
| 4 |
+
Type: "AWS::IAM::Role"
|
| 5 |
+
Properties:
|
| 6 |
+
AssumeRolePolicyDocument:
|
| 7 |
+
Version: "2012-10-17"
|
| 8 |
+
Statement:
|
| 9 |
+
- Effect: "Allow"
|
| 10 |
+
Principal:
|
| 11 |
+
Service:
|
| 12 |
+
- "cloudformation.amazonaws.com"
|
| 13 |
+
Action:
|
| 14 |
+
- "sts:AssumeRole"
|
| 15 |
+
Path: "/"
|
| 16 |
+
|
| 17 |
+
PowertoolsLayerIamPolicy:
|
| 18 |
+
Type: "AWS::IAM::Policy"
|
| 19 |
+
Properties:
|
| 20 |
+
PolicyName: PowertoolsLambdaLayerPolicy
|
| 21 |
+
PolicyDocument:
|
| 22 |
+
Version: "2012-10-17"
|
| 23 |
+
Statement:
|
| 24 |
+
- Sid: CloudFormationTransform
|
| 25 |
+
Effect: Allow
|
| 26 |
+
Action: cloudformation:CreateChangeSet
|
| 27 |
+
Resource:
|
| 28 |
+
- arn:aws:cloudformation:us-east-1:aws:transform/Serverless-2016-10-31
|
| 29 |
+
- Sid: GetCfnTemplate
|
| 30 |
+
Effect: Allow
|
| 31 |
+
Action:
|
| 32 |
+
- serverlessrepo:CreateCloudFormationTemplate
|
| 33 |
+
- serverlessrepo:GetCloudFormationTemplate
|
| 34 |
+
Resource:
|
| 35 |
+
# this is arn of the Powertools for AWS Lambda (Python) SAR app
|
| 36 |
+
- arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer-v3-python312-x86_64
|
| 37 |
+
- Sid: S3AccessLayer
|
| 38 |
+
Effect: Allow
|
| 39 |
+
Action:
|
| 40 |
+
- s3:GetObject
|
| 41 |
+
Resource:
|
| 42 |
+
# AWS publishes to an external S3 bucket locked down to your account ID
|
| 43 |
+
# The below example is us publishing Powertools for AWS Lambda (Python)
|
| 44 |
+
# Bucket: awsserverlessrepo-changesets-plntc6bfnfj
|
| 45 |
+
# Key: *****/arn:aws:serverlessrepo:eu-west-1:057560766410:applications-aws-lambda-powertools-python-layer-versions-1.10.2/aeeccf50-****-****-****-*********
|
| 46 |
+
- arn:aws:s3:::awsserverlessrepo-changesets-*/*
|
| 47 |
+
- Sid: GetLayerVersion
|
| 48 |
+
Effect: Allow
|
| 49 |
+
Action:
|
| 50 |
+
- lambda:PublishLayerVersion
|
| 51 |
+
- lambda:GetLayerVersion
|
| 52 |
+
Resource:
|
| 53 |
+
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:aws-lambda-powertools-python-layer*
|
| 54 |
+
Roles:
|
| 55 |
+
- Ref: "PowertoolsLayerIamRole"
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/serverless.yml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
service: powertools-lambda
|
| 2 |
+
|
| 3 |
+
provider:
|
| 4 |
+
name: aws
|
| 5 |
+
runtime: python3.12
|
| 6 |
+
region: us-east-1
|
| 7 |
+
|
| 8 |
+
functions:
|
| 9 |
+
powertools:
|
| 10 |
+
handler: lambda_function.lambda_handler
|
| 11 |
+
layers:
|
| 12 |
+
- !GetAtt AwsLambdaPowertoolsPythonLayer.Outputs.LayerVersionArn
|
| 13 |
+
|
| 14 |
+
resources:
|
| 15 |
+
- AwsLambdaPowertoolsPythonLayer:
|
| 16 |
+
Type: AWS::Serverless::Application
|
| 17 |
+
Properties:
|
| 18 |
+
Location:
|
| 19 |
+
ApplicationId: arn:aws:serverlessrepo:us-east-1:057560766410:applications/aws-lambda-powertools-python-layer
|
| 20 |
+
SemanticVersion: 2.43.1
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/sar/terraform.tf
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
terraform {
|
| 2 |
+
required_version = "~> 0.13"
|
| 3 |
+
required_providers {
|
| 4 |
+
aws = "~> 3.50.0"
|
| 5 |
+
}
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
provider "aws" {
|
| 9 |
+
region = "us-east-1"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
resource "aws_serverlessapplicationrepository_cloudformation_stack" "deploy_sar_stack" {
|
| 13 |
+
name = "aws-lambda-powertools-python-layer"
|
| 14 |
+
|
| 15 |
+
application_id = data.aws_serverlessapplicationrepository_application.sar_app.application_id
|
| 16 |
+
semantic_version = data.aws_serverlessapplicationrepository_application.sar_app.semantic_version
|
| 17 |
+
capabilities = [
|
| 18 |
+
"CAPABILITY_IAM",
|
| 19 |
+
"CAPABILITY_NAMED_IAM"
|
| 20 |
+
]
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
data "aws_serverlessapplicationrepository_application" "sar_app" {
|
| 24 |
+
application_id = "arn:aws:serverlessrepo:us-east-1:057560766410:applications/aws-lambda-powertools-python-layer"
|
| 25 |
+
semantic_version = var.aws_powertools_version
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
variable "aws_powertools_version" {
|
| 29 |
+
type = string
|
| 30 |
+
default = "2.43.1"
|
| 31 |
+
description = "The Powertools for AWS Lambda (Python) release version"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
output "deployed_powertools_sar_version" {
|
| 35 |
+
value = data.aws_serverlessapplicationrepository_application.sar_app.semantic_version
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# Fetch Powertools for AWS Lambda (Python) Layer ARN from deployed SAR App
|
| 39 |
+
output "aws_lambda_powertools_layer_arn" {
|
| 40 |
+
value = aws_serverlessapplicationrepository_cloudformation_stack.deploy_sar_stack.outputs.LayerVersionArn
|
| 41 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/amplify.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Create a new one with the layer
|
| 2 |
+
❯ amplify add function
|
| 3 |
+
? Select which capability you want to add: Lambda function (serverless function)
|
| 4 |
+
? Provide an AWS Lambda function name: <NAME-OF-FUNCTION>
|
| 5 |
+
? Choose the runtime that you want to use: Python
|
| 6 |
+
? Do you want to configure advanced settings? Yes
|
| 7 |
+
...
|
| 8 |
+
? Do you want to enable Lambda layers for this function? Yes
|
| 9 |
+
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-x86_64:4
|
| 10 |
+
❯ amplify push -y
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Updating an existing function and add the layer
|
| 14 |
+
❯ amplify update function
|
| 15 |
+
? Select the Lambda function you want to update test2
|
| 16 |
+
General information
|
| 17 |
+
- Name: <NAME-OF-FUNCTION>
|
| 18 |
+
? Which setting do you want to update? Lambda layers configuration
|
| 19 |
+
? Do you want to enable Lambda layers for this function? Yes
|
| 20 |
+
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-x86_64:4
|
| 21 |
+
? Do you want to edit the local lambda function now? No
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/cdk_x86.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from aws_cdk import Aws, Stack, aws_lambda
|
| 2 |
+
from constructs import Construct
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class SampleApp(Stack):
|
| 6 |
+
|
| 7 |
+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
|
| 8 |
+
super().__init__(scope, construct_id, **kwargs)
|
| 9 |
+
|
| 10 |
+
powertools_layer = aws_lambda.LayerVersion.from_layer_version_arn(
|
| 11 |
+
self,
|
| 12 |
+
id="lambda-powertools",
|
| 13 |
+
layer_version_arn=f"arn:aws:lambda:{Aws.REGION}:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-x86_64:4",
|
| 14 |
+
)
|
| 15 |
+
aws_lambda.Function(
|
| 16 |
+
self,
|
| 17 |
+
"sample-app-lambda",
|
| 18 |
+
runtime=aws_lambda.Runtime.PYTHON_3_12,
|
| 19 |
+
layers=[powertools_layer],
|
| 20 |
+
code=aws_lambda.Code.from_asset("lambda"),
|
| 21 |
+
handler="hello.handler",
|
| 22 |
+
)
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/pulumi_x86.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
import pulumi
|
| 4 |
+
import pulumi_aws as aws
|
| 5 |
+
|
| 6 |
+
role = aws.iam.Role(
|
| 7 |
+
"role",
|
| 8 |
+
assume_role_policy=json.dumps(
|
| 9 |
+
{
|
| 10 |
+
"Version": "2012-10-17",
|
| 11 |
+
"Statement": [
|
| 12 |
+
{"Action": "sts:AssumeRole", "Principal": {"Service": "lambda.amazonaws.com"}, "Effect": "Allow"},
|
| 13 |
+
],
|
| 14 |
+
},
|
| 15 |
+
),
|
| 16 |
+
managed_policy_arns=[aws.iam.ManagedPolicy.AWS_LAMBDA_BASIC_EXECUTION_ROLE],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
lambda_function = aws.lambda_.Function(
|
| 20 |
+
"function",
|
| 21 |
+
layers=[
|
| 22 |
+
pulumi.Output.concat(
|
| 23 |
+
"arn:aws:lambda:",
|
| 24 |
+
aws.get_region_output().name,
|
| 25 |
+
":017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-x86_64:4",
|
| 26 |
+
),
|
| 27 |
+
],
|
| 28 |
+
tracing_config={"mode": "Active"},
|
| 29 |
+
runtime=aws.lambda_.Runtime.PYTHON3D12,
|
| 30 |
+
handler="index.handler",
|
| 31 |
+
role=role.arn,
|
| 32 |
+
architectures=["x86_64"],
|
| 33 |
+
code=pulumi.FileArchive("lambda_function_payload.zip"),
|
| 34 |
+
)
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/sam.yaml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
AWSTemplateFormatVersion: '2010-09-09'
|
| 2 |
+
Transform: AWS::Serverless-2016-10-31
|
| 3 |
+
|
| 4 |
+
Resources:
|
| 5 |
+
MyLambdaFunction:
|
| 6 |
+
Type: AWS::Serverless::Function
|
| 7 |
+
Properties:
|
| 8 |
+
Runtime: python3.12
|
| 9 |
+
Handler: app.lambda_handler
|
| 10 |
+
Layers:
|
| 11 |
+
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-x86_64:4
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/serverless.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
service: powertools-lambda
|
| 2 |
+
|
| 3 |
+
provider:
|
| 4 |
+
name: aws
|
| 5 |
+
runtime: python3.12
|
| 6 |
+
region: us-east-1
|
| 7 |
+
|
| 8 |
+
functions:
|
| 9 |
+
powertools:
|
| 10 |
+
handler: lambda_function.lambda_handler
|
| 11 |
+
architecture: arm64
|
| 12 |
+
layers:
|
| 13 |
+
- arn:aws:lambda:${aws:region}:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-x86_64:4
|
testbed/aws-powertools__powertools-lambda-python/examples/homepage/install/x86_64/terraform.tf
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
terraform {
|
| 2 |
+
required_version = "~> 1.0.5"
|
| 3 |
+
required_providers {
|
| 4 |
+
aws = "~> 3.50.0"
|
| 5 |
+
}
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
provider "aws" {
|
| 9 |
+
region = "{region}"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
resource "aws_iam_role" "iam_for_lambda" {
|
| 13 |
+
name = "iam_for_lambda"
|
| 14 |
+
|
| 15 |
+
assume_role_policy = <<EOF
|
| 16 |
+
{
|
| 17 |
+
"Version": "2012-10-17",
|
| 18 |
+
"Statement": [
|
| 19 |
+
{
|
| 20 |
+
"Action": "sts:AssumeRole",
|
| 21 |
+
"Principal": {
|
| 22 |
+
"Service": "lambda.amazonaws.com"
|
| 23 |
+
},
|
| 24 |
+
"Effect": "Allow"
|
| 25 |
+
}
|
| 26 |
+
]
|
| 27 |
+
}
|
| 28 |
+
EOF
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
resource "aws_lambda_function" "test_lambda" {
|
| 32 |
+
filename = "lambda_function_payload.zip"
|
| 33 |
+
function_name = "lambda_function_name"
|
| 34 |
+
role = aws_iam_role.iam_for_lambda.arn
|
| 35 |
+
handler = "index.test"
|
| 36 |
+
runtime = "python3.12"
|
| 37 |
+
layers = ["arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-x86_64:4"]
|
| 38 |
+
|
| 39 |
+
source_code_hash = filebase64sha256("lambda_function_payload.zip")
|
| 40 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/bring_your_own_persistent_store.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import logging
|
| 3 |
+
from typing import Any, Dict, Optional
|
| 4 |
+
|
| 5 |
+
import boto3
|
| 6 |
+
from botocore.config import Config
|
| 7 |
+
|
| 8 |
+
from aws_lambda_powertools.utilities.idempotency import BasePersistenceLayer
|
| 9 |
+
from aws_lambda_powertools.utilities.idempotency.exceptions import (
|
| 10 |
+
IdempotencyItemAlreadyExistsError,
|
| 11 |
+
IdempotencyItemNotFoundError,
|
| 12 |
+
)
|
| 13 |
+
from aws_lambda_powertools.utilities.idempotency.persistence.base import DataRecord
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class MyOwnPersistenceLayer(BasePersistenceLayer):
|
| 19 |
+
def __init__(
|
| 20 |
+
self,
|
| 21 |
+
table_name: str,
|
| 22 |
+
key_attr: str = "id",
|
| 23 |
+
expiry_attr: str = "expiration",
|
| 24 |
+
status_attr: str = "status",
|
| 25 |
+
data_attr: str = "data",
|
| 26 |
+
validation_key_attr: str = "validation",
|
| 27 |
+
boto_config: Optional[Config] = None,
|
| 28 |
+
boto3_session: Optional[boto3.session.Session] = None,
|
| 29 |
+
):
|
| 30 |
+
boto3_session = boto3_session or boto3.session.Session()
|
| 31 |
+
self._ddb_resource = boto3_session.resource("dynamodb", config=boto_config)
|
| 32 |
+
self.table_name = table_name
|
| 33 |
+
self.table = self._ddb_resource.Table(self.table_name)
|
| 34 |
+
self.key_attr = key_attr
|
| 35 |
+
self.expiry_attr = expiry_attr
|
| 36 |
+
self.status_attr = status_attr
|
| 37 |
+
self.data_attr = data_attr
|
| 38 |
+
self.validation_key_attr = validation_key_attr
|
| 39 |
+
super().__init__()
|
| 40 |
+
|
| 41 |
+
def _item_to_data_record(self, item: Dict[str, Any]) -> DataRecord:
|
| 42 |
+
"""
|
| 43 |
+
Translate raw item records from DynamoDB to DataRecord
|
| 44 |
+
|
| 45 |
+
Parameters
|
| 46 |
+
----------
|
| 47 |
+
item: Dict[str, Union[str, int]]
|
| 48 |
+
Item format from dynamodb response
|
| 49 |
+
|
| 50 |
+
Returns
|
| 51 |
+
-------
|
| 52 |
+
DataRecord
|
| 53 |
+
representation of item
|
| 54 |
+
|
| 55 |
+
"""
|
| 56 |
+
return DataRecord(
|
| 57 |
+
idempotency_key=item[self.key_attr],
|
| 58 |
+
status=item[self.status_attr],
|
| 59 |
+
expiry_timestamp=item[self.expiry_attr],
|
| 60 |
+
response_data=item.get(self.data_attr, ""),
|
| 61 |
+
payload_hash=item.get(self.validation_key_attr, ""),
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
def _get_record(self, idempotency_key) -> DataRecord:
|
| 65 |
+
response = self.table.get_item(Key={self.key_attr: idempotency_key}, ConsistentRead=True)
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
item = response["Item"]
|
| 69 |
+
except KeyError:
|
| 70 |
+
raise IdempotencyItemNotFoundError
|
| 71 |
+
return self._item_to_data_record(item)
|
| 72 |
+
|
| 73 |
+
def _put_record(self, data_record: DataRecord) -> None:
|
| 74 |
+
item = {
|
| 75 |
+
self.key_attr: data_record.idempotency_key,
|
| 76 |
+
self.expiry_attr: data_record.expiry_timestamp,
|
| 77 |
+
self.status_attr: data_record.status,
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
if self.payload_validation_enabled:
|
| 81 |
+
item[self.validation_key_attr] = data_record.payload_hash
|
| 82 |
+
|
| 83 |
+
now = datetime.datetime.now()
|
| 84 |
+
try:
|
| 85 |
+
logger.debug(f"Putting record for idempotency key: {data_record.idempotency_key}")
|
| 86 |
+
self.table.put_item(
|
| 87 |
+
Item=item,
|
| 88 |
+
ConditionExpression=f"attribute_not_exists({self.key_attr}) OR {self.expiry_attr} < :now",
|
| 89 |
+
ExpressionAttributeValues={":now": int(now.timestamp())},
|
| 90 |
+
)
|
| 91 |
+
except self._ddb_resource.meta.client.exceptions.ConditionalCheckFailedException:
|
| 92 |
+
logger.debug(f"Failed to put record for already existing idempotency key: {data_record.idempotency_key}")
|
| 93 |
+
raise IdempotencyItemAlreadyExistsError
|
| 94 |
+
|
| 95 |
+
def _update_record(self, data_record: DataRecord):
|
| 96 |
+
logger.debug(f"Updating record for idempotency key: {data_record.idempotency_key}")
|
| 97 |
+
update_expression = "SET #response_data = :response_data, #expiry = :expiry, #status = :status"
|
| 98 |
+
expression_attr_values = {
|
| 99 |
+
":expiry": data_record.expiry_timestamp,
|
| 100 |
+
":response_data": data_record.response_data,
|
| 101 |
+
":status": data_record.status,
|
| 102 |
+
}
|
| 103 |
+
expression_attr_names = {
|
| 104 |
+
"#response_data": self.data_attr,
|
| 105 |
+
"#expiry": self.expiry_attr,
|
| 106 |
+
"#status": self.status_attr,
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
if self.payload_validation_enabled:
|
| 110 |
+
update_expression += ", #validation_key = :validation_key"
|
| 111 |
+
expression_attr_values[":validation_key"] = data_record.payload_hash
|
| 112 |
+
expression_attr_names["#validation_key"] = self.validation_key_attr
|
| 113 |
+
|
| 114 |
+
self.table.update_item(
|
| 115 |
+
Key={self.key_attr: data_record.idempotency_key},
|
| 116 |
+
UpdateExpression=update_expression,
|
| 117 |
+
ExpressionAttributeValues=expression_attr_values,
|
| 118 |
+
ExpressionAttributeNames=expression_attr_names,
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
def _delete_record(self, data_record: DataRecord) -> None:
|
| 122 |
+
logger.debug(f"Deleting record for idempotency key: {data_record.idempotency_key}")
|
| 123 |
+
self.table.delete_item(
|
| 124 |
+
Key={self.key_attr: data_record.idempotency_key},
|
| 125 |
+
)
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/customize_persistence_layer.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 4 |
+
DynamoDBPersistenceLayer,
|
| 5 |
+
idempotent,
|
| 6 |
+
)
|
| 7 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 8 |
+
|
| 9 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 10 |
+
persistence_layer = DynamoDBPersistenceLayer(
|
| 11 |
+
table_name=table,
|
| 12 |
+
key_attr="idempotency_key",
|
| 13 |
+
expiry_attr="expires_at",
|
| 14 |
+
in_progress_expiry_attr="in_progress_expires_at",
|
| 15 |
+
status_attr="current_status",
|
| 16 |
+
data_attr="result_data",
|
| 17 |
+
validation_key_attr="validation_key",
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@idempotent(persistence_store=persistence_layer)
|
| 22 |
+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
|
| 23 |
+
return event
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/customize_persistence_layer_redis.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 4 |
+
idempotent,
|
| 5 |
+
)
|
| 6 |
+
from aws_lambda_powertools.utilities.idempotency.persistence.redis import (
|
| 7 |
+
RedisCachePersistenceLayer,
|
| 8 |
+
)
|
| 9 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 10 |
+
|
| 11 |
+
redis_endpoint = os.getenv("REDIS_CLUSTER_ENDPOINT", "localhost")
|
| 12 |
+
persistence_layer = RedisCachePersistenceLayer(
|
| 13 |
+
host=redis_endpoint,
|
| 14 |
+
port=6379,
|
| 15 |
+
in_progress_expiry_attr="in_progress_expiration",
|
| 16 |
+
status_attr="status",
|
| 17 |
+
data_attr="data",
|
| 18 |
+
validation_key_attr="validation",
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@idempotent(persistence_store=persistence_layer)
|
| 23 |
+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
|
| 24 |
+
return event
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/getting_started_with_idempotency.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dataclasses import dataclass, field
|
| 3 |
+
from uuid import uuid4
|
| 4 |
+
|
| 5 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 6 |
+
DynamoDBPersistenceLayer,
|
| 7 |
+
idempotent,
|
| 8 |
+
)
|
| 9 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 10 |
+
|
| 11 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 12 |
+
persistence_layer = DynamoDBPersistenceLayer(table_name=table)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class Payment:
|
| 17 |
+
user_id: str
|
| 18 |
+
product_id: str
|
| 19 |
+
payment_id: str = field(default_factory=lambda: f"{uuid4()}")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class PaymentError(Exception): ...
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@idempotent(persistence_store=persistence_layer)
|
| 26 |
+
def lambda_handler(event: dict, context: LambdaContext):
|
| 27 |
+
try:
|
| 28 |
+
payment: Payment = create_subscription_payment(event)
|
| 29 |
+
return {
|
| 30 |
+
"payment_id": payment.payment_id,
|
| 31 |
+
"message": "success",
|
| 32 |
+
"statusCode": 200,
|
| 33 |
+
}
|
| 34 |
+
except Exception as exc:
|
| 35 |
+
raise PaymentError(f"Error creating payment {str(exc)}")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def create_subscription_payment(event: dict) -> Payment:
|
| 39 |
+
return Payment(**event)
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/getting_started_with_idempotency_payload.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"user_id": "xyz",
|
| 3 |
+
"product_id": "123456789"
|
| 4 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/integrate_idempotency_with_batch_processor.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Any, Dict
|
| 3 |
+
|
| 4 |
+
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
|
| 5 |
+
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
|
| 6 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 7 |
+
DynamoDBPersistenceLayer,
|
| 8 |
+
IdempotencyConfig,
|
| 9 |
+
idempotent_function,
|
| 10 |
+
)
|
| 11 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 12 |
+
|
| 13 |
+
processor = BatchProcessor(event_type=EventType.SQS)
|
| 14 |
+
|
| 15 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 16 |
+
dynamodb = DynamoDBPersistenceLayer(table_name=table)
|
| 17 |
+
config = IdempotencyConfig(event_key_jmespath="messageId")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@idempotent_function(data_keyword_argument="record", config=config, persistence_store=dynamodb)
|
| 21 |
+
def record_handler(record: SQSRecord):
|
| 22 |
+
return {"message": record.body}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def lambda_handler(event: Dict[str, Any], context: LambdaContext):
|
| 26 |
+
config.register_lambda_context(context) # see Lambda timeouts section
|
| 27 |
+
|
| 28 |
+
return process_partial_response(
|
| 29 |
+
event=event,
|
| 30 |
+
context=context,
|
| 31 |
+
processor=processor,
|
| 32 |
+
record_handler=record_handler,
|
| 33 |
+
)
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/integrate_idempotency_with_batch_processor_payload.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"Records": [
|
| 3 |
+
{
|
| 4 |
+
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
|
| 5 |
+
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
|
| 6 |
+
"body": "Test message.",
|
| 7 |
+
"attributes": {
|
| 8 |
+
"ApproximateReceiveCount": "1",
|
| 9 |
+
"SentTimestamp": "1545082649183",
|
| 10 |
+
"SenderId": "replace-to-pass-gitleak",
|
| 11 |
+
"ApproximateFirstReceiveTimestamp": "1545082649185"
|
| 12 |
+
},
|
| 13 |
+
"messageAttributes": {
|
| 14 |
+
"testAttr": {
|
| 15 |
+
"stringValue": "100",
|
| 16 |
+
"binaryValue": "base64Str",
|
| 17 |
+
"dataType": "Number"
|
| 18 |
+
}
|
| 19 |
+
},
|
| 20 |
+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
|
| 21 |
+
"eventSource": "aws:sqs",
|
| 22 |
+
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
|
| 23 |
+
"awsRegion": "us-east-2"
|
| 24 |
+
}
|
| 25 |
+
]
|
| 26 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/integrate_idempotency_with_validator_payload.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"version": "2.0",
|
| 3 |
+
"routeKey": "$default",
|
| 4 |
+
"rawPath": "/my/path",
|
| 5 |
+
"rawQueryString": "parameter1=value1¶meter1=value2¶meter2=value",
|
| 6 |
+
"cookies": [
|
| 7 |
+
"cookie1",
|
| 8 |
+
"cookie2"
|
| 9 |
+
],
|
| 10 |
+
"headers": {
|
| 11 |
+
"Header1": "value1",
|
| 12 |
+
"Header2": "value1,value2"
|
| 13 |
+
},
|
| 14 |
+
"queryStringParameters": {
|
| 15 |
+
"parameter1": "value1,value2",
|
| 16 |
+
"parameter2": "value"
|
| 17 |
+
},
|
| 18 |
+
"requestContext": {
|
| 19 |
+
"accountId": "123456789012",
|
| 20 |
+
"apiId": "api-id",
|
| 21 |
+
"authentication": {
|
| 22 |
+
"clientCert": {
|
| 23 |
+
"clientCertPem": "CERT_CONTENT",
|
| 24 |
+
"subjectDN": "www.example.com",
|
| 25 |
+
"issuerDN": "Example issuer",
|
| 26 |
+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
|
| 27 |
+
"validity": {
|
| 28 |
+
"notBefore": "May 28 12:30:02 2019 GMT",
|
| 29 |
+
"notAfter": "Aug 5 09:36:04 2021 GMT"
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
},
|
| 33 |
+
"authorizer": {
|
| 34 |
+
"jwt": {
|
| 35 |
+
"claims": {
|
| 36 |
+
"claim1": "value1",
|
| 37 |
+
"claim2": "value2"
|
| 38 |
+
},
|
| 39 |
+
"scopes": [
|
| 40 |
+
"scope1",
|
| 41 |
+
"scope2"
|
| 42 |
+
]
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
|
| 46 |
+
"domainPrefix": "id",
|
| 47 |
+
"http": {
|
| 48 |
+
"method": "POST",
|
| 49 |
+
"path": "/my/path",
|
| 50 |
+
"protocol": "HTTP/1.1",
|
| 51 |
+
"sourceIp": "192.168.0.1/32",
|
| 52 |
+
"userAgent": "agent"
|
| 53 |
+
},
|
| 54 |
+
"requestId": "id",
|
| 55 |
+
"routeKey": "$default",
|
| 56 |
+
"stage": "$default",
|
| 57 |
+
"time": "12/Mar/2020:19:03:58 +0000",
|
| 58 |
+
"timeEpoch": 1583348638390
|
| 59 |
+
},
|
| 60 |
+
"body": "{\"message\": \"hello world\", \"username\": \"tom\"}",
|
| 61 |
+
"pathParameters": {
|
| 62 |
+
"parameter1": "value1"
|
| 63 |
+
},
|
| 64 |
+
"isBase64Encoded": false,
|
| 65 |
+
"stageVariables": {
|
| 66 |
+
"stageVariable1": "value1",
|
| 67 |
+
"stageVariable2": "value2"
|
| 68 |
+
}
|
| 69 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_composite_key.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 4 |
+
DynamoDBPersistenceLayer,
|
| 5 |
+
idempotent,
|
| 6 |
+
)
|
| 7 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 8 |
+
|
| 9 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 10 |
+
persistence_layer = DynamoDBPersistenceLayer(table_name=table, sort_key_attr="sort_key")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@idempotent(persistence_store=persistence_layer)
|
| 14 |
+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
|
| 15 |
+
user_id: str = event.get("body", "")["user_id"]
|
| 16 |
+
return {"message": "success", "user_id": user_id}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_composite_key_payload.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"body": "{\"user_id\":\"xyz\",\"product_id\":\"123456789\"}"
|
| 3 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_custom_config.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from botocore.config import Config
|
| 4 |
+
|
| 5 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 6 |
+
DynamoDBPersistenceLayer,
|
| 7 |
+
IdempotencyConfig,
|
| 8 |
+
idempotent,
|
| 9 |
+
)
|
| 10 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 11 |
+
|
| 12 |
+
# See: https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore-config
|
| 13 |
+
boto_config = Config()
|
| 14 |
+
|
| 15 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 16 |
+
persistence_layer = DynamoDBPersistenceLayer(table_name=table, boto_config=boto_config)
|
| 17 |
+
|
| 18 |
+
config = IdempotencyConfig(event_key_jmespath="body")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@idempotent(persistence_store=persistence_layer, config=config)
|
| 22 |
+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
|
| 23 |
+
return event
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_custom_config_payload.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"body": "{\"user_id\":\"xyz\",\"product_id\":\"123456789\"}"
|
| 3 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_custom_session.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import boto3
|
| 4 |
+
|
| 5 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 6 |
+
DynamoDBPersistenceLayer,
|
| 7 |
+
IdempotencyConfig,
|
| 8 |
+
idempotent,
|
| 9 |
+
)
|
| 10 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 11 |
+
|
| 12 |
+
# See: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html#module-boto3.session
|
| 13 |
+
boto3_session = boto3.session.Session()
|
| 14 |
+
|
| 15 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 16 |
+
persistence_layer = DynamoDBPersistenceLayer(table_name=table, boto3_session=boto3_session)
|
| 17 |
+
|
| 18 |
+
config = IdempotencyConfig(event_key_jmespath="body")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@idempotent(persistence_store=persistence_layer, config=config)
|
| 22 |
+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
|
| 23 |
+
return event
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_dataclass_explicitly_output_serializer.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
|
| 4 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 5 |
+
DynamoDBPersistenceLayer,
|
| 6 |
+
IdempotencyConfig,
|
| 7 |
+
idempotent_function,
|
| 8 |
+
)
|
| 9 |
+
from aws_lambda_powertools.utilities.idempotency.serialization.dataclass import DataclassSerializer
|
| 10 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 11 |
+
|
| 12 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 13 |
+
dynamodb = DynamoDBPersistenceLayer(table_name=table)
|
| 14 |
+
config = IdempotencyConfig(event_key_jmespath="order_id") # see Choosing a payload subset section
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@dataclass
|
| 18 |
+
class OrderItem:
|
| 19 |
+
sku: str
|
| 20 |
+
description: str
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@dataclass
|
| 24 |
+
class Order:
|
| 25 |
+
item: OrderItem
|
| 26 |
+
order_id: int
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@dataclass
|
| 30 |
+
class OrderOutput:
|
| 31 |
+
order_id: int
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@idempotent_function(
|
| 35 |
+
data_keyword_argument="order",
|
| 36 |
+
config=config,
|
| 37 |
+
persistence_store=dynamodb,
|
| 38 |
+
output_serializer=DataclassSerializer(model=OrderOutput),
|
| 39 |
+
)
|
| 40 |
+
def process_order(order: Order):
|
| 41 |
+
return OrderOutput(order_id=order.order_id)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def lambda_handler(event: dict, context: LambdaContext):
|
| 45 |
+
config.register_lambda_context(context) # see Lambda timeouts section
|
| 46 |
+
order_item = OrderItem(sku="fake", description="sample")
|
| 47 |
+
order = Order(item=order_item, order_id=1)
|
| 48 |
+
|
| 49 |
+
# `order` parameter must be called as a keyword argument to work
|
| 50 |
+
process_order(order=order)
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_exceptions.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 6 |
+
DynamoDBPersistenceLayer,
|
| 7 |
+
IdempotencyConfig,
|
| 8 |
+
idempotent_function,
|
| 9 |
+
)
|
| 10 |
+
from aws_lambda_powertools.utilities.idempotency.exceptions import IdempotencyPersistenceLayerError
|
| 11 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 12 |
+
|
| 13 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 14 |
+
persistence_layer = DynamoDBPersistenceLayer(table_name=table)
|
| 15 |
+
|
| 16 |
+
config = IdempotencyConfig()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@idempotent_function(data_keyword_argument="data", config=config, persistence_store=persistence_layer)
|
| 20 |
+
def call_external_service(data: dict):
|
| 21 |
+
# Any exception raised will lead to idempotency record to be deleted
|
| 22 |
+
result: requests.Response = requests.post(
|
| 23 |
+
"https://jsonplaceholder.typicode.com/comments/",
|
| 24 |
+
json=data,
|
| 25 |
+
)
|
| 26 |
+
return result.json()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def lambda_handler(event: dict, context: LambdaContext):
|
| 30 |
+
try:
|
| 31 |
+
call_external_service(data=event)
|
| 32 |
+
except IdempotencyPersistenceLayerError as e:
|
| 33 |
+
# No idempotency, but you can decide to error differently.
|
| 34 |
+
raise RuntimeError(f"Oops, can't talk to persistence layer. Permissions? error: {e}")
|
| 35 |
+
|
| 36 |
+
# This exception will not impact the idempotency of 'call_external_service'
|
| 37 |
+
# because it happens in isolation, or outside their scope.
|
| 38 |
+
raise SyntaxError("Oops, this shouldn't be here.")
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_idempotency_key_required.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 4 |
+
DynamoDBPersistenceLayer,
|
| 5 |
+
IdempotencyConfig,
|
| 6 |
+
idempotent,
|
| 7 |
+
)
|
| 8 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 9 |
+
|
| 10 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 11 |
+
persistence_layer = DynamoDBPersistenceLayer(table_name=table)
|
| 12 |
+
config = IdempotencyConfig(
|
| 13 |
+
event_key_jmespath='["user.uid", "order_id"]',
|
| 14 |
+
raise_on_no_idempotency_key=True,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@idempotent(config=config, persistence_store=persistence_layer)
|
| 19 |
+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
|
| 20 |
+
return event
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_idempotency_key_required_payload_error.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"user": {
|
| 3 |
+
"uid": "BB0D045C-8878-40C8-889E-38B3CB0A61B1",
|
| 4 |
+
"name": "Foo",
|
| 5 |
+
"order_id": 10000
|
| 6 |
+
}
|
| 7 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_idempotency_key_required_payload_success.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"user": {
|
| 3 |
+
"uid": "BB0D045C-8878-40C8-889E-38B3CB0A61B1",
|
| 4 |
+
"name": "Foo"
|
| 5 |
+
},
|
| 6 |
+
"order_id": 10000
|
| 7 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_idempotent_function_custom_output_serializer.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Dict, Type
|
| 3 |
+
|
| 4 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 5 |
+
DynamoDBPersistenceLayer,
|
| 6 |
+
IdempotencyConfig,
|
| 7 |
+
idempotent_function,
|
| 8 |
+
)
|
| 9 |
+
from aws_lambda_powertools.utilities.idempotency.serialization.custom_dict import CustomDictSerializer
|
| 10 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 11 |
+
|
| 12 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 13 |
+
dynamodb = DynamoDBPersistenceLayer(table_name=table)
|
| 14 |
+
config = IdempotencyConfig(event_key_jmespath="order_id") # see Choosing a payload subset section
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class OrderItem:
|
| 18 |
+
def __init__(self, sku: str, description: str):
|
| 19 |
+
self.sku = sku
|
| 20 |
+
self.description = description
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class Order:
|
| 24 |
+
def __init__(self, item: OrderItem, order_id: int):
|
| 25 |
+
self.item = item
|
| 26 |
+
self.order_id = order_id
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class OrderOutput:
|
| 30 |
+
def __init__(self, order_id: int):
|
| 31 |
+
self.order_id = order_id
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def order_to_dict(x: Type[OrderOutput]) -> Dict: # (1)!
|
| 35 |
+
return dict(x.__dict__)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def dict_to_order(x: Dict) -> OrderOutput: # (2)!
|
| 39 |
+
return OrderOutput(**x)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
order_output_serializer = CustomDictSerializer( # (3)!
|
| 43 |
+
to_dict=order_to_dict,
|
| 44 |
+
from_dict=dict_to_order,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@idempotent_function(
|
| 49 |
+
data_keyword_argument="order",
|
| 50 |
+
config=config,
|
| 51 |
+
persistence_store=dynamodb,
|
| 52 |
+
output_serializer=order_output_serializer,
|
| 53 |
+
)
|
| 54 |
+
def process_order(order: Order) -> OrderOutput:
|
| 55 |
+
return OrderOutput(order_id=order.order_id)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def lambda_handler(event: dict, context: LambdaContext):
|
| 59 |
+
config.register_lambda_context(context) # see Lambda timeouts section
|
| 60 |
+
order_item = OrderItem(sku="fake", description="sample")
|
| 61 |
+
order = Order(item=order_item, order_id=1)
|
| 62 |
+
|
| 63 |
+
# `order` parameter must be called as a keyword argument to work
|
| 64 |
+
process_order(order=order)
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_local_cache_payload.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"body": "{\"user_id\":\"xyz\",\"product_id\":\"123456789\"}"
|
| 3 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_payload_subset_payload.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"version": "2.0",
|
| 3 |
+
"routeKey": "ANY /createpayment",
|
| 4 |
+
"rawPath": "/createpayment",
|
| 5 |
+
"rawQueryString": "",
|
| 6 |
+
"headers": {
|
| 7 |
+
"Header1": "value1",
|
| 8 |
+
"Header2": "value2"
|
| 9 |
+
},
|
| 10 |
+
"requestContext": {
|
| 11 |
+
"accountId": "123456789012",
|
| 12 |
+
"apiId": "api-id",
|
| 13 |
+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
|
| 14 |
+
"domainPrefix": "id",
|
| 15 |
+
"http": {
|
| 16 |
+
"method": "POST",
|
| 17 |
+
"path": "/createpayment",
|
| 18 |
+
"protocol": "HTTP/1.1",
|
| 19 |
+
"sourceIp": "ip",
|
| 20 |
+
"userAgent": "agent"
|
| 21 |
+
},
|
| 22 |
+
"requestId": "id",
|
| 23 |
+
"routeKey": "ANY /createpayment",
|
| 24 |
+
"stage": "$default",
|
| 25 |
+
"time": "10/Feb/2021:13:40:43 +0000",
|
| 26 |
+
"timeEpoch": 1612964443723
|
| 27 |
+
},
|
| 28 |
+
"body": "{\"user_id\":\"xyz\",\"product_id\":\"123456789\"}",
|
| 29 |
+
"isBase64Encoded": false
|
| 30 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/idempotency/src/working_with_response_hook.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
from typing import Dict
|
| 4 |
+
|
| 5 |
+
from aws_lambda_powertools import Logger
|
| 6 |
+
from aws_lambda_powertools.utilities.idempotency import (
|
| 7 |
+
DynamoDBPersistenceLayer,
|
| 8 |
+
IdempotencyConfig,
|
| 9 |
+
idempotent_function,
|
| 10 |
+
)
|
| 11 |
+
from aws_lambda_powertools.utilities.idempotency.persistence.datarecord import (
|
| 12 |
+
DataRecord,
|
| 13 |
+
)
|
| 14 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 15 |
+
|
| 16 |
+
logger = Logger()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def my_response_hook(response: Dict, idempotent_data: DataRecord) -> Dict:
|
| 20 |
+
# Return inserted Header data into the Idempotent Response
|
| 21 |
+
response["x-idempotent-key"] = idempotent_data.idempotency_key
|
| 22 |
+
|
| 23 |
+
# expiry_timestamp can be None so include if set
|
| 24 |
+
expiry_timestamp = idempotent_data.get_expiration_datetime()
|
| 25 |
+
if expiry_timestamp:
|
| 26 |
+
response["x-idempotent-expiration"] = expiry_timestamp.isoformat()
|
| 27 |
+
|
| 28 |
+
# Must return the response here
|
| 29 |
+
return response
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
table = os.getenv("IDEMPOTENCY_TABLE", "")
|
| 33 |
+
dynamodb = DynamoDBPersistenceLayer(table_name=table)
|
| 34 |
+
config = IdempotencyConfig(response_hook=my_response_hook)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@idempotent_function(data_keyword_argument="order", config=config, persistence_store=dynamodb)
|
| 38 |
+
def process_order(order: dict) -> dict:
|
| 39 |
+
# create the order_id
|
| 40 |
+
order_id = str(uuid.uuid4())
|
| 41 |
+
|
| 42 |
+
# create your logic to save the order
|
| 43 |
+
# append the order_id created
|
| 44 |
+
order["order_id"] = order_id
|
| 45 |
+
|
| 46 |
+
# return the order
|
| 47 |
+
return {"order": order}
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def lambda_handler(event: dict, context: LambdaContext):
|
| 51 |
+
config.register_lambda_context(context) # see Lambda timeouts section
|
| 52 |
+
try:
|
| 53 |
+
logger.info(f"Processing order id {event.get('order_id')}")
|
| 54 |
+
return process_order(order=event.get("order"))
|
| 55 |
+
except Exception as err:
|
| 56 |
+
return {"status_code": 400, "error": f"Error processing {str(err)}"}
|
testbed/aws-powertools__powertools-lambda-python/examples/logger/sam/template.yaml
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
AWSTemplateFormatVersion: "2010-09-09"
|
| 2 |
+
Transform: AWS::Serverless-2016-10-31
|
| 3 |
+
Description: Powertools for AWS Lambda (Python) version
|
| 4 |
+
|
| 5 |
+
Globals:
|
| 6 |
+
Function:
|
| 7 |
+
Timeout: 5
|
| 8 |
+
Runtime: python3.12
|
| 9 |
+
Tracing: Active
|
| 10 |
+
Environment:
|
| 11 |
+
Variables:
|
| 12 |
+
POWERTOOLS_SERVICE_NAME: payment
|
| 13 |
+
POWERTOOLS_LOG_LEVEL: INFO
|
| 14 |
+
Layers:
|
| 15 |
+
# Find the latest Layer version in the official documentation
|
| 16 |
+
# https://docs.powertools.aws.dev/lambda/python/latest/#lambda-layer
|
| 17 |
+
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPythonV3-python312-x86_64:4
|
| 18 |
+
|
| 19 |
+
Resources:
|
| 20 |
+
LoggerLambdaHandlerExample:
|
| 21 |
+
Type: AWS::Serverless::Function
|
| 22 |
+
Properties:
|
| 23 |
+
CodeUri: ../src
|
| 24 |
+
Handler: inject_lambda_context.handler
|
testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_and_remove_keys.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from aws_lambda_powertools import Logger
|
| 2 |
+
|
| 3 |
+
logger = Logger(service="payment", name="%(name)s")
|
| 4 |
+
|
| 5 |
+
logger.info("Name should be equal service value")
|
| 6 |
+
|
| 7 |
+
additional_log_attributes = {"process": "%(process)d", "processName": "%(processName)s"}
|
| 8 |
+
logger.append_keys(**additional_log_attributes)
|
| 9 |
+
logger.info("This will include process ID and name")
|
| 10 |
+
logger.remove_keys(["processName"])
|
| 11 |
+
|
| 12 |
+
# further messages will not include processName
|
testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_and_remove_keys_output.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"level": "INFO",
|
| 4 |
+
"location": "<module>:16",
|
| 5 |
+
"message": "Name should be equal service value",
|
| 6 |
+
"name": "payment",
|
| 7 |
+
"service": "payment",
|
| 8 |
+
"timestamp": "2022-07-01 07:09:46,330+0000"
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"level": "INFO",
|
| 12 |
+
"location": "<module>:23",
|
| 13 |
+
"message": "This will include process ID and name",
|
| 14 |
+
"name": "payment",
|
| 15 |
+
"process": "9",
|
| 16 |
+
"processName": "MainProcess",
|
| 17 |
+
"service": "payment",
|
| 18 |
+
"timestamp": "2022-07-01 07:09:46,330+0000"
|
| 19 |
+
}
|
| 20 |
+
]
|
testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from aws_lambda_powertools import Logger
|
| 2 |
+
from aws_lambda_powertools.utilities.typing import LambdaContext
|
| 3 |
+
|
| 4 |
+
logger = Logger()
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def lambda_handler(event: dict, context: LambdaContext) -> str:
|
| 8 |
+
order_id = event.get("order_id")
|
| 9 |
+
|
| 10 |
+
# this will ensure order_id key always has the latest value before logging
|
| 11 |
+
# alternative, you can use `clear_state=True` parameter in @inject_lambda_context
|
| 12 |
+
logger.append_keys(order_id=order_id)
|
| 13 |
+
logger.info("Collecting payment")
|
| 14 |
+
|
| 15 |
+
return "hello world"
|
testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys_extra_output.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"level": "INFO",
|
| 3 |
+
"location": "collect.handler:9",
|
| 4 |
+
"message": "Collecting payment",
|
| 5 |
+
"timestamp": "2021-05-03 11:47:12,494+0000",
|
| 6 |
+
"service": "payment",
|
| 7 |
+
"request_id": "1123"
|
| 8 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys_kwargs_output.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"level": "INFO",
|
| 3 |
+
"location": "collect.handler:8",
|
| 4 |
+
"message": "Collecting payment",
|
| 5 |
+
"timestamp": "2022-11-26 11:47:12,494+0000",
|
| 6 |
+
"service": "payment",
|
| 7 |
+
"request_id": "1123"
|
| 8 |
+
}
|
testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys_vs_extra.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
from aws_lambda_powertools import Logger
|
| 6 |
+
|
| 7 |
+
ENDPOINT = os.getenv("PAYMENT_API", "")
|
| 8 |
+
logger = Logger(service="payment")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class PaymentError(Exception):
|
| 12 |
+
...
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def lambda_handler(event, context):
|
| 16 |
+
logger.append_keys(payment_id="123456789")
|
| 17 |
+
charge_id = event.get("charge_id", "")
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
ret = requests.post(url=f"{ENDPOINT}/collect", data={"charge_id": charge_id})
|
| 21 |
+
ret.raise_for_status()
|
| 22 |
+
|
| 23 |
+
logger.info("Charge collected successfully", extra={"charge_id": charge_id})
|
| 24 |
+
return ret.json()
|
| 25 |
+
except requests.HTTPError as e:
|
| 26 |
+
raise PaymentError(f"Unable to collect payment for charge {charge_id}") from e
|
| 27 |
+
|
| 28 |
+
logger.info("goodbye")
|
testbed/aws-powertools__powertools-lambda-python/examples/logger/src/append_keys_vs_extra_output.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"level": "INFO",
|
| 4 |
+
"location": "<module>:22",
|
| 5 |
+
"message": "Charge collected successfully",
|
| 6 |
+
"timestamp": "2021-01-12 14:09:10,859",
|
| 7 |
+
"service": "payment",
|
| 8 |
+
"sampling_rate": 0.0,
|
| 9 |
+
"payment_id": "123456789",
|
| 10 |
+
"charge_id": "75edbad0-0857-4fc9-b547-6180e2f7959b"
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"level": "INFO",
|
| 14 |
+
"location": "<module>:27",
|
| 15 |
+
"message": "goodbye",
|
| 16 |
+
"timestamp": "2021-01-12 14:09:10,860",
|
| 17 |
+
"service": "payment",
|
| 18 |
+
"sampling_rate": 0.0,
|
| 19 |
+
"payment_id": "123456789"
|
| 20 |
+
}
|
| 21 |
+
]
|