Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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 silver bullet here. Basically emulate a huge chunk of AWS locally and 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. 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 was trying to do something super complicated.
- It’s 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 did try using serverless-offline-sqs but could not get 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 application connects to your uses lambda to fire off occasional SNS notifications 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. It’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 test
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 method of calling your AWS resources, so if you see any errors around aws regions, double check you’re following the rules.
Better local development for Serverless Functions was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.