EXPLORING THE SERVERLESS CHRONICLES -- AWS LAMBDA


Today, we will be discussing AWS Lambda, which is a service provided by AWS. With Lambda, you can run your code without needing to manage any servers. It enables us to execute our code in a serverless environment, meaning we don't have to worry about the underlying infrastructure. Now, let's gain some practical experience by exploring AWS Lambda hands-on.

Prerequisites:

You will need an AWS free-tier account with the necessary permissions. If you haven't created an account yet, please refer to my previous blog for instructions on creating one.
  1. Sign in to your AWS account using your account credentials.
  2. Enter "LAMBDA" into the search bar and press Enter, The search results should display the AWS Lambda service



  3. From the search results, locate and click on the "Lambda" option. This will direct you to the AWS Lambda service page.
  4. Click on the region selector dropdown located at the top-right corner of the console. A list of available AWS regions will be displayed. Select the region you prefer from the list.


  5. In the AWS Lambda console, click on the "Create function" button. Ensure that you have selected the desired region where you want to execute the function.

    After clicking on "Create function", you will be directed to a new screen where you can provide the basic details for function creation

Author from Scratch: This option allows you to create a Lambda function from the ground up using your preferred programming language without any preconfigured code or templates.

Use a Blueprint: This option provides preconfigured Lambda function templates for various use cases, enabling you to start with functional code and customize it to suit your needs.

Container Image: This option lets you package your Lambda function in a container, giving you the flexibility to use any programming language or runtime that can be containerized.

Function name: Enter a descriptive name for your function.

Runtime: Select the runtime environment for your function, such as Node.js, Python, or Java.

Architecture:

x86_64: Intel-compatible 64-bit architecture used in most desktop and server environments.

arm64: ARM-based 64-bit architecture commonly found in mobile devices, embedded systems, and certain server environments.

** As this is our first lambda function do not touch any other things, make its default and click on create function.

Permissions: Configure the required permissions for your function to interact with other AWS resources.

After you have entered all the required details for your function, simply click on the "Create function" button. This will start the process of creating your Lambda function with the provided settings. AWS Lambda will validate the information and set up the function according to your specifications.

Please be patient, as it may take a little time for the function to be created. Once the process is complete, you will be redirected to the function's details page, where you can continue to configure and manage your Lambda function as needed.


As you scroll down, you will find the following screen:


Feel free to customize the body/code as needed. Here, I have written "Welcome to Code Builders Hut" Click on the Deploy button to deploy the changes. Next, click on the test tab and select "configure test environment" to open the test configuration screen. Provide a name for the event, for example, Test1, and click on Save. You will then be able to view the execution results.


Congratulations on successfully executing your first Lambda function in AWS! You can now observe the response, which should display "Welcome to Code Builders Hut" based on the provided code.

This code/function is small and simple, similar to a "Hello World" example, but it serves as a great starting point to understand the basics of AWS Lambda.

Another Sample Example using the Lambda function in AWS

Now Let's proceed with writing a modified code to calculate the addition of two numbers. Here is the updated code snippet:

export const handler = async (event) => {
    // Check if Input1 is provided in the event
    if (!event.input1) {
        return {
            statusCode: 400,
            body: JSON.stringify('Input1 is required.'),
        };
    }
    // Check if Input2 is provided in the event
    if (!event.input2) {
        return {
            statusCode: 400,
            body: JSON.stringify('Input2 is required.'),
        };
    }
    // Retrieve input values from the event
    const input1 = event.input1;
    const input2 = event.input2;
    // Perform addition
    const sum = input1 + input2;
    // Create a response object with the sum
    const response = {
        statusCode: 200,
        body: JSON.stringify(`The sum of ${input1} and ${input2} is ${sum}`),
    };
    return response;
};


During the configuration of the test event, provide the values for input1 and input2 to specify the two numbers for the addition calculation.


After clicking on the Save button, you will be able to view the successful results displayed below.


Yes, you have successfully executed the two Lambda functions. Well done!

Monitor:

To monitor your Lambda function, navigate to the "Monitor" tab. There, you will find various graphs and visualizations that provide insights into the performance and behavior of your function.


Similarly, you can explore many other things as well.

NOTE: ** Clean the resources to avoid billing **


Thank you all for taking the time to read my blog. I appreciate your support and engagement. Stay tuned for more blog posts where I will share hands-on experiences with various other AWS services.

Happy Coding !!

Chandu Patthem

I am a dynamic software professional with a passion for blogging. As a Certified AWS Cloud Practitioner and Cloud Professional, I bring extensive expertise in IT and various technologies including AWS, Java, Spring Boot, Node.js, Postgres, and MongoDB. Alongside my professional journey, I have developed exceptional time management and organizational skills, which are invaluable in my role as a blogger. Through my blog, I strive to provide insightful content, share my experiences, and engage with a wider audience. As a blogger, I am dedicated to delivering valuable information, thought-provoking ideas, and practical solutions. Join me on this exciting journey as we explore the latest trends, industry insights, and innovations together. Thank you for your interest, and I look forward to connecting with fellow bloggers, readers, and enthusiasts who share my passion for technology and knowledge sharing.

Post a Comment

Previous Post Next Post