Serverless computing, or simply “serverless”, is a very popular approach of creating web applications nowadays. “Pillars of the web” market - Google, Amazon and Microsoft are investing heavily into serverless because it provides new possibilities for their clients. Building serverless application benefits from significantly reduced operational cost, complexity, and engineering lead time. Let’s now define what does “Serverless application” mean.

What is serverless application?

Serverless application - is an application which partially or fully rely on third-party, cloud-hosted applications and services. These services are managing server-side logic and state. The huge portion of modern applications are now using third-party services for their databases, such as Parse and Firebase, and authentication (e.g., Auth0, AWS Cognito). But it can go much further - developers now can host their business logic in the cloud. This is called Function-as-a-service, or FaaS. So there is no dedicated server to host the functions. It’s all done by the cloud-hosting provider. AWS Lambda is one of the most popular implementations of a Functions-as-a-Service platform at present, but there are many others, too. Here you have picture where you can see difference between more traditional hosting approaches against FaaS

difference between traditional hosting approaches against FaaS

Here you can see that all infrastructure is done by the provider. You don’t need to think about operating system, server costs, server-specific code. You can fully focus on the business logic and features for your application. Here one can see major advantages of serverless

advantages of serverless computing

Now let’s take a deeper look on how to implement serverless backend for your or your client application.

Implementation of serverless solution on AWS

First of all, serverless application is the same for client as server-“present”. So we need a physical endpoint. In AWS, API Gateway service is responsible for routing. You can create an endpoint from the console or use AWS cloudformation service to make this for you. 

Next, you need to upload your function into the cloud. For that purpose AWS Lambda service is created. You upload your function there and go back to API Gateway. Attach your function to the endpoint, deploy the API and its live. You can now call your endpoint. Simple, right?

Not really. When developing large system, deployment of the new functions, APIs and their configuration will become huge pain in the neck. To automate this process AWS cloudformation can be used, but it’s quite complicated for the beginner to understand and start the coding process.

To simplify the process of deployment the Serverless framework is your best friend.

Serverless framework

Little note here. We want now define the term “microservice” as a set of lambda functions, which will be deployed on AWS.

Serverless framework is a wrapper around cloudformation template engine which allows you to deploy microservices using much less deployment code that in case of pure cloudformation template. Let’s start by installing it

npm install -g serverless

After installation, run next command in your project folder

serverless

This creates “serverless.yaml” file. In this file, you describe your microservice configuration in the cloud. 

Inside of your project you need to have “serverless.yaml” file in which you describe your microservice configuration in the cloud. Let’s take a look on example

serverless yaml file

Here we have a simple service with one endpoint “store”, which should return a list of stores (of course, this depends on the lambda function implementation). Let’s go line by line:

  • service: it’s the name of the service
  • provider: the place where we put provide-specific information
  • name: name of the cloud provider
  • runtime: runtime under which lambda functions are going to be executed
  • stage: stage under which microservice will be deployed. It’s need when during the development we have multiple environments (test, stage, production etc). This “${opt:stage,'dev'}” means that the value should be taken from “stage” CMD argument or 'dev'. I’ll show this when we reach the deployment phase.
  • iamRoleStatements: here we can define IAM permissions for lambda functions like access to S3 buckets, SQS, SNS etc. Here I’ve provided access to AWS Secrets manager can store some sensitive information such as db credentials.
  • environment: here you can define environment variables with which your functions are going to be executed.
  • functions: a list of functions of the microservice
  • GetStores: is the name of the function in AWS environment
  • handler: path to the lambda function code in your project
  • events: list of events which will trigger the function. Here we defined the “http” event which means that it will create endpoint in AWS API Gateway service and bind the lambda function to it.
  • path: url path
  • method: http method
  • integration: you select whether you use lambda-proxy integration or not
  • cors: allow “cors”-requests

Now we need to write lambda function. It can be something like this

lambda function

Next, we need to deploy it. To do this you need first to set your AWS credentials

export AWS_ACCESS_KEY_ID=<your-key-here>

export AWS_SECRET_ACCESS_KEY=<your-secret-key-here>

and 

serverless deploy -stage=<your stage>

or use a shorthand for this

sls deploy -stage=<your stage>

As we mentioned before, you can put your “stage” value here and it will be used in the serverless.yaml file. You can use this to make complex configurations for your cloud environment. Of course, you can omit this argument and it will use default one. Will be good to mention here that the final name of the your lambda function will have the following structure

{Name_of_microservice}-{stage}-{name_of_the_function}

All this info comes from serverless.yaml file.

When deploy is finished, you are free to test your new serverless API and extend it in a way you like.

There are a lot things that can be done from this point. You can add complete CRUD support, cron jobs, SQS event handlers, custom and AWS Cognito authorizers. Also you can create new resources from serverless.yaml like S3 buckets, AWS RDS databases. You can implement routing through a real domain created in AWS Route53 service and a lot more. Just play around and learn the possibilities of this approach

Or if you are an entrepreneur with the great idea which will be useful for a lot of people - don’t hesitate. Contact us and we will help you to achieve your goal. Serverless approach will make your business solution cheap, scalable and reliable.