DEV Community

Benjamin Ajewole
Benjamin Ajewole

Posted on • Updated on

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

Image for post

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 installed on your local machine,
  • you also have an AWS account,
  • you also have to create an IAM user 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

Install Serverless framework globally using npm

npm install -g serverless
Enter fullscreen mode Exit fullscreen mode

Configure serverless AWS credentials

serverless config credentials --provider aws --key ***TR3UCUNUZ4RMQ --secret ***PmuaJu1hXF4WA1JfG1sx9+7/+kKg/a
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)

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
Enter fullscreen mode Exit fullscreen mode

Deploy the function to AWS

sls deploy
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Latest comments (0)