cfnlambda icon indicating copy to clipboard operation
cfnlambda copied to clipboard

Allow cfn_response to be called inside handler

Open benkehoe opened this issue 9 years ago • 0 comments

If the handler knows the physical resource ID, it has to call cfn_response itself. I've made Status more complex to allow the following workflow (while allowing backwards compatibility):

@handler_decorator()
def handler(event, context):
    valid = validate(event)
    if not valid:
        return Status.getFailed('event failed validation')
    try:
        success, physical_resource_id, data = handler_logic(event)
    except Exception, e:
        return Status.getFailed(str(e))

    status = Status.SUCCESS if success else Status.getFailed('failed')
    resp = cfn_response(event, context, status, 
            physical_resource_id=physical_resource_id, response_data=data)
    # optionally do something with response object

    return Status.getFinished(status) # wrapper still takes care of cleanup based on status

The new status can also be used without calling cfn_response in the handler:

@handler_decorator()
def handler(event, context):
    valid = validate(event)
    if not valid:
        return Status.getFailed('event failed validation')
    try:
        success, physical_resource_id, data = handler_logic(event)
    except Exception, e:
        return Status.getFailed(str(e))

    status = Status.SUCCESS if success else Status.getFailed('failed')
    return status, data # wrapper calls cfn_response

I also made handler_decorator work when it's used without parentheses.

benkehoe avatar Nov 03 '15 13:11 benkehoe