Serverless: What is it about and how do you get started?

Bharath Uday
dotStar
Published in
5 min readJul 30, 2018

Before we start to understand what serverless architecture is and how to make a fully scalable enterprise application, we need to understand microservices architecture and how is it different from monolithic architecture.

Microservices architecture or microservices system lets us build an application as a group/suite of small services, each running its own process that can be independently deployed. They can be written in any language and can use any data storage system as long as they fulfill their purpose in the services landscape.

Monolithic applications, on the other hand, are built as a single autonomous unit. Hence, scaling specific sections of the code means scaling the entire application which is often time-consuming and difficult.

Serverless — Function As A Service

Serverless is one of the many ways in which you can make a microservices based application. It is, simply put, an execution model in which the cloud provider dynamically manages the allocation of machine resources. Which means that scaling will never be an issue. Serverless has ushered in a new era after SASS, PASS, IAAS. It is essentially FAASFunction As A Service.

What this means is that serverless lets you use lambda functions(in AWS), cloud functions(in GCP), Azure Functions(in Microsoft Azure) and so on like a glue which holds you’re entire cloud infrastructure together.

Serverless lets you create some of the fastest APIs even though they are stateless by virtue of some techniques like in-memory caching etc.

How does Serverless Work internally?

To understand this, we need to know a little bit about the AWS Lambda lifecycle. Let's consider the hello world example which displays a hello world message on hitting the API endpoint “/hello”.

The first time the endpoint is invoked, AWS will fetch the zipped code from S3, launch a container in the cluster to represent your function, and the event is passed to your handler which in turn returns the required response. After the first invocation is done AWS freezes the lambda instance that was created to prevent it from doing any background activity. The next request will unfreeze the container and pass the new event to your function handler. This container will remain in the cluster, ready to unfreeze and serve requests as long as it doesn’t stay idle for too long. If it stays idle for too long then this container is discarded or purged completely.

This is what happens in the case of successive calls but how does serverless handle concurrent calls?

A single Lambda function container can only serve one invocation at a given point of time, so concurrent requests will trigger AWS to fetch and launch additional containers for the response. These containers will get created and destroyed depending on the traffic.

The Serverless website in itself is pretty informative to start off with, but there might be some issues that you may run into while making an enterprise level application some of which will be covered in this article.

Pre-Requisites:

  1. AWS account - AWS has 12 months of free tiers and plenty of lifetime free tiers.
  2. Node.js and npm installed (v6.5.0 or higher)
  3. Serverless Framework ( v1.9.0 or later) — Follow the getting started steps on the link and you should be up and running in no time.

Let's get started now:

Once you create an AWS account you need to do some initial configuration within the AWS console to use it in your project using AWS CLI and also to interact with the project repository from your local to your code repository in AWS code commit considering you are using the AWS landscape for all intents and purposes. Follow the following steps to set up this project.

Steps:
Step One: Login into your AWS account. Navigate go to the “My Security Credentials” option under My Account tab.

You would need to generate an AWS access and secret key in order to access your AWS console through AWS-CLI. This can be done by expanding the access keys tab within the security credentials option as shown below:

If you intend to use EC2 instances then it is always advisable to generate CloudFront Key pairs for using signed URL’s.

If you intend to use the AWS codecommit repository, then generate your git credentials and download the .csv file so as to check out and use the repository locally.

This is all the basic setup you need for this project.

Step Two: Register a custom domain in you’re account using Route53 AWS component. This is very important because if you haven't configured a registered domain then serverless creates one for you dynamically and configures you’re API Gateway to this dynamic URL. You can skip this step if you don’t mind the change in the HTTP endpoint every time you do an “SLS deploy”. Route 53 will also help you manage multi-region,multi-stage rollouts

Step Three: Once you have a registered domain please go ahead and generate an SSL/TSL certificate for the same using Amazon Certificate Manager(ACM).This is a very important step if you need to configure you’re a custom domain with the API Gateway of your project.

Step Four: Please use the following steps to configure the generated certificate and custom domain:

  1. Go to API Gateway component in AWS.
  2. Select “Custom Domain Names” from the sidebar panel
  3. Add your Domain Name.
  4. Select the correct ACM certificate.
  5. Select Edge Optimized as you’re Endpoint Configuration.
  6. Do not use the base path mappings here if you have already configured the base path inside you’re serverless.yml file.

Step Five: The most important step of this project is configuring the database. For the sake of this example, let's consider the non-relational DB from AWS,ie, DynamoDB.Serverless advertises configuring the database schema and its permissions within serverless.yml which can make your serverless configuration file super heavy and cluttered. Use one of the popular data mappers available to define the schema. I prefer: https://github.com/ryanfitz/vogels

A combination of vogels with npm joi will be ideal as it takes of schema creation and validation as well.

These were some of the key takeaways from my serverless experience, the documentation provided on the serverless website is pretty exhaustive and is ideal to set up the project. Please reach out to me if you want any further help. Please find the boilerplate here: https://github.com/bapillai/serverless-aws-boilerplate

The adoption rate of Serverless Architecture:

Some of the major tech companies like Netflix, Amazon, Twitter, etc have moved from monolithic to microservices architecture.

--

--