Creating your first Serverless API

Serverless most often refers to applications that don't require you to provision or manage any servers. You can focus on your core product and business logic instead of responsibilities like operating system (OS) access control, OS patching, provisioning, right-sizing, scaling, and availability.

It's a revolution in the current technical trend. A detailed post on serverless can be found below:

AWS Serverless
Let’s see what does Serverless means!

Now, let us get our hand dirty with some code.

We will create a Node JS based Lambda API. For this we will use the readily provided architecture from AWS.

Creating the business logic code

Steps:

  • Log on to your AWS Console
  • Search for Lambda
    1
  • Go to Lambda Dashboard and click Create function
  • Select "Author from Scratch", add a name and choose Runtime as Node.
    2
  • Keep everything else as it is and click create function
  • This should bring you to below dashboard
    3
    1. Function name
    2. The place where we can write the code
  • Now let us write some code
exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify({
            status: 'Success',
            message: 'Hello from Lambda!'
            }),
    };
    return response;
};

This code returns a JSON response with status as Success and message as Hello from Lambda! Deploy this code by clicking Deploy button.

  • Now let us test this for output
    4-1
    We give a name to the test event and click create. Once done we can use this to test our Lambda
  • We click on Test Button and we can see the result from our code
    5

Adding an API Gateway

API Gateway can be used as a trigger and the code.

Check here for more details on Gateway and it's use:

API Gateway
What are the benefits of API Gateway?

Steps to attach an API Gateway

  • Click on add Trigger
    6
  • Add API Gateway Trigger for REST API as below
    7
  • Now the API Gateway will be attached and function overview will look as below
    8
  • On clicking thr trigger you can see the trigger details and API endpoint
    9
  • Finally, call the endpoint using postman
    10

All right!! After following the above process, we have successfully created our first Lambda API  without opening any Editor or IDE!

Interesting, isn't it ?

Hope you liked it! Cheers 🍻

Tirthankar Kundu

Tirthankar Kundu