Skip to content
Ben Kehoe edited this page Feb 8, 2018 · 4 revisions

cfn_custom_resource provides an abstract base class to make it easier to implement AWS CloudFormation custom resources. It was developed from Gene Wood's cfnlambda, which provides lower-level functions and decorators for the same purpose.

At its simplest, you subclass the CloudFormationCustomResource and implement three methods: create, update, and delete, for each of the respective CloudFormation actions. Indicate failure by raising an exception. If you want attributes available on the resource within CloudFormation, return them as a dictionary. Your subclass has a class method get_handler, which will return a Lambda handler function. Logging to CloudWatch is provided by the logger field.

from cfn_custom_resource import CloudFormationCustomResource

# For resources of type Custom::MyCustomResource
class MyCustomResource(CloudFormationCustomResource):
    def create(self):
        # Implement
        # For AWS SDKs, use:
        #   self.get_boto3_client(service_name)
        #   self.get_boto3_resource(service_name)
        # Set the name of what you are creating to the value in
        #   self.physical_resource_id
        # This id is autogenerated for you, but you can set it if you want or need
        # This id is what CloudFormation uses for Ref
        # The resource properties defined in the template are in
        #   self.resource_properties
        # Attributes can be set by returning a dictionary

    def update(self):
        # Implement
        # The name of what you previously created is
        #   self.physical_resource_id
        # The updated properties are in
        #   self.resource_properties
        # To check what's changed, compare with
        #   self.old_resource_properties
        # If you set attributes in create(), you need to set them here too  

    def delete(self):
        # implement

handler = MyCustomResource.get_handler()

Further reading

Clone this wiki locally