pythonamazon-web-servicesdebuggingaws-lambdachalice

debug chalice Lambda Event Sources in vscode


Chalice does not support debug mode with Lambda Event Sources. Functions based on event sources, such as on_sqs_message, are not even triggered locally when the Chalice app is launched. This limitation raises a critical question: how can developers effectively use debug tools, breakpoints, and other essential debugging features in such a constrained environment?

While it is possible to adopt a degraded process (lacking a proper debugger, variable watcher, etc.), this approach leads to significant inefficiencies. Developers must deploy the Chalice app—a process that takes approximately three minutes—every time a code change is made. Furthermore, the added step of monitoring logs in CloudWatch increases the overhead. Such a workflow is far from acceptable in the software industry, where efficiency and rapid iteration are essential.

It is worth noting that route endpoints function correctly with the debugger at the local level.


Solution

  • The most elegant way I have found to achieve this is to use an endpoint corresponding to the event source function name you want to trigger in the debugger.

    if Environment.is_not_critical():
        # if env is dev you can activate the debug lambda mode
        # TODO: it may be usefull or not to implement end to end tests
        @app.route("/debug/{lambda_to_debug_name}", methods=["GET"])  # type: ignore # pylint: disable=E0602 # noqa:F821
        def debug(lambda_to_debug_name: str):
            """Call the lambda you want to debug from the /debug/function_to_debug_name endpoint"""
            with open(f"events/{lambda_to_debug_name}.json", "r", encoding="utf-8") as f:
                event = f.read()
                event_json = json.loads(event)
                lambda_function = globals()[lambda_to_debug_name]
                lambda_function(event_json, None)
    

    The only limitation is that you need to know the event source's event content beforehand and save it in a JSON file for the first use.