To begin with, what is a server?
A server is essentially a computer that runs continuously to deliver services. It is intended to offer client computers or devices services including data storage, application hosting, file sharing, and communication services, and often runs continuously 24/7.
This is what a single server looks like...
commonly used in data centers, server rooms, and other enterprise environments. It is designed to provide reliable, high-performance computing power for a variety of applications, including web hosting, virtualization, data analytics, and cloud computing.
Refer to my previous blog for a better understanding of servers and the web.
What is Serverless, then?
The name, "serverless" does not mean that there are no servers involved. In fact, there are still servers running in the background, but the difference is that the developer does not have to worry about managing or maintaining those servers.
In a serverless architecture, the cloud provider is responsible for managing the servers and infrastructure, allowing developers to focus solely on writing and deploying code. Instead of managing the server infrastructure.
This makes it easier and more cost-effective for developers to build and deploy applications, as they do not have to worry about server maintenance, scaling, and other infrastructure concerns. No Worries :)
In order to be serverless, you must rely on the cloud provider.
Cloud Provider
There are three major cloud service providers.
Amazon Web Services (AWS)
Azure
Google Cloud Platform (GCP)
I decided to go with AWS for this blog. You can go with any cloud.
There are around 200+ services offered by Amazon Web Services.
A few of them are listed below.
Any guesses as to the service used for serverless computing?
True, It's aws Lambda.
AWS Lambda
AWS Lambda, the serverless computing service offered by Amazon Web Services (AWS), is a key player in the serverless landscape. In this blog post, we will discuss what serverless architecture is and how AWS Lambda can be used to build and deploy serverless applications.
You can write your code in various programming languages like Python, Node.js, Java, C#, and Go.
Lambda supports event-driven computing, meaning that the function only runs when it is triggered by an event such as an API call, a file upload, or a message in a queue.
Lambda allows you to build and deploy serverless applications without the need to worry about the underlying infrastructure. AWS manages the servers, scaling, and high availability of the Lambda functions.
This means that you only pay for the compute time that your code uses, and you do not have to pay for idle time. This makes AWS Lambda a cost-effective solution for running small to medium-sized workloads.
Let's get our hands dirty
Requirements
Aws Account
Terminal (Ubuntu Recommended)
Serverless Framework (SLS)
Steps
Setup AWS Account Link
Setup AWS CLI Link
Install SLS in terminal Link
type
>$ sls help
to check whether successfully framework is installed or nottype
>$ sls
to create projectAfter creating you will have three files
serverless.yml
The hello()
function is a lambda function handler. It is a method in your function code that processes events. As soon as your function is invoked, Lambda runs your handler method. As you can see in serverless.yml, this is registered with the service as handler.
The first argument is the event object. An event is a JSON-formatted document that contains data for a lambda function to process.
The second argument is the context object. A context object is passed to your function by Lambda at runtime. This object provides methods and properties that provide information about the invocation, function, and runtime environment.
import json
def hello(event, context):
body = {
"message": "Go Serverless v3.0! Your function executed successfully!",
"input": event,
}
return {"statusCode": 200, "body": json.dumps(body)}
Serverless.yml
This is the primary config file for your new project. If you open it up, you should see something like this. If you want to create more endpoints, you have to mention the handler and path in this file.
service: Serverless
frameworkVersion: '3'
provider:
name: aws
runtime: python3.8
functions:
hello:
handler: handler.hello
events:
- http:
path: /hello
method: get
Deploy
Finally, you need to deploy your function:
>$ sls deploy
Output
Voila! The endpoint link will be generated.
You can verify the API by checking the link.
Now switch to the AWS console
Visit AWS Lambda, click the function name
You can see the virtual overview of the function.
Handler.py allows you to create as many lambda functions as you need and configure them in serverless.yml as you need
Alright, those are the basics, but there is always more to learn
Conclusion
AWS Lambda is an excellent serverless computing service that allows developers to build and deploy serverless applications without worrying about the underlying infrastructure. With its pay-as-you-go pricing model and high availability, AWS Lambda is a cost-effective solution for running small to medium-sized workloads. Whether you are building a web application or a backend for a mobile application, AWS Lambda provides a simple and intuitive interface that makes it easy to build, deploy, and manage serverless applications.