def handler()

in lambda/python/cfn_example_functions/create_drop.py [0:0]


def handler(event: dict, context):
    try:
        logger.debug(json.dumps(event))
        props = event[CFN_RESOURCE_PROPERTIES]
        for field in [PROP_CREATE_SQL, PROP_DROP_SQL]:
            assert field in props, f"{field} is a mandatory field"

        request_type = event[CFN_REQUEST_TYPE]

        if request_type == CFN_REQUEST_DELETE:
            sql_statement = props[PROP_DROP_SQL]
        elif request_type == CFN_REQUEST_CREATE:
            sql_statement = props[PROP_CREATE_SQL]
        else:
            # This component only supports constant CREATE/DELETE SQLs
            raise ValueError(f"{CFN_REQUEST_TYPE} must be one of {CFN_REQUEST_TYPES}")

        event[EVENT_SQL_STATEMENT] = sql_statement

        response = lambda_client.invoke(
            FunctionName=CDK_STEPFUNCTIONS_REDSHIFT_LAMBDA,
            InvocationType='RequestResponse',
            Payload=json.dumps(event).encode('utf-8')
        )
        logger.info(f"Lambda returned {response}")

    except Exception as ve:
        fail_reason = f"Encountered issue {ve}"
        cfnresponse.send(event, cfnresponse.FAILED, 'n/a', fail_reason)
        assert False, fail_reason