def handler()

in cdk-stacks/lambdas/custom-resources/frontend-config/index.py [0:0]


def handler(event, context):

    def cfn_error(message=None):
        logger.error("| cfn_error: %s" % message)
        cfn_send(event, context, CFN_FAILED, reason=message)

    try:
        logger.info(event)

        # cloudformation request type (create/update/delete)
        request_type = event['RequestType']

        # extract resource properties
        props = event['ResourceProperties']
        old_props = event.get('OldResourceProperties', {})
        physical_id = event.get('PhysicalResourceId', None)

        bucket_name = props["BucketName"]
        object_key = props["ObjectKey"]

        # if we are creating a new resource, allocate a physical id for it
        # otherwise, we expect physical id to be relayed by cloudformation
        if request_type == "Create":
            physical_id = "vce.frontend-config.%s" % str(uuid4())
        else:
            if not physical_id:
                cfn_error(
                    "invalid request: request type is '%s' but 'PhysicalResourceId' is not defined" % request_type)
                return

        if request_type == "Create" or request_type == "Update":
            web_app_staging_object_prefix = props["WebAppStagingObjectPrefix"]
            web_app_root_object_prefix = props["WebAppRootObjectPrefix"]
            object_content = props.get("Content")
            object_content_type = props.get("ContentType")
            create(bucket_name, web_app_staging_object_prefix, web_app_root_object_prefix, object_key,
                   object_content, object_content_type)

        if request_type == "Delete":
            web_app_staging_object_prefix = props["WebAppStagingObjectPrefix"]
            delete(bucket_name, web_app_staging_object_prefix, object_key)

        cfn_send(event, context, CFN_SUCCESS, physicalResourceId=physical_id)

    except KeyError as e:
        cfn_error("invalid request. Missing key %s" % str(e))
    except Exception as e:
        logger.exception(e)
        cfn_error(str(e))