Serverless tutorial on creating Rest API with API Gateway and AWS Lambda function

In this guide, we will be learning following concepts:

  1. Creating your first Serverless application using AWS Lambda and API Gateway
  2. Creating AWS lambda function
  3. Adding API gateway event to Lambda function
  4. Creating POST REST API using API Gateway with Lambda integration

In the last tutorial we go through guide to setup Serverless framework for AWS and all the prerequisites. If you have not set it up already go through the previous tutorial.

Your basic serverless template contains two file serverless.yml that contains the basic orchestration of projects like permissions, project information, lambda function information and events and handler.js which is your main function or code that serves as a lambda function.

  • Here we will write an lambda function which takes two parameters and add these parameters to give us the result of addition
  • So first create a file add.js
  • add.js takes two parameter param1 and param2 as POST request. Add these parameters and give the result of addition in response.
  • If any error occurs it returns an error.
'use strict';exports.handler = async (event) => {
try{
let param = JSON.parse(event.body);
let result = param.param1 + param.param2;
console.log("result",result);
return{
StatusCode: 200,
body:JSON.stringify({
result: result
})
}
}catch(err){
console.log("Error",err);
return{
StatusCode: err.statusCode? err.statusCode :500,
body:JSON.stringify({
error: err.name ? err.name: "Exception",
message: err.message ? err.message : "Unknown error"
})
}
}};
  • Now we will edit serverless.yml. In this file we will add an HTTP event. We will add path as add and since we are creating a POST API we will add method as post.
  • To deploy the function in AWS type serverless deploy -s prod where prod is the stage name
  • So stack has been deployed and you can see /add API endpoint created for us. Here you can also see name of the function in functions. Now lets test this out
  • To test this we will use Postman. Copy the API endpoint and paste in Postman. Choose POST request. write a body in json
{"param1" : 1, "param2": 3}
  • To review the API. Go to AWS console and search for API Gateway. In API Gateway searh for the project name. Here you can see the lambda integration and method and resource name
  • To review the lambda function, Go to AWS Lambda and search for the fucntion
  • Click on the function name. And on the next page we can see the API Gateway event which triggers the lambda function.

Hurray! In this guide we have learnt to create our first serverless application. In the next tutorial, we will digmore into API Gateway and Lambda function(link)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
vaibhav sharma

Full Stack IoT developer working on effective Wireless Sensor Network