Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

AWS Lambda: A Hands-On Beginner's Guide

Posted on Sep 28 In this blog post, I'll begin by providing a brief overview of serverless computing. Then, I'll introduce AWS Lambda, an essential component of serverless architecture. I'll explain how Lambda functions operate and explore their various use cases and benefits, as well as their limitations.I'll also offer practical examples of Lambda functions, including:What is Serverless ComputingBefore delving into Serverless computing, let's take a moment to understand server-based computing. In server-based computing, you typically set up your infrastructure and then deploy your code across multiple tiers. In the deployment architecture below, we can visualize a 3-tier system where the frontend code resides on a host, often running a web server. The backend code operates on a separate server, usually an application server, and there's a database server as well. In this setup, developers not only focus on writing code but also need to manage the infrastructure.Now, in Serverless computing, developers can concentrate solely on their development tasks, leaving AWS to handle the heavy lifting associated with infrastructure management. Using the same examples as before, both frontend and backend codes are deployed as Lambda functions. AWS RDS, a managed database service, is utilized to store data. In this scenario, developers can primarily focus on writing code rather than designing, implementing, and maintaining the infrastructure.Server-based vs Serverless computingServer-based computing involves managing infrastructure tiers, while serverless computing allows developers to focus solely on code without infrastructure concerns.Server-based:Serverless:What is AWS Lambda?AWS Lambda, as a serverless compute service, empowers developers to execute code without the hassle of server provisioning. Here are some key aspects of AWS Lambda:Pros and CornsJust like anything else in life, AWS Lambda comes with its own set of advantages and disadvantages. Here are a few considerations to keep in mind:Pros:Corns:Use casesAWS Lambda presents a multitude of diverse use cases, making it incredibly valuable for anyone looking to explore the world of serverless computing. Here are a few noteworthy ones you wouldn't want to overlook:If you're reading this now, I'm sure you're eager to dive into more advanced topics. So, let's roll up our sleeves and delve into a few Lambda examples to get hands-on experience.Getting started with AWS LambdaTo get started with AWS Lambda, log in to your AWS account and access the AWS Lambda dashboard. You'll find two main components:For today's focus, I'll delve into AWS Lambda functions. Now, let's dive in and explore with some practical examples.Creating a basic "Hello World" Lambda function.Let's dive into our first exampleStep 1: Create a FunctionClick on "Create Function" in the AWS Lambda dashboard.Step 2: Choose Author From ScratchYou'll be presented with three options:Select "Author from Scratch."Step 3: Provide Basic InformationFill in the following details:Function Name: Hello-World-LambdaRuntime: Select "Python 3.11" (AWS Lambda supports multiple runtimes like .NET, Go, Java, Node.js, and Ruby).Keep the other default values and click on "Create Function."Step 4: Function CreatedCongratulations, you've successfully created your first Lambda function!Step 5: Add Code to the FunctionBy default, AWS Lambda provides you with a basic code template. You'll see the following Python code:Step 6: Test the FunctionTo test the Lambda function, click on "Test." This will mimic an event to trigger your function. Then, click "Invoke."Step 7: View the OutputCongratulations! You'll see the following output:ResponseYou've successfully created and tested your "Hello World" AWS Lambda function. This is just the beginning of what you can achieve with AWS Lambda for serverless computing.Understanding the lambda_handler FunctionIn AWS Lambda, think of the lambda_handler function as the main starting point for your code. When an event triggers your Lambda function, this function is automatically called.It's somewhat like a detective gathering clues. The event parameter holds information about what caused your Lambda function to activate. It's your way of figuring out what's happening.The context parameter is like a toolbox with tools for the job. It provides details such as how much memory you can use and how long your function can work.In essence, lambda_handler is your function's manager, examining incoming events and using the tools from the context to get the job done.Establishing a connection between Lambda and an RDS database.Let's establish a connection between AWS Lambda and an RDS database, which promises to be an intriguing example. If you don't already have an RDS test database, you can create one to test this out. Now, let's take a look at the Lambda code to make this connection.Step 1 : Begin by creating the function.Step 2 :Copy the provided code.Step 3 :Click the "Deploy" button.Step 4 :Now, it's time to test the code. However, you might encounter an error:Error Message:Let's package this program and configure the runtime correctly to resolve this issue.Logging in to an EC2 and Ensuring Required Permissions are there.Before proceeding, make sure you are logged in to your EC2 instance. Ensure you have the necessary permissions to perform tasks. If you encounter any issues, address permission concerns accordingly.Step 1: Create a Directory Named 'Lambda-Connect_RDS'Step 2: Navigate into the created directory and copy your Lambda code, naming the file 'Lambda-Connect_RDS.py'.Step 3: Now, let's install the 'pymysql' Python library. Execute the following command:Step 4: Create a Zip File Comprising Your Python Code and Runtime Libraries:Step 5: With the zip file ready, you can proceed to upload it to Lambda.Step 6: Create an S3 Bucket and Copy the Zip File to It:To accomplish this, you can run the following command:Step 7: Navigate to the S3 bucket and verify that the zip file has been successfully copied.Step 8: Copy the URL of the 'Lambda-Connect_RDS.zip' file.Step 9: In the Lambda-Connect_RDS Lambda function settings, navigate to the 'Code' section, choose 'Upload from,' and provide the S3 location. Save this configuration to load your code into Lambda.Step 10: Set the Correct Handler Name in the 'Runtime' Section:Use 'Lambda-Connect_RDS.lambda_handler' as the handler name.Step 11: Grant Lambda Permissions to Access the RDS Database:Create an RDS role (e.g., AWSLambdaRDSFullAccess).Navigate to IAM -> Roles -> Create role -> Choose 'AWS Service' as the trusted entity, 'Use case' as 'Lambda,' and add the 'AmazonRDSDataFullAccess' policy.Next, in the Lambda function settings, navigate to 'Permissions' and edit the existing role to add 'LambdaRDSFullAccess' under 'Existing Role.'Step 12: Configure VPC Settings:Ensure that you have selected the correct VPC, subnets, and security groups to allow access to the RDS instance.Step 13: Test the Function:After completing these steps, test the function. You should see the expected output:Using Lambda to send messages to a Slack channel.Now the final exerise for this post.Step 1: Before we proceed, create a Slack channel and obtain the Slack webhook URL, which you will use in the Lambda function. It should look something like this:Step 2: Now, let's create the Lambda function.Name the Lambda function "Connect-Lambda-with-slack."Step 3: We'll need to prepare the necessary files on an EC2 instance. Here's how:Create a directory.Inside this directory, create a Python file named "Connect-Lambda-with-slack.py" and add the following code:Step 4: Install the required libraries:Run the following commands to install the "requests" library:Step 5: Create a zip package that includes your Python code and required libraries:Step 6: Copy the resulting zip file to an S3 bucket:Step 7: Retrieve the URL of the "Connect-Lambda-with-slack.zip" file stored in your S3 bucket. This URL will be used to upload the code to the Lambda function.Step 8: In the Lambda function's configuration, under the "Runtime" settings, set the handler to "Connect-Lambda-with-slack.lambda_handler."Step 9: Perform a test of the Lambda function to ensure its functionality.Step 10: Upon running the test, you will observe the following output, and the message will be sent to your designated Slack channel.Finally, we have reached the end of this post on AWS Lambda. We hope you found it informative and enjoyable.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Ricardo Sueiras - Sep 4 Ken Collins - Sep 4 Rick Delpo - Sep 3 Theo Jung - Sep 2 Would you like to become an AWS Community Builder? Learn more about the program and apply to join when applications are open next. Once suspended, aws-builders will not be able to comment or publish posts until their suspension is removed. Once unsuspended, aws-builders will be able to comment and publish posts again. Once unpublished, all posts by aws-builders will become hidden and only accessible to themselves. If aws-builders is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Indika_Wimalasuriya. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag aws-builders: aws-builders consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging aws-builders will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

AWS Lambda: A Hands-On Beginner's Guide

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×