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 contributors who use Nix, a flake.nix provides a reproducible dev shell.

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.0.0.tar.gz (19.2 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.0.0-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cfn_handler-1.0.0.tar.gz
  • Upload date:
  • Size: 19.2 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.0.0.tar.gz
Algorithm Hash digest
SHA256 60ba3c3c0012dd871047384213a92e4b02cc0c2e541bd382f80a06dadc165760
MD5 fe96672fdbb2568ac1a484c2030650c7
BLAKE2b-256 e0d26e9ceccce4714391070a578a7d9e1b12db44e94bd255470090803e51bb2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfn_handler-1.0.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.0.0-py3-none-any.whl.

File metadata

  • Download URL: cfn_handler-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.5 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 62496b287ce7e61e5598470ecdceeff2078e8885cb26264efac5a646a61bf60f
MD5 325ce22e4b40e42932c27ec561e84c7a
BLAKE2b-256 f6a28bf67d7fd2b2dc471d7169adaccffa6a83a859eb65a4843e8acc6b8b20d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfn_handler-1.0.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