DEV Community

Rahul Lokurte
Rahul Lokurte

Posted on • Edited on

1

Build Serverless Api Using lambda-api

Serverless application can be easily created with AWS lambda and AWS Gateway. We can write an lambda function and configure the gateway. We get a Rest endpoint which we can call to reach the lambda function. This approach is easy, but comes with a caveats. We have to configure API gateway, manage the deployments, publish to stages.

In this blog, we will see how to develop and deploy a serverless application using
Lambda API, a lightweight framework for developing serverless applications.

Lets create a new project directory.

mkdir serverless-samples
cd serverless-samples
Enter fullscreen mode Exit fullscreen mode

We will initiate the project and Install lambda-api.

npm init
npm i lambda-api
Enter fullscreen mode Exit fullscreen mode

The Lambda-api just needs the 3 steps.

  1. Require the framework and Instantiate it.
  2. Define a route.
  3. Declare our Lambda Handler.

Create a new handler.js file and add the below contents.

//----------------------------------------------------------------------------//
// Require the framework
//----------------------------------------------------------------------------//

const app = require("lambda-api")({ version: "v1.0", base: "v1" });

//----------------------------------------------------------------------------//
// Define an API routes
//----------------------------------------------------------------------------//

// GET
app.get("/greet/:name", (req, res) => {
  // Send the response
  res.status(200).json({
    //name: req.params,
    status: "ok",
    body: "Hello " + req.params.name,
  });
});

//----------------------------------------------------------------------------//
// Lambda handler
//----------------------------------------------------------------------------//
module.exports.router = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;

  // Run the request
  app.run(event, context, callback);
}; // end router handler
Enter fullscreen mode Exit fullscreen mode

In the above code, we have created an GET endpoint with /greet/:name. The Path parameter is name which will be passed to the GET endpoint. The API will return the response with statuscode as 200 and the body will greet a person with name that is passed in the pathparams. To run the request, we use app.run() method.

Deploy to AWS

Login into AWS and search for Lambda service. Create a new function, provide the name and Runtime as shown below.

create-function

Create a zip file of the project and upload the zip file to function serverless-sample and change the handler to handler.router.

handler-router

Invoke the function with a test event. We need to choose the template that matches the service that triggers our function or we can also create a event document in Json.

serverless-test-event

As shown above, we have to select a New event option and select apigateway-aws-proxy template and give an test event name as gatewaysample. We will get a sample event which is compatible with the api-gateway.

Now test the endpoint by adding values to below keys.

  1. "resource": "/v1/greet/rahul"
  2. "path": "/v1/greet/rahul"
  3. "httpMethod": "GET"

Once configured, click on Test button, We get the below greeting for the name passed into the function.

serverless-sample-output

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay