Serverless through Serverless

If you are developer and new to serverless, this short article will give you a brief understanding about it. As soon as I heard the term ‘serverless’ I thought how is that possible? How can there be web services without servers? That is impossible 😕. Yeah, that sounds weird, but what actually happening there is a lot more interesting. Let’s start.

Does ‘serverless’ mean that there are no servers?

No way, that is not what serverless is about. But let me put it in this way, if I say someone else is handling all of my server related matters such as hosting servers, scaling them according to the traffic, etc now I only have to think about my service’s functionality. How does that sound? Yeah that’s what serverless is about. You have to build your service’s functionality while serverless service providers are there to handle your server’s requirements. Serverless allows us to focus on business logic rather than infrastructure.

##Is ‘serverless’ an architecture or a framework?

In the initial stage it was bit confusing for me to identify the difference between the serverless architecture and the framework. Let’s see the difference between the two concepts.

Serverless architecture

Instead of maintaining our own servers we are relying on the servers which are provided by third party vendors. (This is also known as ‘Backend as a Service (BaaS)’ ). Third party vendors provide backend as a services where we only have to consider about our service’s functionality.

Serverless framework

Now there are many popular serverless service providers out there. Each of these vendors provide their own way to setup and get the functions run on their platform. We only have to deploy our functions in their service while they are handling all the server side configurations. Few of those providers are as follows.

  • AWS Lambda Functions
  • Microsoft Azure Cloud Functions
  • Google Cloud Functions
  • IBM OpenWhisk

In order to get their services we should follow their infrastructure specific configurations. Serverless framework came to field to abstract all the popular service providers into one single platform. Since serverless framework handles vendor specific configurations under the hood, it allows us to develop our functions on common platform.

Let’s build a simple REST API on AWS Lambda functions using serverless framework.

How to get started on serverless framework?

Serverless framework can be installed globally by using the following command.

npm install serverless -g

Then you can use serverless framework with serverless / sls prefixes in CLI.

Prior to use Lambda on serverless, you should have an AWS account. Then you need to give programmatic access to AWS account with Admin access through “IAM” user. So serverless framework can access your AWS services. To get the required access you can follow the steps below.

Go to IAM on AWS, then add a user.

Areas

Then check the Programmatic access under Access type, and give the AdministratorAccess under existing policies.

Areas

After creating IAM user, run the following command using Access key and Secret key that you just created.

sls config credentials --provider aws --key <key> -- secret <secret>

Now you can start coding. Since the framework provides a template for basic structure, you could simply use following command for a boilerplate. (In here we are using Node.js, other than that you can use Python, Java, etc)

sls create --template aws-nodejs --path <Service-name>

You can give any project name for . This will be discussed later.

This will create following default handler.js and serverless.yml files.

Your lambda function’s business logic resides on this handler.js.

serverless.yml is allowing us to configure infrastructure resources that are required. There are few key things you should know about serverless.yml when it comes to creating an API. Those are as follows. Those are discussed here with regard to AWS Lambda.

Services

Services are like projects. We declare our functions and any infrastructure that are required under those services. Such as events that triggers particular functions.

Functions

In AWS all the functions that resides on a service are lambda functions. Those function’s configurations are implemented in serverless.yml .

Events

Operations that trigger the functions. In AWS there are multiple ways to trigger the functions such as code pipelines, S3 bucket upload, API gateway endpoints.

You only need above three to make a simple REST API. Other than that there are so much you can do using serverless. Check the documentation for the detailed view.

In here we define the service, provider details and our function details in functions . Under functions there can be any number of function declarations. Each of those function should have lambda function implementation under handler.js. In here we are using ‘hello’ function. As I told earlier, in events we are defining what are the triggering mechanisms to run the functions. Since we are creating an API, a http event has been used. This http event will use the AWS’s API gateway to create an endpoint. In that http object, function’s routing path and the method of the REST API can be configured. In here we have used base path as hello/welcome and the get method.

Simple handler function is used to get the name from query parameters and send Hello, ${user} response back to the client.

That’s what you have to do in order to create a simple API. Now you can deploy the written API to AWS Lambda using following command.

sls deploy

Areas

This will deploy the Lambda function and give you the API endpoint that you configured in http events. You can simply test the API that you created using Postman.

Areas

Serverless framework give us freedom to do our work independently from the vendor. If you have to work with different serverless vendors simultaneously, this framework will help you to get those works done easily.

Learn more about serverless framework from here.

Ashantha Lahiru

Ashantha Lahiru

Engineer who is passionates about technology.