From Code to Cloud: Examining Serverless Computing Using AWS Lambda
When we are going to talk about serverless computing. We first need to understand what cloud computing is, as serverless computing falls under cloud computing. ( In this article, I will focus on AWS services. )
What is Cloud Computing?
Cloud computing is delivering the computer resources, such as servers (Ec2), storage (S3), databases (DynamoDB), and more, over the internet. These service providers, such as AWS, Azure, or Google Cloud, handle the maintenance of the infrastructure and actual hardware, enabling smooth usage of these services by enterprises.
What is Serverless Computing?
With serverless computing, developers can focus on writing and deploying code without managing the underlying servers. The resources required to operate your apps are automatically provisioned, scaled, and managed by the cloud provider. We can look at it in a simple way,
Ex:
Traditional server (Express)
In this approach, it is required to keep a server running. Requires provisioning and maintaining the server (Ec2). Also, charges are calculated for the uptime, not for the actual usage.
// Example for Express server
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/hello', (req, res) => { res.send('Hello, World!'); });
app.listen(PORT, () => { console.log(`Server is running on https://localhost:${PORT}`); });
Serverless implementation (AWS Lambda)
In this approach, it is required to deploy a function to AWS Lambda and invoke it via API Gateway or CloudWatch Events.
There are several AWS services that can invoke a Lambda function. I will talk about the main three of them.
This service provides a way to expose Lambda functions as RESTful APIs or WebSocket, enabling web and mobile applications to invoke Lambda functions through HTTP requests.
We can triggers Amazon S3 to invoke Lambda when an object is uploaded, deleted, or modified in an S3 bucket.
Lambda functions can be scheduled to run on a regular basis or triggered in reaction to AWS service events using CloudWatch Events.
// Example for Lambda Function
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: 'Hello, World!',
};
return response;
};
In the above examples, we can see clear variations between the two approaches. With Express.js, we need to manage an entire server. This usually costs more depending on the server's availability and requires more work to maintain.
On the other hand, in a serverless environment, such as AWS Lambda, we only need to focus on the specific function we want to execute. These environments automatically scale the function based on demand and charge only for the actual execution time.
Comparison between Server-based (Express.js) and Serverless (AWS Lambda)
How to Create an AWS Lambda Function: A Step-by-Step Guide
Step 1: Sign in to AWS management console
( You can use AWS Free Tier. )
Step 2: Open AWS Lambda Service
Step 3: Create a New Lambda Function
Step 4: Select the Function Configuration
Step 5: Write the Lambda Function Code
( You can write and edit the code in the index.js editor. )
export const handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
( Using the provided inline editor, you can modify the function code. The Upload from menu can be used if you need to upload code from an S3 or ZIP file. )
领英推荐
Step 6: Test the Lambda Function
What is Event JSON?
Your Lambda function's input data is represented by the Event JSON, a structured JSON object, when it is called. You can use the event argument in your Lambda function to get this data.
Then you should see, like following the response after running the lambda function successfully.
Response:
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
Step 7: Monitor Lambda Execution
AWS Lambda automatically logs execution details to CloudWatch Logs.
These methods make it simple to use the AWS Lambda console to construct and test a Lambda function. Following successful testing, Amazon CloudWatch may be used to easily monitor the function's execution and performance logs, offering insightful information and guaranteeing seamless functioning.
Set Up an API Gateway to Trigger the Lambda Function
Amazon API Gateway will be set up to act as a RESTful API endpoint that initiates your Lambda function. This enables external systems or users to interact with your function via a URL by allowing the Lambda function to be invoked via an HTTP request.
Step 1: Open API Gateway Console
You can see this page.
Step 2: Create a New API
Here I will go with the REST API.
( This option ensures the API is deployed in the selected AWS region and is accessible directly without the need for private networking. )
Step 3: Test the API Endpoint
Step 4: Monitor the API with CloudWatch
Conclusion:
With AWS Lambda and API Gateway, you can quickly and easily build serverless apps that are robust, scalable, and affordable. While API Gateway gives you a simple way to make your functions accessible to the public using safe, RESTful APIs, Lambda lets you concentrate entirely on your code. By removing the hassle of infrastructure management, they allow developers to create and release apps more quickly.
Give serverless computing a try now to expand the potential of your applications! Please feel free to ask questions or share your experiences in the comments section.
let's learn together! ??
#CloudComputing #APIGateway #AWSLambda #ServerlessComputing #CloudWatch #AWS
Business Analyst | Product & Project Management
1 个月Way to go.. ??