Create a Serverless backend with AWS Lambda Function, Amazon API Gateway and Serverless Framework
Published: 2021-02-10 23:06:49

---
title: Create a Serverless backend with AWS Lambda Function, Amazon API Gateway and Serverless Framework
published: true
description: Serverless backend may also be called Backend as a Service (Baas) and Function as a Service(Faas).
tags:
//cover_image: https://miro.medium.com/max/528/1*nlyFQ6_S2z7tooasrCWQHA.png
---

![Image for post](https://miro.medium.com/max/528/1*nlyFQ6_S2z7tooasrCWQHA.png)

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.
2.  Low cost and more efficient because you only pay when your Serverless function is running
3.  Easy to deploy and update
4.  No server management is required

## 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.
2.  It is difficult to debug because you will be dependent on your providers for debugging and monitoring tools
3.  Long-running processing could be more costly on serverless compared to a dedicated server
4.  It may take some time for your serverless backend to handle that first function request, so, you might need to keep it in a running state.

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
*   **AWS Lambda** — a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes
*   **API Gateway** — a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale
*   **Nodejs** — a JavaScript runtime built on Chrome’s V8 JavaScript engine

## To get started:

*   You must already have [Nodejs](https://nodejs.org/en/download/) installed on your local machine,
*   you also have an [AWS account](https://aws.amazon.com/resources/create-account/),
*   you also have to create an [IAM user](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html#id_users_create_console) with programmatic access but for the sake of this article, I’ll be using the root user,
*   copy the access key id and secret access key from your [AWS console](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html)

*Install* [*Serverless*](https://www.serverless.com/) *framework globally using npm*

```
npm install -g serverless
```

*Configure serverless AWS credentials*
```
serverless config credentials --provider aws --key ***TR3UCUNUZ4RMQ --secret ***PmuaJu1hXF4WA1JfG1sx9+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***
*   .gitignore file
*   handler.js file
*   serverless.yml file ( *This file contains serverless deployment configuration*)

{% gist https://gist.github.com/Rexben001/601af0b416dab1776978c6d11b86b53a %}

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.

{% gist https://gist.github.com/Rexben001/0ecb99084ea50a523995df8b0587e1b1 %}

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](https://aws.amazon.com/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.

{% gist https://gist.github.com/Rexben001/4f218df410d6cedf7332497b3568fd54 %}

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/](https://shs8k2ryia.execute-api.us-east-1.amazonaws.com/dev/say/hello)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
2.  We learnt the pros and cons of Serverless
3.  We learnt how to create a serverless backend with API using serverless framework, AWS Lambda, Amazon API Gateway and Nodjes
4.  We learnt how to test functions locally
5.  We learnt how to deploy functions