Serverless microservices on AWS Lambda using serverless framework w/NodeJS


Many of us cringe at the cost of our cloud hosting fees. At Dermveda we have started a migration over to micro-services to break down our monolithic API. This helps us build smaller teams that can work on scoped functionality of our website with clear lines of communication between services. A side effect of micro-services are that you need many additional servers running your services. This not only increases your cost but also your chances of problems when it comes to reliability. Solution: Server-less micro-services! Server-less architecture gives you the advantage of reliability and low cost. Through AWS Lamba, you currently only get charged per Lamba execution. When writing this article AWS is charging ‘$0.20 per 1 million requests thereafter ($0.0000002 per request)’. The key point here is ‘per request’. This is killer when it comes to low traffic services. This is a short guide to show you how to create your first server-less service. Download github project here.


server-less: The general design architecture.


Server-less architecture has come a long way and has some awesome pros but it also has some potentially fatal flaws. Many of us know that writing software is not just about a great product, but also about scalability and writing applications that are easy for other developers to understand, debug, and build on top of. I love where server-less architecture takes us but building a server-less application is just half the battle, we need to make sure we can write applications that do not make developers lives harder. Server-less applications have unfortunate dependencies on cloud services and do not always help with identifying the relationships between our code. I chose to work with a new product called ‘serverless’ that essentially makes organization and fluidity that much easier. I believe this product is the glue that holds together readability and fluidity of server-less applications and I am excited where they are heading.

Getting Started

Now that positions and opinions are out of the way let’s build something. As stated before we will being using serverless cli to build our service. This project also requires an AWS account and NodeJS installed on your machine. For NodeJS installation your can follow the steps here.

npm install -g serverless
Create user with name serverless-admin
Add ‘Administrators’ group to user
serverless config credentials --provider aws --key EXAMPLEKEY --secret EXAMPLESECRET
$ serverless create --template aws-nodejs --path my-service 
$ cd my-service
- handler.js
- serverless.yml

User.js Model

We will need a User model use when getting and saving users to and from our database. Make sure to also install mongoose via npm.

npm install mongoose --save
- user.js


The serverless.yml file is used by the serverless library to handle setting up and structuring your server-less configuration in AWS. This file can do a whole bunch or really cool things but we will be using it to create our AWS Lambda functions and setting up our API Gateway. Delete the current content of serverless.yml and paste in the code below:


This is your main source file for all your AWS Lambda functions. Remove any boilerplate code and add the two functions in the code below:


This function merely checks our MongoDB for any users with matching email to the parameter passed through the url.


This function is for saving new users. By sending a POST to function we can pass in a new email and our Lambda function will create a new user in the database.

Callback timeout

Above you will notice this very important line of code:

context.callbackWaitsForEmptyEventLoop = false;
callback(null, response);


This is the most streamlined part of this build, just run:

serverless deploy -v
serverless deploy function -f getUser


Testing can be done in either the AWS Lambda GUI or API Gateway in AWS or through the terminal using serverless. Using ‘ — path’ or ‘’— data’ to pass data to your function.

serverless invoke local --function getUser --path data.json


Currently debugging is not so elegant. I have found that using AWS Lambda console logs work for most cases. Luckily serverless has a great feature of retrieving current logs and future logs (live feed) for a specified function. I have listed an example for requesting logs for a specific function below.

serverless logs -f getUser


This example has been quite a simple one but I hope it gives you the foundation to use serverless to build your own applications. Be sure to read more of serverless documentation and checkout more examples to see all it’s true potential!

More Resources

Want more? Checkout a recent video tutorial I did on Serverless framework and micro-services.

Staff Engineer @Confluent | Ex-Amazon