Skip to content

Latest commit

 

History

History
96 lines (71 loc) · 6.34 KB

File metadata and controls

96 lines (71 loc) · 6.34 KB
title description
Homepage
AWS Lambda Powertools Python

import Note from "../src/components/Note"

Powertools is a suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier.

Looking for a quick run through of the core utilities?

Check out this detailed blog post with a practical example.

Install

Powertools is available in PyPi. You can use your favourite dependency management tool to install it

  • poetry: poetry add aws-lambda-powertools
  • pip: pip install aws-lambda-powertools

Quick hello world example using SAM CLI

sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python
  • Tracing - Decorators and utilities to trace Lambda function handlers, and both synchronous and asynchronous functions
  • Logging - Structured logging made easier, and decorator to enrich structured logging with key Lambda context details
  • Metrics - Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
  • Bring your own middleware - Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
  • Parameters utility - Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time
  • Batch utility - Batch processing for AWS SQS, handles partial failure.

Lambda Layer

Powertools is also available as a Lambda Layer. It is distributed via the AWS Serverless Application Repository (SAR).

App ARN
aws-lambda-powertools-python-layer arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer

If using SAM, you can include this SAR App as part of your shared Layers stack, and lock to a specific semantic version. Once deployed, it'll be available across the account this is deployed to.

  AwsLambdaPowertoolsPythonLayer:
    Type: AWS::Serverless::Application
    Properties:
      Location:
        ApplicationId: arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer
        SemanticVersion: 1.3.1 # change to latest semantic version available in SAR

Features

Utility Description
Tracing Decorators and utilities to trace Lambda function handlers, and both synchronous and asynchronous functions
Logging Structured logging made easier, and decorator to enrich structured logging with key Lambda context details
Metrics Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
Bring your own middleware Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
Parameters utility Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time

Environment variables

Environment variables used across suite of utilities.

Environment variable | Description | Utility ------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | ------------------------------------------------- POWERTOOLS_SERVICE_NAME | Sets service name used for tracing namespace, metrics dimension and structured logging | All POWERTOOLS_METRICS_NAMESPACE | Sets namespace used for metrics | Metrics POWERTOOLS_TRACE_DISABLED | Disables tracing | Tracing POWERTOOLS_TRACE_MIDDLEWARES | Creates sub-segment for each custom middleware | Middleware factory POWERTOOLS_LOGGER_LOG_EVENT | Logs incoming event | Logging POWERTOOLS_LOGGER_SAMPLE_RATE | Debug log sampling | Logging LOG_LEVEL | Sets logging level | Logging

Debug mode

As a best practice, AWS Lambda Powertools logging statements are suppressed. If necessary, you can enable debugging using set_package_logger:

from aws_lambda_powertools.logging.logger import set_package_logger

set_package_logger()

Tenets

  • AWS Lambda only – We optimise for AWS Lambda function environments and supported runtimes only. Utilities might work with web frameworks and non-Lambda environments, though they are not officially supported.
  • Eases the adoption of best practices – The main priority of the utilities is to facilitate best practices adoption, as defined in the AWS Well-Architected Serverless Lens; all other functionality is optional.
  • Keep it lean – Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
  • We strive for backwards compatibility – New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
  • We work backwards from the community – We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
  • Idiomatic – Utilities follow programming language idioms and language-specific best practices.

* Core utilities are Tracer, Logger and Metrics. Optional utilities may vary across languages.