Rest API using AWS Lamda, Serverless and Nodejs

Prerequisite

  1. AWS Lamda : If you are not aware of lamda function, please visit the following link. http://www.fullstacktechnos.com/2017/11/what-is-aws-lamda.html
  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. http://www.fullstacktechnos.com/2017/11/create-your-first-nodejs-lamda-function.html

Setup

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
v6.11.3
 $sls -v
 1.18.1
 $aws — version
 aws-cli/1.11.127 Python/3.6.2 Darwin/17.2.0 botocore/1.5.90
 $cat ~/.aws/credentials 
 [default]
 aws_access_key_id=*******************
 aws_secret_access_key=*****************

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

https://github.com/fullstacktechnos/serverless-lamda-test

Checkout the code :

git clone https://github.com/fullstacktechnos/serverless-lamda-test.git

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);
 };

serverless.yml:

# Welcome to serverless. Read the docs

# https://serverless.com/framework/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
 provider:
 name: aws
 runtime: nodejs6.10
 
 # The `functions` block defines what code to deploy
 functions:
 helloWorld:
 handler: handler.helloWorld
 # The `events` block defines how to trigger the handler.helloWorld code
 events:
 — 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 serverless.com (https://serverless.com/framework/docs/providers/aws/guide/events/)
 
 
 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:
 None
 endpoints:
 GET — https://l2krwp3ete.execute-api.us-east-1.amazonaws.com/dev/hello-world
 functions:
 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.

https://aws.amazon.com/lambda/details/

https://serverless.com/framework/docs/providers/aws/guide/quick-start/


Originally published at www.fullstacktechnos.com on November 20, 2017.