Skip to main content

Implement custom AWS CloudFormation resources with Python Lambda functions.

This helps you reference non-AWS resources within CloudFormation - for instance, let’s get a StatusCake alert in our stack:

Type: Custom::StatusCakeTest
Version: 1.0
Properties:
  WebsiteName: vitalwebsite.com
  WebsiteURL: http://vitalwebsite.com
  CheckRate: 300
  TestType: HTTP

(Implementation left as an exercise for the reader)

See also

  • Sample code in the examples directory.

  • AWS docs

Lambda Handler

Here’s a simple resource:

from custom_resource import BaseHandler

class Handler(BaseHandler):
    def create(self, event, context):
        return "CreatedId", {"AndSome": "MetaData"}

    def update(self, event, context):
        return "UpdatedId", {"AndSome": "MetaData"}

    def delete(self, event, context):
        return "DeletedId", {"AndSome": "MetaData"}

handler = Handler()

We extend the BaseHandler class, and implement create, update and delete methods. All of these methods are required.

The methods must either:

  • Return a string representing a resource ID. This can be used within your CFN template via the Ref function.

  • Return a resource ID string, and a dict containing strings for keys and values. These key/value pairs can be used with the GetAtt function.

  • Return a custom_resource.Success or custom_resource.Failed object.

  • Return a custom_resource.Defer object, signifying you’ll process this asynchronously. See async responses below.

  • Raise an exception.

BaseHandler will respond to CloudFormation unless Defer is returned.

Async responses

Your Handler method can return Defer to signal asynchronous processing.

To later respond, you need a copy of the original event object. This will be passed to the Responder class, e.g:

with Responder(event) as responder:
    responder.success(physical_resource_id="123", data={"Meta": "Data"})
    # or
    responder.failure(physical_resource_id="456", reason="Something went wrong")

Using with, your resource will always respond to CloudFormation even on exception - ensuring your stack doesn’t stall and eventually timeout.

Release files for custom_resource 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for custom_resource 0.1.9
File Size Uploaded
custom_resource-0.1.9.tar.gz 5.2 kB Details

Release files / custom_resource-0.1.9.tar.gz

Download URL custom_resource-0.1.9.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
943b2d4efba2a76a3eeaf970e98807310084f7025db58e3019e6de1cc1504fd4
BLAKE2b-256 checksum
How to use checksums
a8d255787a4308ac957d7fecb80870399c2cf44de6956114f066ff654a8d099e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.9 This release

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

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