Serverless frameworks helps you develop and deploy your functions to the cloud. In this blog, I will focus on deploying functions to AWS lambda but serverless also supports:
A serverless framework lets you run code without provisioning or managing servers. It’s an event driven computing platform and that means it runs your code when it is triggered.
Your piece of code is called a function and it should only be responsible for a single task. For example, it can resize images when you upload them to Amazon S3. You might think that that is not very interesting. After all, you could spin up a virtual machine let it do the same task.
Serverless frameworks shine on two fronts: scalability and pricing.
Let’s talk about Amazon AWS Lambda. Amazon charges you only when your code is being executed and bills you per one-hundred milliseconds of use that means you don’t need to pay if your code doesn’t run.
That is a massive saving compared to regular virtual machines where you have to pay per hour that you use it regardless of how much stuff you run on it.
Amazon is also offering a free tear for lamb that every month you’ll get one-million requests and three million seconds of computer time free.
Second advantage is scalability. Coming back to AWS Lambda, with Lambda, you literally upload your code and let Amazon handle all of the scaling for you.
Amazon spins up a container every time your code is being tricked if that container get swamped by requests it simply creates a second container and balances the load across them.
When you don’t receive requests anymore the containers are simply destroyed.
Now Amazon can achieve the scalability because Lambda functions are stateless. You can’t store data in your Lambda functions for long periods of time. For that, you have to use another service like DynamoDB.
Those benefits make lambda highly attractive for people building applications. Ever since Amazon allows you to connect API gateway to Lambda functions, companies have been building complete back as for the apps and websites with this.
Some people call this a serverless computing platform but servers are still involved. It just refers to the fact that you don’t have to handle provisioning securing and managing of servers.
You can use lamb that to build a web API to do that we’re going to use the server list framework.
The five main concepts of serverless frameworks that you should understand when you want to build a scalable and price efficient API with AWS lambda:
In this case, it refers to an AWS lambda function. It’s an independent unit of deployment just like a micro service. It’s just a single package of code that is deployed to the cloud and is often responsible for performing a single task for example, saving a user to a database.
On AWS, you can run your functions in the following languages:
The serverless framework supports all of these languages but in this blog, I will use Node.js. You can program more functionality into a function but that is not recommended. The best practice is to keep your function small and, therefore, reliable. Serverless frameworks makes it easy to manage and deploy a lot of functions so you don’t have to worry about deploying hundreds of them.
Events are anything that can trigger your function in AWS. Many services can trigger an event. For example, you could trigger an event after a new file was uploaded to an S3 bucket or you might trigger an event when someone calls your API. when you define an event in the service framework it will automatically create the necessary infrastructure.
Say you want your functions to be triggered by an HTTP call that serverless willdeploy an API gateway endpoint and will configure your function to listen to these events.
There is also the notion of resources in the surveillance configuration file you can define all the AWS resources that are used by your function.
That might be things like an s3bucket for storage or some DynamoDB tables. When you deploy your application the serverless framework will not only deploy your functions but it will also provision their sources that you have defined.
This is very interesting because it’s an automated process and because it is repeatable you can redeploy your entire application that might contain hundreds of functions and many resources to another AWS account with a single command.
The framework as a unit of organization uses a service. It’s kind of like a project file defining all functions events and resources that go along with it. All of this is saved in the serverless.yamo file and can be deployed at once.
You can build an application with one service or you can have multiple services for a single application.
These can overwrite or extend the functionality of the framework you can install many plugins in your service such as Browserify to optimize your JavaScript code or a plug-in that allows you to use Typescript instead of JavaScript
A lot of burden has been lifted from our shoulders by outsourcing the management and maintenance of the infrastructure, the platform, or even higher levels of the staff. There are still some burdens like servers, the logic and everything from creating server-side code that perform a lot of functionalities that are not related to the business like routing, security, authentication and authorization and of course maintaining and debugging the server side of the application are still done by developers.
Serverless framework unburdens us from building and managing the server side of our application. Serverless is building software by focusing more on business logic without thinking about how you are going to serve your software like there is no server, just business logic.