Powertools for AWS Lambda (TypeScript)

repository·main·Indexed 23 days ago

https://github.com/aws-powertools/powertools-lambda-typescript

A developer toolkit and suite of utilities for AWS Lambda functions in TypeScript and JavaScript environments. It helps implement serverless best practices including structured logging, tracing, custom metrics, and partial failure handling for SQS, Kinesis, and DynamoDB streams via the Batch Processing utility.

Tokens
83.1K
Snippets
136
Records
453
Agent score
81%

What's inside Powertools for AWS Lambda (TypeScript)

  1. Overview of the HTTP API Event Handler

    main

    The HTTP API event handler is a utility designed to build HTTP APIs within AWS Lambda. It provides lightweight routing to reduce boilerplate code for several AWS trigger sources, including:

    • Amazon API Gateway (REST and HTTP APIs)
    • Application Load Balancer (ALB)
    • Lambda Function URLs

    It features a built-in middleware engine for request/response transformation and validation, and is flexible enough to support both micro-functions (handling one or a few routes) and monolithic functions.

  2. Overview of Tracer

    main

    Tracer is an opinionated, thin wrapper for the AWS X-Ray SDK for Node.js. It simplifies distributed tracing in AWS Lambda functions by automating the collection of telemetry data.

    Key capabilities include:

    • Automatic Metadata & Annotations: Captures cold starts and service names as annotations, and captures responses or full exceptions as metadata.
    • HTTP(S) Tracing: Automatically traces HTTP(S) clients (including fetch) and generates segments for each request.
    • Flexible Instrumentation: Supports tracing functions using decorators, middleware, or manual instrumentation.
    • AWS SDK Support: Supports tracing both AWS SDK v2 and v3 via the underlying AWS X-Ray SDK.
    • Environment Awareness: Automatically disables tracing when the code is not running within a Lambda environment.
  3. Overview of Powertools for AWS Lambda (TypeScript) features

    main

    Powertools for AWS Lambda (TypeScript) is a modular developer toolkit designed to implement Serverless best practices in AWS Lambda environments. It is built to be used progressively, allowing you to adopt one or more utilities as needed. The toolkit is available for both TypeScript and JavaScript codebases.

    Available utilities include:

    • Tracer: Decorators and utilities for tracing Lambda handlers and functions (sync/async).
    • Logger: Structured logging and middleware for enriching logs with Lambda context.
    • Metrics: Asynchronous custom metrics via CloudWatch Embedded Metric Format (EMF).
    • Event Handler: Handles AWS AppSync real-time events, AppSync GraphQL APIs, Bedrock Agents, etc.
    • Parameters: High-level functions to retrieve parameters from AWS SSM Parameter Store, Secrets Manager, AppConfig, and DynamoDB.
    • Idempotency: Decorators, Middy middleware, and function wrappers to prevent duplicate execution based on payload content.
    • Batch Processing: Handles partial failures in batches from Amazon SQS, Kinesis Data Streams, and DynamoDB Streams.
    • JMESPath Functions: Built-in functions to deserialize common encoded JSON payloads.
    • Parser: Parses and validates AWS Lambda event payloads using Zod.
    • Validation: JSON Schema validation for events and responses, including JMESPath support.
    • Kafka: Handles message deserialization and parsing of Kafka events.
    • Signer: Signs HTTP requests using AWS Signature Version 4 (SigV4), including an optional signed fetch.
  4. Overview of the Logger utility

    main

    The Logger utility provides an opinionated, structured JSON logger designed specifically for AWS Lambda. It automates the capture of Lambda context and cold start information, ensuring logs are consistent and machine-readable for tools like Amazon CloudWatch.

    Key capabilities include:

    • Structured Logging: Outputs all logs as JSON.
    • Context Awareness: Automatically captures Lambda context and cold start indicators.
    • Event Logging: Ability to log the incoming Lambda invocation event (disabled by default).
    • Log Level Sampling: Ability to switch to DEBUG level for a specific percentage of invocations.
    • Log Buffering: Supports buffering logs for a request and flushing them automatically on error or manually.
    • Extensibility: Allows appending additional keys to logs at any time and supports custom log formatters (Bring Your Own Formatter) to match organizational Logging RFCs.
  5. Overview of Idempotency utility

    main

    The Idempotency utility converts Lambda functions into idempotent operations, making them safe to retry. It prevents a Lambda handler from executing more than once for the same event payload within a defined time window and ensures the handler returns the same result for identical payloads.

    Key capabilities include:

    • Payload subset selection: Use JMESPath expressions to select a specific part of the event as the idempotency key.
    • Configurable time windows: Define how long a payload should be considered a duplicate.
    • Timeout protection: Automatically expires in-progress executions if the Lambda function times out.
    • Multiple persistence layers: Supports Amazon DynamoDB, Valkey, Redis OSS, or any Redis-compatible cache.
  6. Explore Powertools community resources and tutorials

    main

    The community has produced several high-quality resources for learning Powertools for AWS Lambda (TypeScript):