def cfn_handler()

in community/custom_resources/python_custom_resource_helper/crhelper.py [0:0]


def cfn_handler(event, context, create, update, delete, logger, init_failed):
    logger.info("Lambda RequestId: {} CloudFormation RequestId: {}".format(context.aws_request_id, event['RequestId']))

    # Define an object to place any response information you would like to send
    # back to CloudFormation (these keys can then be used by Fn::GetAttr)
    response_data = {}

    # Define a physical_id for the resource, if the event is an update and the
    # returned physical_id changes, cloudformation will then issue a delete
    # against the old id
    physical_resource_id = None

    logger.debug("EVENT: {}".format(event))
    # handle init failures
    if init_failed:
        send(event, context, "FAILED", response_data, physical_resource_id, logger, init_failed)
        raise Exception('FAILED')

    # Setup timer to catch timeouts
    t = threading.Timer((context.get_remaining_time_in_millis() / 1000.00) - 0.5,
                        timeout, args=[event, context, logger])
    t.start()

    try:
        # Execute custom resource handlers
        logger.info("Received a {} Request".format(event['RequestType']))
        if event['RequestType'] == 'Create':
            physical_resource_id, response_data = create(event, context)
        elif event['RequestType'] == 'Update':
            physical_resource_id, response_data = update(event, context)
        elif event['RequestType'] == 'Delete':
            delete(event, context)

        # Send response back to CloudFormation
        logger.info("Completed successfully, sending response to cfn")
        send(event, context, "SUCCESS", response_data, physical_resource_id,
             logger=logger)

    # Catch any exceptions, log the stacktrace, send a failure back to
    # CloudFormation and then raise an exception
    except Exception as e:
        logger.error(e, exc_info=True)
        send(event, context, "FAILED", response_data, physical_resource_id, logger=logger, reason=e)
        raise Exception('FAILED')
    finally:
        # Cancel timer before exit
        t.cancel()