From Code to Cloud: Examining Serverless Computing Using AWS Lambda
From Code to Cloud: AWS Lambda.

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.

  • Amazon API Gateway

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.

  • Amazon S3

We can triggers Amazon S3 to invoke Lambda when an object is uploaded, deleted, or modified in an S3 bucket.

  • CloudWatch Events

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)

Comparison between Server-based (Express.js) and Serverless (AWS Lambda)
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

  • Go to AWS Management Console and sign in with your AWS credentials.
  • If you don’t have an AWS account, create one.

( You can use AWS Free Tier. )

AWS Management Console
AWS Management Console.

Step 2: Open AWS Lambda Service

  • Click on the link after typing "Lambda" into the search field of the AWS Management Console.

AWS Lambda
AWS Lambda.

Step 3: Create a New Lambda Function

  • Once you're in the Lambda dashboard, click on the Create function.

Creating new Lambda function
Creating a new Lambda function.

Step 4: Select the Function Configuration

  • Function Name: Give your Lambda function a name, such as HelloWorldFunction.
  • Runtime: Choose the function's runtime. For a Node.js function, for instance, select Node.js 20.x.
  • Click Create function after choosing the runtime and entering the function name.

The created Lambda function
The created Lambda function.

Step 5: Write the Lambda Function Code

  • The Function Code section will be displayed after the function has been created.
  • For a simple "Hello from Lambda!" function created for you.

( 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;
};        
Lambda function code creation.
Lambda function code creation.

( 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

  • First, click the Test button or Ctrl+Shipt+I to create a test environment. Then click Create new test event in the popup.
  • And then fill in the necessary field to create the test environment.

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 save the test environment and then click the Test button or Ctrl+Shipt+I to run the function.

Then you should see, like following the response after running the lambda function successfully.

Response:
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}        
Expected Response
Expected Response.

Step 7: Monitor Lambda Execution

AWS Lambda automatically logs execution details to CloudWatch Logs.

  • You can view logs by going to the Monitoring tab in the Lambda function dashboard and clicking on View logs in CloudWatch.

Cloud watch logs for lambda function
CloudWatch logs for lambda function.

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.

Amazon API Gateway
Amazon API Gateway.

Step 2: Create a New API

  • Then choose the API type from HTTP API, WebSocket API, REST API, or REST API Private.

Here I will go with the REST API.

  • Click the build button to build new REST API or click import to import files from your computer.


Create new REST API in AWS API gateway
Create a new REST API in the AWS API gateway.

  • Fill in the necessary field on the page. Under the Endpoint Type field, select Regional if your API does not require a Virtual Private Cloud (VPC) setup.

( This option ensures the API is deployed in the selected AWS region and is accessible directly without the need for private networking. )

  • After clicking the save button, you will navigate to the below page.

AWS Resource details
AWS Resource details.

  • Then click the Create Method button to create a method.
  • Then choose the Lambda function option.
  • After that, choose the method (GET, POST,...) and lambda function in the dropdown provided. And fill in other necessary fields on the page.
  • Click the Create button.
  • Then you should navigate to the below page.

Resource overview.
Resource overview.

  • Deploy the API.

Stages for the API.
Stages for the API.

Step 3: Test the API Endpoint

  • Use tools like Postman, cURL, or a browser to send an HTTP request to the Invoke URL.

Test in the postman.
Test in the postman.

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


Pawara Munasinghe

Business Analyst | Product & Project Management

1 个月

Way to go.. ??

要查看或添加评论,请登录

社区洞察

其他会员也浏览了