What Is Serverless Framework, Build Serverless API With AWS Lambda?

Harsha Liyanarachchi
Geek Culture
Published in
5 min readJun 8, 2020

What is serverless?

Serverless is about abstracting away all your infrastructure needs as a simple configuration that gets deployed for you. Simply it allows users to write and deploy backend services without worrying about the underlying infrastructure. Even if we call as serverless still used physical servers but programmers don’t need to worry about those servers. Infrastructure isn’t your responsibility. Scaling and performance are handled automatically, and you are billed only for the exact resources you use

Cloud providers like AWS, Azure, and Google Cloud are responsible for the provisioning of infrastructure to execute the piece of code. This written code sent to the cloud provider to run is in the form of a function that is commonly known as Function as a service (FaaS).so chargers are calculated for the number of resources used to execute the code (only pay for the processing time used by each function as it executes).

AWS serverless overview

Microsoft Azure serverless overview

Google Cloud serverless overview

Without going into more details on serverless architecture. Let focus on today’s topic what is the serverless framework and what kind of work we can do using that.

What is Serverless Framework?

Another open-source web framework written using Node.js enables us to build scalable applications without worrying about provisioning and managing infrastructure. which will help to get rid of scaling servers, provisioning of resources, etc. Initially, it supports only to build the application on AWS Lambda but the current serverless framework able to build and deploy applications in many other cloud providers which supports function as a service, few of them are,

etc.

Here are all serverless infrastructure providers

Let’s Implement a simple serverless API with AWS Lambda using Python

As the very first step, we need to install Node.js to the local machine because the serverless is node.js CLI tool. Can use node.js version check command to verify the installation.

node -v

Note: Serverless runs on Node v6 or higher

The next step in the line is, install the serverless framework. Type following npm command in cmd.

npm install -g serverless

you can verify it using the following command,

serverless -v

Now we need to configure the AWS account to provision and manage the resources on behalf of you.

  • Log in to the AWS account(If you don’t have an account, create an AWS account)
  • Next will create an IAM user and access role to secure the service deployment on AWS by enabling the serverless framework to issue temporary access keys to deploy services in AWS. (In this post, I’m using my default AWS profile)
serverless config credentials --provider aws --key <AccessKey> --secret <secretKey> --profile <profile name>

Once it is successful, we can see an entry in the credential file in the .aws folder in the local machine.

Let’s create a simple backend service using serverless now.

use the following command in cmd will create a service.

serverless create --template aws-python --path backend

Using the above command we can create python service. There are many templates available to create services in the serverless framework. check here.

If you can’t remember the commands, type serverless in the console like below image and we can create service.

This will create 2 files in the project directory.

Default serverless.yml looks like below

Default handler.py looks like

handler.py

Let’s modify these 2 files to create a simple API which we can use to add two numeric values.

As the first step, we should modify the serverless.yml to dec

  • Defined the name of the service: backend services (This should be a unique service name for a particular serverless account).
  • To monitor the serverless project in the serverless dashboard(dashboard.serverless.com), we can mention the app and org.
  • As next, we should mention the configuration of the cloud provider. In our case, we are using AWS as our cloud service provider. (Should need to config the AWS configurations)
  • In the last part, we include the lambda function. In our case, we hoping to define a POST API.

For more information refer here.

Let’s see modified serverless.yml with all the necessary configurations for the POST API.

Now we should implement the service in the handler.py.

Let’s write a simple python function to add two numeric values and return the JSON response.

Now all the implementation and configuration are done

Let’s deploy this using serverless command

serverless deploy

once you execute this command it will be provisioning all the necessary resources in AWS and deploy our code.

The successful deployment will return with the endpoint URL.

Let’s try out our simple API created using a serverless framework

Now it is your turn to try out…………..

**Note: Please check AWS CloudFormation, there you can see all AWS resources provisioned for a particular serverless project.

Please feel free to post your questions here….!!!!!!!!!!!!!

--

--

Harsha Liyanarachchi
Geek Culture

Full Stack Engineer | Java | Spring Boot | Kotlin | React | Ruby | AWS | OCI | Cypress | CI/CD | Agile