What does Serverless really mean and how does one go about it?
Before we start to understand what serverless architecture is and how to make a fully scalable enterprise application we need to understand what a micro services architecture means and how is it different from monolithic architecture.
Microservices architecture or microservice systems lets us build an application as a group/suite of small services, each running its own process and 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.
Adoption rate of Serverless Architecture:
Some of the major tech companies like Netflix,Amazon,Twitter etc have moved from monolithic to micro services architecture.
What does FAAS mean in the serverless space?
Serverless is one of the many ways in which you can make a micro services based application, it is simply put an execution model in which the cloud provider dynamically manages the allocation of machine resources which means scaling will never be an issue.Serverless has ushered in a new era after SASS,PASS,IAAS we have FAAS,ie,Function As A Service.What this means is that serverless lets you use lambda functions(in AWS),cloud function(in GCP),Azure Functions(in Microsoft Azure) and so on as a glue which holds you’re entire cloud infrastructure together.
Serverless lets you create some of the fastest api’s 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.Lets 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 just now 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 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.
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)- -Here’s a quick reference for making sure you have them installed.
3)Serverless Framework ( v1.9.0 or later) - Follow the getting started steps outlined here and you should be up and running in no time.
Lets 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 setup this project.
Step One:Once you have created an AWS account please login into you’re account.Once you login please go to the “My Security Credentials” option under My Account tab.
You would need to generate an AWS access and secret key inorder to access you’re 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(git) repository,then please go ahead and generate git credentials as well and download the .csv file so as to checkout and use the repository locally.
This is all the basic setup you need for this project.
Step Two:Please register a custom domain in you’re account using Route53 AWS component.This is very important because if you havn’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 enndpoint everytime 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 a SSL/TSL certificate for the same using Amazon Certificate Manager(ACM).This is a very important step if you need to configure you’re custom domain with the API Gateway of you’re project.
Step Four:Please use the following steps to configure the generated certificate and custom domain:
- Go to API Gateway component in AWS.
- Select “Custom Domain Names” from the sidebar panel
- Add your Domain Name.
- Select the correct ACM certificate.
- Select Edge Optimized as you’re Endpoint Configuration.
- 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,lets 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 take aways from my serverless experience , the documentation provided on the serverless website is pretty exhaustive and is ideal to setup the project.Please reach out to me if you want any further help.Please find the boiler plate here:https://github.com/bapillai/serverless-aws-boilerplate