Today we're going to look at a simple set up to get AWS Lambda Functions running in VS Code.  You'll be able to run your code locally in a Docker container with the Lambda runtime and debug your function through the VS Code debugging tool.

My development environment is an Ubuntu Linux laptop with Node.js code running in the Lambda.  These tools are open source and cross platform, so you should be able to install these tools on a variety of systems.  However, if you are running another AWS Lambda supported language, such as Python, you might need to change some configurations to match your codebase.

Prerequisites

VS CODE

Visual Studio Code is a cross platform IDE developed by Microsoft.  You can find download links and instructions here: https://code.visualstudio.com/Download

AWS SAM CLI

The AWS Serverless Application Model (SAM) CLI tool is a program that allows you to scaffold, run, and deploy serverless applications.  You can find download links and instructions here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html.  The SAM CLI will require Docker as well.  Documentation is available on that guide to get it installed.

Creating your Serverless Function with SAM

The SAM CLI allows you to create a new lambda project with defined Templates.  The tool makes it easy to set this up with this command.

sam init

Follow through the prompts to create your application.  I chose a Nodejs12.x application.

Package.json

I had to edit the package.json file in the SAM template to make this work.  Below are the npm start and npm run debug commands I created to run the SAM environment with NPM.

The most important sections are lines 9 and 10.  These will actually launch the SAM CLI tooling instead of Node.js.  The debug script will specifically be used to hook into the VS Code Debugger tools.  

Launch.json

This is a crucial configuration for setting up these two tools to work together.  The launch.json is a file that VS Code uses when setting up the debugger.  I have my code posted below, and I'll walk through the important parts.

The type is node, so if you are using a different programming language, you'll need to use an appropriate configuration in VS Code.  On line 9 is cwd or current working directory.  You'll need this to work with the application structure generated by the SAM CLI.

For debugging, the lines to add are 18 and 19 for localRoot and remoteRoot.  This will allow VS Code to map your local JS files to the JS files running in the Lambda container.  

Once you have the files set up, give it a try!  Set a breakpoint in app.js and launch the VS Code Debugger.  You should see the execution pause on your breakpoint and have the ability to inspect variables.

Hope this was helpful!  If it was or you want to make some suggestions, feel free to send me an email at mitch.gollub@gmail.com.