Skip to main content

Implement custom AWS CloudFormation resources with Python Lambda functions.

Project description

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.

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

custom_resource-0.1.9.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file custom_resource-0.1.9.tar.gz.

File metadata

File hashes

Hashes for custom_resource-0.1.9.tar.gz
Algorithm Hash digest
SHA256 943b2d4efba2a76a3eeaf970e98807310084f7025db58e3019e6de1cc1504fd4
MD5 f9fa06dbb14a8022e15a8da7c8da9099
BLAKE2b-256 a8d255787a4308ac957d7fecb80870399c2cf44de6956114f066ff654a8d099e

See more details on using hashes here.

Supported by

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