def lambda_handler()

in workmail-ws1-integration/src/app.py [0:0]


def lambda_handler(event: dict, context) -> dict:
    """
    WS1 event handler.

    See: https://docs.aws.amazon.com/workmail/latest/adminguide/mdm-integration.html

    :param event: dict, containing information received from WS1. Examples:
        tst/lambda_test_connection.json
        tst/lambda_event_notification.json

    :param context: lambda Context runtime methods and attributes. See:
        https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

    :return: dict, containing HTTP response status code. Example:
        {"statusCode": 200}
    """
    try:
        logging.debug(event)

        if event["httpMethod"] == "GET":
            logging.info("WS1 Test Connection")

        elif event["httpMethod"] == "POST":
            logging.info("WS1 Event Notification")
            check_authorization(event)
            ws1_device_data = load_ws1_device_data(event)
            update_workmail_device_access(ws1_device_data)

        else:
            raise WS1IntegrationException("Unknown request")

    except WS1IntegrationException as e:
        logging.warning(f"WS1IntegrationException: {e} - ignoring the event")
        logging.debug(e, exc_info=True)  # change log level to DEBUG to see stack traces
        return {"statusCode": e.status_code}

    except Exception as e:
        logging.exception(e)
        return {"statusCode": 500}

    return {"statusCode": 200}