Monday, November 20, 2017

Rest API using AWS Lamda, Serverless and Nodejs


1) AWS Lamda - If you are not aware of lamda function, please visit the following link.

2) Serverless Framework - I am using serverless to deploy my aws infrastructure. Please visit the following article to setup the serverless in your local box.
You don't need to create any project though.


If you have configured serverless for aws successfully then verify the installations by typing the command in the terminal. Your version number may not match mine.

$node -v
$sls -v
$aws --version
aws-cli/1.11.127 Python/3.6.2 Darwin/17.2.0 botocore/1.5.90
$cat ~/.aws/credentials 

I have already created the sample nodejs lamda project with serverless. Please find the following  github link.

Checkout the code.

git clone

handler.js : (lamda code goes here)

'use strict';

module.exports.helloWorld = (event, context, callback) => {
  const response = {
    statusCode: 200,
    headers: {
      'Access-Control-Allow-Origin': '*', // Required for CORS support to work
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,

  callback(null, response);


# Welcome to serverless. Read the docs

# Serverless.yml is the configuration the CLI
# uses to deploy your code to your provider of choice

# The `service` block is the name of the service
service: serverless-hello-world

# The `provider` block defines where your service will be deployed
  name: aws
  runtime: nodejs6.10

# The `functions` block defines what code to deploy
    handler: handler.helloWorld
    # The `events` block defines how to trigger the handler.helloWorld code
      - http:
          path: hello-world
          method: get
          cors: true

To define a rest endpoint we have to mention the method (get or post) and path inside events -> http.
cors: true will enable the cross.

To know more about it please visit the (

If you want to push the same code to your aws environment then run the following command.

$sls deploy

Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (967 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
Serverless: Stack update finished...
Service Information
service: serverless-hello-world
stage: dev
region: us-east-1
api keys:
  GET -
  helloWorld: serverless-hello-world-dev-helloWorld

- Go to the browser and enter the GET endpoint URL. Your's will be different than mine. Mine is for testing purpose. It wont be available for you.

- You can see the event object that lamda return as a response in the browser.

* For more information please visit the following link.

1 comment:

  1. Kanhasoft is the top-notch Node.js App development company in India. We have 45+ experienced programmers to delivered successful NodeJS Application development globally. Visit our site to know more about us.