Skip to main content

Modern CloudFormation Custom Resource lifecycle handler for AWS Lambda

Project description

cfn-handler

CI PyPI Python Versions License

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.

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

cfn_handler-1.1.0.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

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

cfn_handler-1.1.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file cfn_handler-1.1.0.tar.gz.

File metadata

  • Download URL: cfn_handler-1.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for cfn_handler-1.1.0.tar.gz
Algorithm Hash digest
SHA256 da8b96119a3d10d1ef32a23ecd72fd914f8353f3452f3787eed78a46eaaaebd2
MD5 0c9f6678ed9b7ac671bb5a7488706567
BLAKE2b-256 7c6fcded0d1eea085d31ac5ce965d6cf6af3d7eea876d8d368a78f36b0f2e294

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfn_handler-1.1.0.tar.gz:

Publisher: release.yml on igorlg/cfn-handler

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

File details

Details for the file cfn_handler-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: cfn_handler-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for cfn_handler-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80e3629878eb195d8bc0be8d8cbe1dbbe1a44a0f2f20f3cd863a7e6222292913
MD5 5e82f6b1c7de0a789cabce33eeacf63a
BLAKE2b-256 778364ff6f1bf28e70b38061c1d0b36894c58a1511c0410428e6a6589b030a20

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfn_handler-1.1.0-py3-none-any.whl:

Publisher: release.yml on igorlg/cfn-handler

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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page