Better local development for Serverless Functions
Posted on Mon 11 February 2019 in Tech
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
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:
- Bits worked, but if you have any sort of half complicated setup(like a region outside us-east-1 or eu-west-1), it will break down. Uploading a whole cloudformation template was a disaster of broken links between services. I got to the point of editing localstack code to try fix issue after issue I was coming across and gave up. The product would be cool if it wasn't trying to do something so super complicated.
- It's also a huge resource drain on a developer's machine.
- Fundamentally, you shouldn't have to emulate half of AWS to test your code.
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.
Lambda
For the actual code being run, I was going to use jest to start writing tests. Jest works well and integrates nicely into serverless.
MongoDB
To emulate MongoDB, I'm going to use an in-memory version of MongoDB that ties nicely into our Mongoose models.
SQS/SNS
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
"scripts":{
"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.