Better local development for Serverless Functions

Shane Dowling
Feb 11, 2019 · 3 min read

Lambda is a terrific piece of kit for all the benefits listed on the AWS product page and Serverless is a very useful framework for developing Lambda functions. However, developing serverless applications locally is a total pain if what you’re solving isn’t totally trivial.

When things get complicated and your Lambda functions start to integrate with other AWS services, things really begin to break down. There are a few things that look like silver-bullets, I’ll share them here and explain why they didn’t work for me, then give you a working example that I myself struggled to find(hence me writing this).


Localstack is definitely the biggest attempt at a silver bullet here. Basically it emulates a huge chunk of AWS locally and you connect your serverless functions into that. Why I didn’t use it:

Serverless Offline

Serverless offline is fairly straightforward. It should allow you to spin up your functions locally and call them as needed. Where this fell down for me was that it required you to have HTTP endpoints for your lambda functions, all of our Lambda functions were driven either by SQS events or a Cron. We also instantiate our SNS topics in our code, so it would constantly attempt to create SNS topics and AWS would throw errors.

I also did try using serverless-offline-sqs but could not get it to drive events into our Lamdba functions.

I was burning a lot of time getting a dev environment setup and I realised that this was fundamentally the wrong approach. I needed to start writing proper unit tests and use mocking to emulate the infrastructure(I mean ideally they’d be isolated properly and infrastructure/functions would be totally ignorant of each other, but that’s for another day).

The Example

Say we’re developing an application that makes use of:

  • Lambda
  • MongoDB
  • SQS
  • SNS

Your lambda function connects to your MongoDB, does a thing and fires off occasional SNS notifications/SQS messages to other services.


For the actual code being run, I was going to use jest to start writing tests. Jest works well and integrates nicely into serverless.


To emulate MongoDB, I’m going to use an in-memory version of MongoDB that ties nicely into our Mongoose models.


For any Amazon infrastructure, we’re going to use aws-sdk-mock. This is an excellent wrapper around sinon for mocking the AWS infrastructure, that’s super useful for unit tests as you’ll see later.

Tieing it all together

So firstly lets install our dependencies.

npm install aws-sdk-mock mongodb-memory-server sinon jest --save-dev

Let’s also create a tests folder to keep all this test code in.

mkdir tests

Setting up MongoDB

Now, for our code to integrate with our in-memory Mongo-DB server, we’re going to need to add a few setup/teardown functions and a Mongo Environment. Clone this repo and put:

  • setup.js
  • teardown.js
  • mongo-environment.js

into your new test folder.

In your root directory add a jest.config.js with this content.

module.exports = {
globalSetup: './tests/setup.js',
globalTeardown: './tests/teardown.js',
testEnvironment: './tests/mongo-environment.js',
roots: ['./tests/'],

This should be enough to get MongoDB up and running. Now onto our actual tests scripts.

Setting up our tests

Now, finally, you can tie it all together with this snippet:

Add jest to your package.json

"test": "jest"

And run with npm test .

One major point to note is that aws-mock-sdk requires a very specific way of instantiating AWS resources(within the scope of the function tests), so if you see any errors around aws regions, double check you’re following the rules.


Sign up for Get Better Tech Emails via


how hackers start their afternoons. the real shit is on Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Shane Dowling

Written by

Senior Dev at Runner and terrible guitar player.

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store