Serverless Framework

Build scalable and cost effective solutions faster

Anuj Kumar
kodeyoga
6 min readJul 28, 2021

--

Photo by Carl Heyerdahl on Unsplash

Most of us would have come across some or the other component of serverless architecture by now. There are many reasons for serverless architecture becoming very popular so rapidly these days, we will go through these reasons briefly below.

Serverless Framework is an open source tool available for building, packaging and deploying serverless applications across multiple cloud providers and platforms like AWS, GCP, Azure, Kubernetes, etc. Let’s go through some basics first.

Serverless Architecture

Serverless Architecture is a design pattern where in the infrastructure required for running the application is initialized and managed by a third party provider on demand. Compute resources are used only when there is a need for application logic execution, it saves a lot of compute power and cost.

For more details around need, benefits and use-cases, please go through the blog here.

Is it really Serverless?

People new to this concept, always has this question in their mind, and it is a very fundamental question as all of us know that a server is required for hosting and running any application.

It is called serverless because it does not have any dedicated servers always up and running to host the application, i.e. it is serverless only when the application is idle. As soon as a request or an event needs the application to be running it is deployed on a server and the required action is taken by the application.

We can think of a serverless application as an auto-scaling application where in the initial state or minimum configuration is set to use zero compute resources.

Serverless Computing Manifesto

Below are some of the guiding principles for anyone looking to start with serverless pattern.

  • Function are the unit of deployment and scaling.
  • No machines, VMs, or containers visible in the programming model.
  • Permanent storage lives elsewhere.
  • Scales per request; Users cannot over- or under-provision capacity.
  • Never pay for idle (no cold servers/containers or their costs).
  • Implicitly fault-tolerant because functions can run anywhere.
  • BYOC — Bring Your Own Code.
  • Metrics and logging are a universal right.

The need for a framework

As it happens with any technology, with the evolution arises a need for new tools and frameworks to simplify it’s usage and increase it’s adoption by the community. We need a framework as we do not want to get drowned in the details of provisioning and managing all the resources required for a serverless applications on multiple cloud providers available today. We should channelize our energy on developing the business logic and let the other supporting infrastructure related stuff abstracted and handled by something else like a managed service.

Serverless Framework to the rescue

Serverless Framework is exactly what we need to empower developers to build and deliver high quality applications easily and quickly, without worrying too much about configurations and resources provisioning on different Cloud Provider. It supports all the major Cloud Providers and platforms like AWS, GCP, Azure, Kubernetes, etc.

Let’s learn with a simple example

In the following section we will build a Node.js hello-world serverless application, which on invocation will return a json response having Hello message along with an appended string at end.

It would create docker based AWS lambda function, the image will be deployed in response to an event and the handler function will be invoked. Docker type lambda function gives you more flexibility in terms of technology choices, development and testing on local environment.

Prerequisites for this example:

  1. Docker is setup.
  2. Serverless CLI is installed, refer here for details.
  3. You have an AWS account and a AWS profile is setup for the same. If not, you can use this guide to set it up.

You can find the complete code for reference, along with the temporary generated files by serverless framework for this example on GitHub.

Create an application using serverless CLI with the help of one of the templates already available with serverless using following command.

This will create following files,

  1. an app.js file to hold business login in handler method.
  2. a Dockerfile, to package the Node.js app in a docker image.
  3. a serverless.yml, for declaring deployment configuation variables.

Let’s take a look at the serverless.yml file, see inline comments for explaination of each section

We have this basic configuration and application code ready to be packaged and deployed.

From root of our newly created application, run the following command for serverless framework to do it’s magic.

As we have configured, provider to be AWS, serverless generated CloudFormation stack for managing all the required AWS services in .serverless folder. Ideally it should not be checked in, but for reference purposes I have pushed it in our example GitHub repo.

Along with this, the docker image was build using the Dockerfile to package our Node.js application. To publish this image, a new repository is created on ECR with same name as the image and the newly created image version is pushed in the ECR repository.

The CloudFormation templates generated above has resource configurations for all the components required at the runtime by our application, e.g. S3 Buckets, IAM roles, Logging and Lambda Functions.

Once we have all the stuff ready, let’s run below command to deploy and test our application. This step will take some time, so be patient.

On successful completion of above command, we can see AWS console to have following resources created.

  1. CloudFormation stack with its artifacts in S3 bucket.
  2. Lambda Function created with type docker.
  3. Roles and policies
  4. CloudWatch Log Groups for application logs.

We are ready to test the app with following command.

As a developer, I am happy that I do not need to spend more time on AWS resource configuration and deployment stuff, just have some basic understanding of serverless world and spend all your energy on business logic.

This was a very basic example, but we can create a full stack and very sophisticated application as well with ease.

Why not use AWS SAM for above example?

This is a very valid question, AWS SAM can also be used to create same application, with similar declarative style and ease. But I feel following are benefits of using Serverless Framework over AWS SAM even though both work on similar principles when working with AWS provider.

  1. Our solution would be easier to migrate to some other cloud provider (say GCP or Azure) in future.
  2. Serverless CLI commands are very intuitive and easy to learn and use.
  3. Resource declarations are more readable as compared to AWS SAM.

Conclusion:

Serverless computing is evolving very rapidly and it’s adoption is also growing exponentially. It is one of the hottest areas in software development world right now. There are many tools / frameworks already present in market to make development and adaptation of this technology easy.

Serverless Framework is one of the most popular among them, others include Architect (Node, AWS), Chalice (Python, AWS), Claudia.js (Node, AWS), Dawson (Node, AWS), DEEP (Node, AWS), Flogo (Go, AWS), Lambada Framework (Java, AWS), Python-Lambda (Python, AWS), Pulumi (Node/Python/Go, AWS/Azure/GCP/Kubernetes), Shep (Node, AWS), Sparta (Go, AWS), Spring Cloud Function (Java, AWS/Azure/OpenWhisk), Zappa (Python, AWS) and many more to come.

We, at KodeYoga, provide value consulting in Full stack development, Cloud Computing, Reactive programming following best practices to convert clients’ vision into reality. We have helped our clients to implement DevSecOps practices and adapt DevSecOps culture. Please follow us at Twitter and Linkedin for updates. Feel free to connect with us over email hello@kodeyoga.com for any questions, we will be happy to help.

--

--