Serverless is a cloud-native development model that allows developers to build and run applications without having to manage servers.
While serverless literally means " without servers ", they are always present but abstracted from the development of applications. Thus, the tasks of provisioning, maintenance and scaling of the infrastructure are performed by the cloud provider and the customer can focus his efforts on the development of his code.
What are the main benefits of serverless?
- Productivity: the serverless model helps to improve developer’s productivity as they are not anymore responsible for maintaining the servers, so time spent provisioning and managing servers can be reallocated to their core business: code development.
- Stop guessing capacity : thanks to auto-scaling, resources are automatically adjusted according to your needs at any given moment. This means you don't have to guess at the capacity of your infrastructure and you avoid having unused resources (which allows cost savings) or running out of resources.
- Cost saving: the serverless model runs with pay-as-you-go billing, based on actual code execution time and resources consumed. You are guaranteed to only pay for what you use.
- Green computing: by optimizing the use of resources, the serverless model helps to reduce energy consumption.
- Deploy faster: With serverless, developers don't have to do the backend configuration, so they can deploy their code faster. They also get the flexibility to deploy all or part of the code at once.
While a serverless architecture offers many benefits, it does not suit every type of application and there are a couple of limiting factors to consider, such as:
- Once your architecture is serverless, it can be very difficult to change cloud providers
- When a function is less solicited, a start latency can appear, especially compared to resources executed on dedicated servers.
- Serverless is not suitable for applications with long request processes. Long execution time and deployment of many resources can generate high costs.
A serverless architecture on AWS:
AWS pioneered the serverless model by launching the AWS lambda service in 2015. Lambda is a serverless compute service that allows you to run code in response to events for almost any type of application or backend service. All you need to do is provide your code in one of the supported languages (Node.js, Python, Java, Go, .Net, Ruby).
Today AWS offers a dozen serverless services, called managed services, on 3 layers: compute, integration and data storage. Below is an example of a full serverless architecture on AWS for a simple web application.
Here, several AWS serverless services are used. First of all, there is Route53 which allows the management of DNS. Then there is CloudFront which acts as a CDN and therefore allows the caching of certain resources. It is linked to the Certificate Manager service, which ensures the installation of SSL/TLS certificates on the domain name. Then we have API Gateway, which acts as the API that connects our user with the application. Combined with the Cognito service, it is able to reject the request if the user is not authenticated or authorized. Otherwise, the request follows its course. It can either go to the S3 service, if the data to be retrieved is static (like an image for example), or to the Lambda service which will act as the back-end of our application. In the last case, our back-end will be able to communicate with a DynamoDB database, to retrieve information related to the user.
All the services used here are serverless, and work perfectly without the need to set up a server. So, if no requests are made, it is possible to not spend a penny. And conversely, if the load were to burst, our infrastructure would be able to scale without any problem.
Bonus: How to build an API Gateway?
Steps to build an API Gateway:
- Create an AWS Lambda
a.Log into your AWS console, and go to the Lambda service
b. Click on "Create function".
c. In the form, fill in the following items:
i. Function name: "my-first-lambda
ii. Runtime: Python 3.9
d. Finally click on "Create function".
2. Create an API Gateway
a. Go to the API Gateway service
b. Click on "Create API
c. Choose HTTP Api and click on "Build
d. Click on "Add integration" and select Lambda
e. In the search bar that appears, select "my-first-lambda
f. In "API name" enter "my-first-api" and click "Review and create", then "Create"
3. Send a request to the API
a. Copy the url present under the name "Invoke URL", and add "/my-first-lambda" at the end.
b. Paste this url in your browser to get the answer from your Lambda : "Hello from Lambda!"
You've just deployed and used an API in just a few clicks!