Skip to content

RFC: Collection of tiny utilities #30

Closed
@heitorlessa

Description

@heitorlessa

Background

At GA, Powertools will offer Tracer, Metrics, Logger, and Middleware factory as the core utilities.

Optionally, I'm pondering on the idea of providing a set of tiny handy utilities that can be used either as standalone functions, or as part of a custom middleware - For example, JSON serialization, detect retries, fetch secrets, etc.

The benefit of providing utilities as functions is two-fold a) ease of maintenance, b) pick and choose and create a single custom middleware instead of nesting a myriad of decorators.

Propose solution

Use case: Custom middleware using a handful of utilities

from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.utilities import validate_event, cors, decode_json, detect_retry

@lambda_handler_decorator
def custom_middleware_name(handler, event, context):
    # Before
    detect_retry(event)
    event = decode_json(event)
    validate_event(event)

    response = handler(event, context)

    # After
    response = cors(response)
    return response

@custom_middleware_name
def lambda_handler(event, context):
    ...

Use case: Using utilities standalone

from aws_lambda_powertools.utilities import validate_event, cors, decode_json, detect_retry

@custom_middleware_name
def lambda_handler(event, context):
    detect_retry(event)
    event = decode_json(event)
    validate_event(event)

    return cors(result)

Request for comments

As part of this RFC, I'd like to know what utilities are the most useful to have upfront - Leave a comment, and vote using 👍 on each comment instead of a new comment.

For ideas, here are some utilities as decorators created by other awesome authors: Lambda Decorators by Grid smarter cities, and Lambda Decorators by Daniel Schep.

Tenets

  • AWS Lambda only – We optimise for AWS Lambda function environments 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFChelp wantedCould use a second pair of eyes/hands

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions