Skip to main content

Lambda API Decorators

Define AWS Lambda API routes and configuration directly in your Python code using decorators.

Lambda API Decorators provides Python decorators for defining API routes and AWS Lambda configuration alongside your Lambda handlers.

Instead of maintaining route and function configuration separately from your application code, you can declare HTTP methods, paths, runtimes, timeouts, memory, environment variables, layers, networking, and other Lambda settings directly on the handler.

Combined with Lambda API Decorators CDK, these definitions are used to automatically generate the corresponding AWS Lambda and Amazon API Gateway infrastructure with AWS CDK.

from lambda_api_decorators import GET, runtime, name, memory_size
import json


@GET("/dogs")
@runtime("python3.11")
@name("LBD-DOGS-GET")
@memory_size(256)
def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "Hello World from /dogs!"
        })
    }

Installation

Install Lambda API Decorators from PyPI:

pip install lambda-api-decorators

To generate AWS infrastructure from the decorated Lambda handlers, install the CDK integration:

pip install lambda-api-decorators-cdk

Available Decorators

API Routes

Every Lambda API handler must define an HTTP method and endpoint path.

Available route decorators:

  • GET
  • POST
  • PUT
  • DELETE
  • ANY

For example:

@GET("/dogs")
def lambda_handler(event, context):
    ...

The route definition is used by the CDK integration to configure the corresponding Amazon API Gateway endpoint and Lambda integration.

Lambda Configuration

Lambda API Decorators also allows Lambda configuration to be declared directly on the handler.

timeout

Defines the Lambda function timeout in seconds.

@timeout(30)

memory_size

Defines the amount of memory allocated to the Lambda function.

@memory_size(512)

name

Defines a custom name for the Lambda function.

@name("LBD-DOGS-GET")

description

Defines the Lambda function description.

@description("Returns the list of dogs")

runtime

References a runtime configuration defined by Lambda API Decorators CDK.

@runtime("python3.11")

role

References an IAM role defined by Lambda API Decorators CDK.

@role("api-role")

vpc

References a VPC configuration defined by Lambda API Decorators CDK.

@vpc("application-vpc")

environment

Associates one or more environment configurations with the Lambda function.

@environment("database", "application")

layer

Associates one or more Lambda Layers with the function.

@layer("common-dependencies")

security_group

Associates one or more security group configurations with the Lambda function.

@security_group("lambda-security-group")

The referenced runtimes, IAM roles, VPCs, environment configurations, layers, and security groups are defined in the AWS CDK application using Lambda API Decorators CDK.

How It Works

Lambda API Decorators keeps API and Lambda configuration close to the application code:

Python Lambda handlers
        │
        │  @GET, @POST, @runtime,
        │  @timeout, @memory_size, ...
        ▼
Lambda API Decorators
        │
        ▼
Lambda API Decorators CDK
        │
        ▼
AWS CDK
        │
        ├── AWS Lambda
        ├── Amazon API Gateway
        ├── IAM
        ├── VPC configuration
        └── other AWS resources

This allows your Lambda handlers to become the source of the API definition while AWS CDK remains responsible for generating and deploying the infrastructure.

  • Lambda API Decorators CDK — AWS CDK integration that generates Lambda and API Gateway infrastructure from decorated Python handlers.
  • Lambda API Decorators Examples — Example applications demonstrating how to use Lambda API Decorators.

Releasing

The Git tag is the source of truth for this package's version. Maintainers create and push a semantic-version tag from main:

git checkout main
git pull

git tag v0.2.0
git push origin v0.2.0

Pushing the tag starts the release workflow, and v0.2.0 becomes Python package version 0.2.0. Versions in this repository are independent from lambda-api-decorators-cdk.

Publishing uses PyPI trusted publishing. The PyPI project must have a trusted publisher configured for this repository, the release.yml workflow, and the pypi GitHub environment.

License

See the repository license for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambda_api_decorators-0.3.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lambda_api_decorators-0.3.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file lambda_api_decorators-0.3.0.tar.gz.

File metadata

  • Download URL: lambda_api_decorators-0.3.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for lambda_api_decorators-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5f7b4d0228543e5f5f8bab3b3e30200918da3de77e340593fc424790557e0ce3
MD5 390e5670a392abdaffe2b5c15da2ea16
BLAKE2b-256 72bd7d164a15dc1fdb15ab2017c3c30565800092a64cbfe68b3a89dc4d82bac8

See more details on using hashes here.

Provenance

The following attestation bundles were made for lambda_api_decorators-0.3.0.tar.gz:

Publisher: release.yml on lambda-api-decorators/lambda-api-decorators

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lambda_api_decorators-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lambda_api_decorators-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b877c971d4cecfabd7edbabf5371b8d709745fa02d6bf8681cc0942fcf000011
MD5 41206cc89446605b29a6b1c59d6eec6b
BLAKE2b-256 a2441c3a0bc494a40396fd485e0052e3283e763e9918a933822461911c099103

See more details on using hashes here.

Provenance

The following attestation bundles were made for lambda_api_decorators-0.3.0-py3-none-any.whl:

Publisher: release.yml on lambda-api-decorators/lambda-api-decorators

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page