Rest API using AWS Lamda, Serverless and Nodejs

in #awslamda7 years ago

Prerequisite

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

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:

service: serverless-hello-world

provider:
name: aws
runtime: nodejs6.10

functions:
helloWorld:
handler: handler.helloWorld

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/

Sort:  

Congratulations @pattanayak.raja! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @pattanayak.raja! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!