Skip to content

Instantly share code, notes, and snippets.

@michaelbrewer
michaelbrewer / app.py
Last active April 12, 2021 07:50
00 - Basic Payment Lambda
import json
import time
def create_payment(payment):
order_key = payment["order_key"]
print(f"Order key:", order_key)
# Simulate failure
if "FAILURE" in order_key:
@michaelbrewer
michaelbrewer / app.py
Last active March 7, 2021 16:38
Demo of idempotent (More complete examples)
import json
import os
import time
from typing import Callable
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
from aws_lambda_powertools.utilities.idempotency import (
@michaelbrewer
michaelbrewer / app-logging.py
Last active April 12, 2021 22:36
01 - Logging
import json
import time
from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging import correlation_paths
logger = Logger()
def create_payment(payment):
@michaelbrewer
michaelbrewer / app-tracer.py
Last active April 12, 2021 22:36
02 - Tracing
import json
import time
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.logging import correlation_paths
logger = Logger()
tracer = Tracer()
@michaelbrewer
michaelbrewer / app-metrics.py
Last active April 12, 2021 22:36
03 - Metrics
import json
import time
from aws_lambda_powertools import Logger, Metrics, Tracer
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.metrics import MetricUnit
logger = Logger()
tracer = Tracer()
metrics = Metrics()
@michaelbrewer
michaelbrewer / app-data-classes.py
Last active April 12, 2021 22:37
04 - DataClasses
import json
import os
import time
from aws_lambda_powertools import Logger, Metrics, Tracer
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.metrics import MetricUnit
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
logger = Logger()
import json
import os
import time
from typing import Any, Dict
from aws_lambda_powertools import Logger, Metrics, Tracer
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.metrics import MetricUnit
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
from aws_lambda_powertools.utilities.typing import LambdaContext
@michaelbrewer
michaelbrewer / app-idempotent.py
Last active April 12, 2021 22:38
06 - Idempotent
import json
import os
import time
from typing import Any, Dict
from aws_lambda_powertools import Logger, Metrics, Tracer
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.metrics import MetricUnit
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, IdempotencyConfig, idempotent
@michaelbrewer
michaelbrewer / app-exception-handling.py
Last active April 12, 2021 22:38
07 - Exception handling
import json
import os
import time
from typing import Any, Callable, Dict
from aws_lambda_powertools import Logger, Metrics, Tracer
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.metrics import MetricUnit
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
@michaelbrewer
michaelbrewer / setup.sh
Last active April 12, 2021 05:12
Setting up aws-lambda-powertools
# Ensure we have the latest pip and pipenv
pip3 install --upgrade pip pipenv
# Setup new Python 3.8 project
pipenv --python 3.8
# Install aws-lambda-powertools
pipenv install boto3 aws-lambda-powertools
# Install dev dependencies
pipenv install black pytest isort mypy --dev --pre