Running and debugging AWS Lambda functions locally with the Serverless framework and VS Code

One of the com­plaints devel­op­ers often have for AWS Lamb­da is the inabil­i­ty to run and debug func­tions local­ly. For Node.js at least, the Server­less frame­work and VS Code pro­vides a good solu­tion for doing just that.

An often under­used fea­ture of the Server­less frame­work is the invoke local com­mand, which runs your code local­ly by emu­lat­ing the AWS Lamb­da envi­ron­ment. Grant­ed, it’s not a per­fect sim­u­la­tion and only works with Node.js and Python, but it has been good enough for most of local devel­op­ment needs.

With VS Code, you have the abil­i­ty to debug Node.js appli­ca­tions, includ­ing the option to launch an exter­nal pro­gram.

Put the two togeth­er and you have the abil­i­ty to local­ly run and debug your Lamb­da func­tions.

Step 1 : install Serverless framework as dev dependency

In gen­er­al, it’s a good idea to install Server­less frame­work as a dev depen­den­cy in a project because:

  1. it allows oth­er devel­op­ers (and the CI serv­er) to use the Server­less frame­work for deploy­ment with­out hav­ing to install it them­selves
  2. it pre­vents incom­pat­i­bil­i­ty issues when you have an incom­pat­i­ble ver­sion of Server­less frame­work installed to that used by the serverless.yml file in the project
  3. since Server­less v1.16.0 dev depen­den­cies are exclud­ed from the deploy­ment pack­age so it wouldn’t add to your deploy­ment size (this is bro­ken in the cur­rent ver­sion v1.18.0 but should be fixed short­ly)

Step 2 : add debug configuration

Invoke the “sls invoke local” CLI com­mand against the “hel­lo” func­tion with an emp­ty object {} as input. It’s also pos­si­ble to invoke the func­tion with a JSON file, see doc here.

Step 3 : enjoy!

There, nice and easy :-)

Cou­ple of things to note:

  • if your func­tion depends on envi­ron­ment vari­ables, then you can set those up in the launch.json con­fig file in step 2
  • if your func­tion needs to access oth­er AWS resources, then you also need to set­up the rel­e­vant envi­ron­ment vari­ables (eg. AWS_PROFILE) for the aws-sdk to access those resources in the cor­rect AWS account
  • this approach will not work for recur­sive func­tions (well, the recur­sion will hap­pen on the deployed Lamb­da func­tion, so you won’t be able to debug it)

Like what you’re read­ing? Check out my video course Pro­duc­tion-Ready Server­less and learn the essen­tials of how to run a server­less appli­ca­tion in pro­duc­tion.

We will cov­er top­ics includ­ing:

  • authen­ti­ca­tion & autho­riza­tion with API Gate­way & Cog­ni­to
  • test­ing & run­ning func­tions local­ly
  • CI/CD
  • log aggre­ga­tion
  • mon­i­tor­ing best prac­tices
  • dis­trib­uted trac­ing with X-Ray
  • track­ing cor­re­la­tion IDs
  • per­for­mance & cost opti­miza­tion
  • error han­dling
  • con­fig man­age­ment
  • canary deploy­ment
  • VPC
  • secu­ri­ty
  • lead­ing prac­tices for Lamb­da, Kine­sis, and API Gate­way

You can also get 40% off the face price with the code ytcui. Hur­ry though, this dis­count is only avail­able while we’re in Manning’s Ear­ly Access Pro­gram (MEAP).