Create a Serverless backend with AWS Lambda Function and Amazon API Gateway

Serverless backend may also be called Backend as a Service (Baas) and Function as a Service(Faas).

According to Wikipedia, Serverless computing is a cloud computing execution model in which the cloud provider allocates machine resources on-demand, taking care of the servers on behalf of their customers.

Pros of a Serverless architecture

  1. Serverless is inherently scalable, it will be able to handle an unusually high number of requests.

Cons of a Serverless architecture

  1. Serverless is not built for long-running processing, it has a time frame. For instance, AWS Lambda timeout after 15 minutes.

I will be creating a Serverless backend endpoint that returns information about the client’s OS information with the following tools:

  • Serverless framework — a more general-purpose tool for deploying and managing serverless application

To get started:

  • You must already have Nodejs installed on your local machine,

Install Serverless framework globally using npm

npm install -g serverless

Configure serverless AWS credentials

serverless config credentials --provider aws --key AKJAPB7TR3UCUNUZ4RMQ --secret vkUtwtehxePmuaJu1hXF4WA1JfG1sx9+7/+kKg/a

Replace the key and secret you copied from your AWS console. You can use serverless with different providers like Azure, AWS, GCP etc.

Create a serverless project using the AWS Nodejs template

serverless create --template aws-nodejs --path clientOs

The above command will create a folder called clientOs and inside this folder, you’ll see the following files/folders:

  • .serverless folder (This folder contains generated CloudFormation file and zipped bundle, the folder gets regenerated every time you run serverless deploy

I updated the handler.js to get client’s operating system information. All I did was to import os from Nodejs library and call different methods like hostname(), platform() etc.

I also updated the serverless.yml with the new function we just created. I added getOsInfo function.

Test the function locally

sls invoke local -f getOsInfo

Deploy the function to AWS

sls deploy

You can login into your AWS console, search for Lambda, under functions, you’ll see your newly deployed function

Amazon API Gateway

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the “front door” for applications to access data, business logic, or functionality from your backend services. Using API Gateway, you can create RESTful APIs and WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and serverless workloads, as well as web applications.

I am going to create an API endpoint for the getOsInfo function using AmazonAPI Gateway. It’s quite easy with serverless framework, all you need to do is to add some lines of code in serverless.yml and deploy

events:  - http:      path: client/os      method: get

We add events with the type http, then the route and the API method which is get.

Your updated serverless.yml should look like this.

Deploy the function to AWS

sls deploy

Check your console, you should see something like this

endpoints:
GET - https://shs8k2mrea.execute-api.us-east-1.amazonaws.com/dev/client/os

You can copy the URL and test it on your browser or Postman or whichever tool you prefer.

Summary

  1. We learnt the definition of Serverless computing

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