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

vaibhav sharma
3 min readMay 24, 2021

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.

Writing an lambda function using serverless

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) => {
let param = JSON.parse(event.body);
let result = param.param1 + param.param2;
StatusCode: 200,
result: result
StatusCode: err.statusCode? err.statusCode :500,
error: ? "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.

Deploying lambda function

  • 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)



vaibhav sharma

Full Stack IoT developer working on effective Wireless Sensor Network