How to Set Up Debugging with Dockerized SAM Local

Question:

I’m working on a project using AWS Lambda with Node.js. We use Docker containers for our development environment.

Our current setup spins up AWS SAM local on port :3000. It runs start-api and mounts the functions in my template.yml file. I test these functions using postman to send JSON to the mounted API endpoint like so: http://127.0.0.1:3000/foo

The Docker setup also spins up a separate Node.js instance on :4000.

I’m able to test the Lambda stuff locally as described above. However, I want to activate debugging so that I can step through the function and inspect variables as opposed to using console.log(). I can’t figure out how to edit the Dockerfile / docker-compose.yml to make that happen.

Here is my docker-compose file:

Here is the Dockerfile for SAM, which is in a directory called “serverless”:

I’ve tried various permutations of adding the –d flag to the “sam” service in RUN directive in docker-compose.yml. For example: sam local start-api --host 0.0.0.0 --d 8080. And then I try to change the port mapping to expose it. However, I can’t figure out how to get the port mapping to work. As soon as I hit the endpoint I get port errors.

I’m still getting up to speed on docker / docker-compose and a total nube when it comes to the Lambda stuff, so sorry if the question is silly.

TIA!

Answer:

I have determined that my initial approach was incorrect.

The benefit of making SAM Local part of my docker-compose setup –or so I thought– was that I was saving my teammates the trouble of installing it locally on their machines. And starting it in this way:

sam local start-api --host 0.0.0.0

seemed to confer the additional convenience of “automagically” mounting all of the API endpoints.

However, this does not afford the user the flexibility to toggle debugging on and off. Nor does it allow the user to switch between POST-ing input with Postman or loading the input data from a file.

So here is the way I’m doing it now:

To use Postman:

or Postman plus debugger:

To bypass Postman and simply read the input from a file:

or to do the same with a debugger:

NOTE: in the above example, "NameOfResource" must be a string and it must match the resource name listed in template.yml (in case it is different from the actual function name in the source code).

Doing it this way I am able to connect a remote Node.js debugger in WebStorm and set breakpoints. I’m also able to connect a Visual Studio Code debugger. However, Visual Studio Code seems to ignore my breakpoints, forcing me to use debugger; statements. This is unfortunate since my teammates all use Visual Studio Code and WebStorm is not free. If anyone knows how to get around the Visual Studio issue, holler!

Leave a Reply