Modern CloudFormation Custom Resource lifecycle handler for AWS Lambda
Project description
cfn-handler
A modern, well-engineered Python library for writing AWS CloudFormation
Custom Resource lifecycle handlers. Inspired by — but not derived from —
the unmaintained aws-cloudformation/custom-resource-helper.
Why cfn-handler?
CloudFormation Custom Resources are AWS Lambda functions invoked during stack operations. Getting them right is harder than it looks: you must respond to a presigned URL within the Lambda timeout, handle CREATE/UPDATE/DELETE semantics correctly, manage long-running operations via polling, and never leave CloudFormation hanging.
cfn-handler does the boilerplate so you can focus on the resource logic.
from cfn_handler import CustomResource
resource = CustomResource()
@resource.create
def on_create(event, context):
# do work, return data dict
return {"Endpoint": "https://my.example.com"}
@resource.update
def on_update(event, context):
return {"Endpoint": "https://my.example.com"}
@resource.delete
def on_delete(event, context):
pass
def handler(event, context):
return resource(event, context)
That's the entire happy path. For long-running operations:
@resource.create
def on_create(event, context):
# kick off work; return None to defer
pass
@resource.poll_create
def on_poll_create(event, context):
# check status; return data when done, raise on failure
if check_ready():
return {"Endpoint": "https://my.example.com"}
# else: do nothing, library will reschedule
Installation
pip install cfn-handler
# or with uv
uv add cfn-handler
cfn-handler requires Python 3.10+ and has zero runtime dependencies.
Polling support uses boto3 lazily; boto3 ships preinstalled in the AWS
Lambda Python runtimes, so no extra install is needed there.
Or use the AWS Lambda Layer
Every release publishes a public Lambda Layer in ~17 commercial regions. To
use it, reference the ARN in your function definition — no pip install
during deploy, no vendoring into your function package:
# SAM
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.12
Layers:
- arn:aws:lambda:us-east-1:<account-id>:layer:cfn-handler:N
Find the right ARN for your region in the latest release notes (per-region table) or the JSON manifest:
curl -fsSL https://github.com/igorlg/cfn-handler/releases/latest/download/layer-arns.json
See layer/README.md for SAM/CDK snippets, the alternative
deploy-it-yourself path, and the maintainer's account ID.
Comparison to crhelper
If you're coming from crhelper, the model will feel familiar:
| Feature | crhelper | cfn-handler |
|---|---|---|
| Lifecycle decorators | @helper.create, @helper.update, @helper.delete |
@resource.create, @resource.update, @resource.delete |
| Polling decorators | @helper.poll_create ... |
@resource.poll_create ... |
| Type hints | None | Full inline (py.typed) |
| Python versions | 3.6+ (last release 2020) | 3.10–3.14 |
| Build | setup.py |
pyproject.toml (PEP 621, hatchling) |
| Tests | unittest | pytest + hypothesis |
| Coverage gate | None | 95% line + branch |
| Type checkers | None | mypy strict + pyright strict |
| Releases | Manual | release-please + PyPI Trusted Publishing |
| Maintained | No (since 2020) | Yes |
The public API is intentionally similar — cfn-handler is a clean
re-implementation that carries forward the proven semantics and fixes 14
long-standing upstream issues that never merged. See CHANGELOG.md
for the full list.
Examples
Working SAM-deployable examples live in examples/:
examples/basic/— minimal Create/Update/Delete handler.examples/polled/— long-running operation with polling.examples/with-physical-id/— explicit physical resource id (replacement on update).examples/failing/— handler that fails, demonstrating FAILED-response semantics.
Project status
v1.0.0 — first stable release. Follows Semantic Versioning.
The public API surface is exactly what's exported from cfn_handler.__all__;
everything under cfn_handler._internal is implementation detail and may
change between minor versions.
Contributing
See CONTRIBUTING.md for the development workflow,
commit conventions, and lockfile policy. For contributors who use Nix, a
flake.nix provides a reproducible dev shell. The CI/release pipeline
itself is documented in docs/CI.md, including the
local-replay tooling and a postmortem of the v1.0.0 release failure.
License
Apache License 2.0 — see LICENSE and NOTICE.
This project is inspired by, but does not include code from, aws-cloudformation/custom-resource-helper. Both projects are licensed under Apache 2.0.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cfn_handler-1.2.0.tar.gz.
File metadata
- Download URL: cfn_handler-1.2.0.tar.gz
- Upload date:
- Size: 22.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75b6be50ca7628eee5238c2bc49f6a95171cf9f37556121c8b82bf08f5147e33
|
|
| MD5 |
0079db0c582a50d40fe874a2bb2f0d59
|
|
| BLAKE2b-256 |
2e792d84ea34462e790983aa9e51c199da3c74160580152e1155e5af390de9ac
|
Provenance
The following attestation bundles were made for cfn_handler-1.2.0.tar.gz:
Publisher:
release.yml on igorlg/cfn-handler
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cfn_handler-1.2.0.tar.gz -
Subject digest:
75b6be50ca7628eee5238c2bc49f6a95171cf9f37556121c8b82bf08f5147e33 - Sigstore transparency entry: 1591186681
- Sigstore integration time:
-
Permalink:
igorlg/cfn-handler@a2192f7d4aa6605f99faf377b7f0df7cd3460854 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/igorlg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a2192f7d4aa6605f99faf377b7f0df7cd3460854 -
Trigger Event:
push
-
Statement type:
File details
Details for the file cfn_handler-1.2.0-py3-none-any.whl.
File metadata
- Download URL: cfn_handler-1.2.0-py3-none-any.whl
- Upload date:
- Size: 21.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5eb2c87fc54be859cc2fb3e92f7d1a6fa8fef27e24b296bd85373717a87bd28
|
|
| MD5 |
6ec196cb1311170887ca8912963f9e95
|
|
| BLAKE2b-256 |
a80834a0fdcd72223f9422122d1a3474cde29878938c0588ad42555a3fe9ed33
|
Provenance
The following attestation bundles were made for cfn_handler-1.2.0-py3-none-any.whl:
Publisher:
release.yml on igorlg/cfn-handler
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cfn_handler-1.2.0-py3-none-any.whl -
Subject digest:
a5eb2c87fc54be859cc2fb3e92f7d1a6fa8fef27e24b296bd85373717a87bd28 - Sigstore transparency entry: 1591186690
- Sigstore integration time:
-
Permalink:
igorlg/cfn-handler@a2192f7d4aa6605f99faf377b7f0df7cd3460854 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/igorlg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a2192f7d4aa6605f99faf377b7f0df7cd3460854 -
Trigger Event:
push
-
Statement type: