Getting started with Serverless microservices with AWS Lambda

In this article, you’ll learn how to build a node.js serverless microservice — deploy it on AWS Lambda with DynamoDb and place it behind AWS API Gateway.

We’ll be using the serverless frameworkan open-source, application framework to easily build serverless architectures on AWS Lambda.

Prerequisites

  • Node.js installed
  • Serverless framework installed — see here
  • Install AWS CLI & create IAM Credentials — For the purpose of this demo, we’ll be using an IAM user policy with `AdministratorAccess` (Don’t do that for your production apps)

Please note the AWS custom profile name is set to `[serverless-demo]` here. We’ll be using this profile in the next step.

Create the application

Let’s go ahead and generate the application template.

You’ll see the following files in your working directory:

#AppRoot/handler.js — By default template will provide a `hello` function handler.

AWS Lambda can invoke this handler when the service executes your code. Handler signature has these three arguments.

  • event — AWS Lambda uses this parameter to pass in event data to the handler.
  • context — AWS Lambda uses this parameter to provide your handler the runtime information of the Lambda function that is executing. Ex: AWS requestId, logs, timeout values etc.
  • callback — You can use the optional callback to return information to the caller, otherwise return value is null.

read more on function handler here.

#AppRoot/serverless.yml — Set the stage, profile, region parameters. The updated file should look like this.

This is more than enough to test our setup. Let’s deploy our first function to Lambda.

Once completed, lambda function can be viewed from the aws console.

While the function doesn’t do anything other than printing a message, you can still test it by clicking the “test” action.

Creating the MicroService

Let’s create an example microservice, which has a RESTful CRUD API to manage book information. The book data will be stored on a DynamoDB table.

CREATE a new book — First we’ll need to add new handler to our application.

#AppRoot/books/create.js

Let’s update the function definition in `serverless.yml` to point this new handler.

This setup specifies that the booksCreate function should be run when someone accesses the API gateway at books/ via a POST request. Further the HTTP endpoints defined here, have cross-site requests enabled for all source domains.

Creating the DynamoDB Table

One thing I really like with Serverless framework is, by simply adding the aws resource configuration to serverless.yml we can bring up other aws resources like dynamodb tables, s3 buckets etc. Serverless Framework translates all syntax in serverless.yml to a AWS CloudFormation template — automagically.

Add DynamoDB config to serverless.yml

You can learn more about AWS resources configurations here.

Deploy to AWS

During the deployment, it will setup a DynamoDb table, an API Gateway endpoint and your CREATE function will be added to Lambda functions list.

That’s it. You now have your API Gateway endpoint to invoke the REST API call. Let’s add some book data.

Your DynamoDb table is named as `serverless-demo-dev`

Monitoring

AWS Lambda automatically monitors functions and these metrics include total requests, latency, and error rates is available through AWS CloudWatch.

You can find the complete source code in GitHub. Happy coding ❤

Randika Rathugamage

Written by

Architect | Coder | Microservice, Serverless Enthusiasts | AWS Certified Solutions Architect (Professional)— Connect at https://www.linkedin.com/in/randika/