ran-isenberg/aws-lambda-handler-cookbook
Tentang
Repositori ini menyediakan cetak biru layanan serverless berbasis open source yang berfungsi dan dapat di-deploy, dengan fungsi AWS Lambda serta kode Python AWS CDK yang menerapkan semua praktik terbaik dan pipeline CI/CD lengkap.
Pembelajaran dan pendidikanYang dapat Anda lakukan
- Python Serverless service with a recommended file structure.
- CDK infrastructure with infrastructure tests and security tests.
- CI/CD pipelines based on Github actions that deploys to AWS with python linters, complexity checks and style formatters.
- CI/CD pipeline order: quality checks → unit, infrastructure and integration tests (local AWS Emulator, no AWS credentials) → deploy → E2E tests, so a failing check or test stops the pipeline before anything is deployed. On pull requests, the quality checks and the tests run in one job, so the setup runs once.
Pertumbuhan bintang terukur
- dalam 24 jam
- 0
- dalam 7 hari
- +2
- dalam 15 hari
- +2
- dalam 1 bulan
- Mengumpulkan riwayat
Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.
README.md
AsliAWS Lambda Handler Cookbook (Python)

This project provides a working, open source based, AWS Lambda handler skeleton Python code includingx DEPLOYMENT code with CDK and a pipeline.
This project can serve as a blueprint for new Serverless services - CDK deployment code, pipeline and handler are covered.
📜Documentation | Blogs website
Contact details | mailto:[email protected]
AWS Recommendation
This repository was recommended in an AWS blog post Best practices for accelerating development with serverless blueprints

Concepts
I spoke at AWS re:invent 2023 with Heitor Lessa, former Chief Architect of Powertools for AWS Lambda about the concepts I implemented in this project.
Getting Started
You can start with a clean service out of this blueprint repository without using the 'Template' button on GitHub.
You can use Cookiecutter.
- Cookiecutter - install with pip/brew
brew install cookiecutterorpip install cookiecutter
Then run:
cookiecutter gh:ran-isenberg/cookiecutter-serverless-python
Answer the questions to select repo name, service name, etc.:

That's it, your developer environment has been set! you are ready to deploy the service:
cd {new repo folder}
uv sync
make deploy
Make sure you have uv installed.
You can also run 'make pr' to run all checks, file formatters, unit and infrastructure tests, integration tests against a local AWS Emulator, then deploy to AWS and run the E2E tests.
The integration tests need no AWS account: start the emulator with make emulator-start (run make emulator-install once: on macOS/Windows it builds a Docker image, on Linux and CI it installs the Debian package), then run make integration.
The Problem
Starting a Serverless service can be overwhelming. You need to figure out many questions and challenges that have nothing to do with your business domain:
- How to deploy to the cloud? What IAC framework do you choose?
- How to write a SaaS-oriented CI/CD pipeline? What does it need to contain?
- How do you handle observability, logging, tracing, metrics?
- How do you write a Lambda function?
- How do you handle testing?
- What makes an AWS Lambda handler resilient, traceable, and easy to maintain? How do you write such a code?
The Solution
This project aims to reduce cognitive load and answer these questions for you by providing a skeleton Python Serverless service blueprint that implements best practices for AWS Lambda, Serverless CI/CD, and AWS CDK in one blueprint project.
Serverless Service - The Order service
This project provides a working orders service where customers can create, get, list, and delete orders of items.
The project deploys an API GW with AWS Lambda integrations and stores data in a DynamoDB table:
POST /api/orders/- Create a new orderGET /api/orders/{order_id}- Get an order by IDDELETE /api/orders/{order_id}- Delete an order by IDGET /api/orders/?limit={n}&next_token={cursor}- List orders with pagination, served by an AWS Lambda Managed Instances pool
All
/api/ordersroutes require an Amazon Cognito access token (Authorization: Bearer <token>), see Authentication./swaggeris public.
flowchart LR
subgraph AWS["AWS Cloud"]
subgraph APIGW["API Gateway"]
REST["REST API<br/>POST /api/orders<br/>GET /api/orders/{id}<br/>GET /api/orders (list)<br/>DELETE /api/orders/{id}"]
end
subgraph Security["Security (Production)"]
WAF["WAF WebACL<br/>AWS Managed Rules"]
end
subgraph Compute["Compute"]
CREATE["Create Order<br/>Lambda Function"]
GET["Get Order<br/>Lambda Function"]
DELETE["Delete Order<br/>Lambda Function"]
LAYER["Lambda Layer<br/>Common Dependencies"]
end
subgraph LMI["Lambda Managed Instances (VPC)"]
LIST_ALIAS["ListOrders Alias: live"]
LIST["List Orders<br/>Lambda Function"]
CP["Capacity Provider<br/>EC2 pool, 2 AZs"]
end
subgraph Auth["Authentication"]
COGNITO["Cognito User Pool<br/>group: managers"]
PRETOKEN["Pre Token Generation<br/>Lambda (adds aud + scopes)"]
end
subgraph Config["Configuration"]
APPCONFIG["AppConfig<br/>Feature Flags"]
end
subgraph Storage["Storage"]
DDB[("DynamoDB<br/>Orders Table")]
IDEMPOTENCY[("DynamoDB<br/>Idempotency Table")]
end
end
CLIENT((Client)) -.sign in, get token.-> COGNITO
COGNITO -.-> PRETOKEN
CLIENT --> WAF
WAF --> REST
REST --> CREATE
REST --> GET
REST --> DELETE
REST --> LIST_ALIAS
LIST_ALIAS --> LIST
LIST -.runs on.-> CP
CREATE --> LAYER
GET --> LAYER
DELETE --> LAYER
CREATE --> APPCONFIG
CREATE --> DDB
CREATE --> IDEMPOTENCY
GET --> DDB
DELETE --> DDB
LIST --> DDB
CREATE -.verifies JWT.-> COGNITO
GET -.verifies JWT.-> COGNITO
DELETE -.verifies JWT.-> COGNITO
LIST -.verifies JWT.-> COGNITO
classDef client fill:#232F3E,stroke:#232F3E,color:#fff
classDef network fill:#8C4FFF,stroke:#5A30B5,color:#fff
classDef security fill:#DD344C,stroke:#A3233A,color:#fff
classDef compute fill:#ED7100,stroke:#B35500,color:#1b1b1b
classDef database fill:#C925D1,stroke:#8F1A95,color:#fff
classDef management fill:#E7157B,stroke:#A50F58,color:#fff
class CLIENT client
class WAF,COGNITO security
class REST network
class CREATE,GET,DELETE,LAYER,LIST,LIST_ALIAS,CP,PRETOKEN compute
class DDB,IDEMPOTENCY database
class APPCONFIG management
Authentication
The API is protected with Amazon Cognito and the Powertools for AWS Lambda JWT verifier (aws_lambda_powertools.utilities.auth_alpha.JWTVerifier, alpha). Every order Lambda verifies the access token itself (signature, issuer, expiration, app client and audience, and the scope on delete), there is no API Gateway authorizer:
| Route | Who can call it |
|---|---|
POST /api/orders, GET /api/orders, GET /api/orders/{order_id} |
Any signed in user (valid access token) |
DELETE /api/orders/{order_id} |
Members of the orders-managers group (orders/manage scope) |
- A missing or invalid token (an ID token too) returns
401, a delete without theorders/managescope returns403. - Orders belong to the user that created them (the access token
subclaim). Users only get, delete and list their own orders, an order of another user returns404, for managers too. The orders table is keyed byowner_idandid, so listing is aQueryof the caller's orders. - A Cognito pre token generation trigger (event version 2) adds the
audclaim, and theorders/managescope for members oforders-managers, to access tokens issued by the Cognito API, which theJWTVerifier.cognito()profile requires. Read about why in the Authentication documentation. - The LIST function runs in the Lambda Managed Instances VPC without internet access, and the Cognito VPC endpoint does not serve the user pool public keys (JWKS). A deploy time custom resource fetches the JWKS and passes it to the LIST function in the
COGNITO_JWKSenvironment variable, which the verifier uses instead of downloading it. - The e2e tests create real users, sign them in to get real access tokens and delete the users when the test session ends. The identity that runs them needs these permissions on the user pool:
cognito-idp:AdminCreateUser,AdminSetUserPassword,AdminAddUserToGroup,AdminInitiateAuthandAdminDeleteUser. - The integration tests run against a local AWS Emulator, whose Cognito tokens can't be verified yet. They sign Cognito-shaped access tokens with a per session test key, published to the handlers in
COGNITO_JWKS, and shape them with the real pre token generation trigger.
LIST orders via AWS Lambda Managed Instances
The GET /api/orders/ endpoint is deliberately powered by
AWS Lambda Managed Instances
(LMI) rather than standard Lambda. LMI maintains a warm pool of execution
environments on an EC2-backed capacity provider, so paginated list traffic
avoids cold starts, runs inside a private VPC with DynamoDB and observability
VPC endpoints, and can cap concurrency per environment (useful for bounding
DynamoDB Query pressure). The CRUD endpoints stay on standard Lambda — LMI is
picked per-function, only where its extra surface area pays off.
See the Lambda Managed Instances deep-dive for tuning knobs (memory-to-vCPU ratio, min/max environments, per-env concurrency), the matching CDK construct, and the gotchas around version publishing and memory/CPU constraints.
Monitoring Design
flowchart TB
subgraph Monitoring["CloudWatch Monitoring"]
subgraph Dashboards["Dashboards"]
HL["High-Level Dashboard<br/>API Gateway Metrics<br/>Business KPIs"]
LL["Low-Level Dashboard<br/>Lambda Metrics<br/>DynamoDB Metrics"]
end
subgraph Alarms["CloudWatch Alarms"]
API_ALARM["API Gateway Alarms<br/>5XX Errors, Latency"]
LAMBDA_ALARM["Lambda Alarms<br/>Errors, P90 Latency"]
DDB_ALARM["DynamoDB Alarms<br/>Throttles, Errors"]
end
end
subgraph Notification["Notification"]
SNS["SNS Topic<br/>KMS Encrypted"]
end
subgraph Resources["Monitored Resources"]
APIGW["API Gateway"]
LAMBDA["Lambda Function"]
DDB["DynamoDB Tables"]
end
APIGW --> API_ALARM
LAMBDA --> LAMBDA_ALARM
DDB --> DDB_ALARM
API_ALARM --> SNS
LAMBDA_ALARM --> SNS
DDB_ALARM --> SNS
API_ALARM --> HL
LAMBDA_ALARM --> LL
DDB_ALARM --> LL
classDef network fill:#8C4FFF,stroke:#5A30B5,color:#fff
classDef compute fill:#ED7100,stroke:#B35500,color:#1b1b1b
classDef database fill:#C925D1,stroke:#8F1A95,color:#fff
classDef management fill:#E7157B,stroke:#A50F58,color:#fff
classDef integration fill:#E7157B,stroke:#A50F58,color:#fff
class APIGW network
class LAMBDA compute
class DDB database
class HL,LL,API_ALARM,LAMBDA_ALARM,DDB_ALARM management
class SNS integration
Features
- Python Serverless service with a recommended file structure.
- CDK infrastructure with infrastructure tests and security tests.
- CI/CD pipelines based on Github actions that deploys to AWS with python linters, complexity checks and style formatters.
- CI/CD pipeline order: quality checks → unit, infrastructure and integration tests (local AWS Emulator, no AWS credentials) → deploy → E2E tests, so a failing check or test stops the pipeline before anything is deployed. On pull requests, the quality checks and the tests run in one job, so the setup runs once.
- CI/CD pipeline deploys to dev/staging and production environments with different gates between each environment
- Automatic GitHub releases with semantic versioning based on conventional commits
- Automatic PR labeling based on commit message prefixes (feat, fix, docs, chore)
- Makefile for simple developer experience.
- The AWS Lambda handler embodies Serverless best practices and has all the bells and whistles for a proper production ready handler.
- AWS Lambda handler uses AWS Lambda Powertools.
- AWS Lambda handler 3 layer architecture: handler layer, logic layer and data access layer
- Features flags and configuration based on AWS AppConfig
- Idempotent API
- REST API protected by WAF with four AWS managed rules in production deployment
- Amazon Cognito authentication, verified in the Lambdas by the Powertools JWT verifier: any signed in user can create, get and list orders, deleting requires the
orders/managescope - Per-user order ownership: users only get, delete and list their own orders (the access token
subclaim is the owner) - Integration tests run against a local AWS Emulator with telemetry off, no AWS account needed
- End to end tests use real Cognito users and real access tokens, created and deleted by the tests
- CloudWatch dashboards - High level and low level including CloudWatch alarms
- Unit, infrastructure, security, integration and end to end tests.
- Automatically generated OpenAPI endpoint: /swagger with Pydantic schemas for both requests and responses
- CI swagger protection - fails the PR if your swagger JSON file (stored at docs/swagger/openapi.json) is out of date
- Automated protection against API breaking changes
- AWS Lambda Managed Instances pool powers the paginated LIST endpoint (VPC, capacity provider, warm environments)
CDK Deployment
The CDK code creates an API GW with paths /api/orders (POST) and /api/orders/{order_id} (GET, DELETE), each backed by a dedicated Lambda function, and a Cognito user pool (with its app client, groups and pre token generation trigger) that protects them.
The AWS Lambda handler uses a Lambda layer optimization which takes all the packages under the [packages] section in the Pipfile and downloads them in via a Docker instance.
This allows you to package any custom dependencies you might have, just add them to the Pipfile under the [packages] section.
Serverless Best Practices
The AWS Lambda handler will implement multiple best practice utilities.
Each utility is implemented when a new blog post is published about that utility.
The utilities cover multiple aspect of a production-ready service, including:
- API Idempotency
- Authentication & Authorization
- CDK Best practices
- Dynamic Configuration & feature flags
- Environment Variables
- Input Validation
- Logging
- Observability: Business KPIs Metrics
- Observability: Monitoring and Tracing
- Serverless Monitoring
- Serverless OpenAPI Documentation with AWS Powertools
- Start Your AWS Serverless Service With Two Clicks
Getting started
Head over to the complete project documentation pages at GitHub pages at https://ran-isenberg.github.io/aws-lambda-handler-cookbook
Code Contributions
Code contributions are welcomed. Read this guide.
Code of Conduct
Read our code of conduct here.
Connect
- Email: [email protected]
- Blog: https://ranthebuilder.cloud/
- Bluesky: @ranthebuilder.cloud
- X: @RanBuilder
- LinkedIn: https://www.linkedin.com/in/ranbuilder/
AI-Assisted Development
This project uses AI-DLC (AI-Driven Development Life Cycle) for AI-assisted software development. Learn more about AI-DLC in this blog post.
AI-DLC provides a structured, adaptive workflow for:
- Requirements Analysis - Intelligent requirements gathering and clarification
- Architecture Design - AI-assisted architectural decisions
- Code Generation - Structured code implementation with best practices
- Testing - Comprehensive test generation and validation
The AI-DLC workflow artifacts are stored in the aidlc-docs/ directory.
Claude Code Project Rules
The repository also ships persistent instructions for Claude Code and other CLAUDE.md-aware AI agents, so generated code follows the project's conventions automatically:
CLAUDE.md- project overview, layout, and planning guidelines, loaded every session..claude/rules/- topic-scoped rule files that load automatically; path-scoped rules load only when the matching files are edited:makefile.md- themakecommand reference; always drive tasks through targets.github-actions.md- CI workflows and SHA-pinning of actions.service.mdandmodels.md- handler/logic/dal layering, Powertools, and Pydantic conventions.testing.md- unit, integration, e2e, and infrastructure test patterns.cdk.md- construct and stack structure, andcdk-nagv3 suppression conventions.docs.md- documentation build, markdownlint, and OpenAPI schema sync.security.md,dependencies.md, andgit-pr.md- secrets and IAM, dependency management, and Conventional Commits.
.claude/skills/aws-emulator/- the AWS Emulator agent skill: install, connect, troubleshoot and check compatibility of the emulator the integration tests run against.
Credits
License
This library is licensed under the MIT License. See the LICENSE file.
Copyright (c) 2026 Ran Isenberg
Jelajahi proyek terkait
Memuat…
