AWS Lambda Tutorial
AWS Lambda is a compute service offered by Amazon Web Services (AWS). Unlike EC2, Elastic Beanstalk, or OpsWorks, Lambda is designed to simplify the development and execution of code without the need to manage servers or underlying infrastructure. Below, we’ll explore what AWS Lambda is, how it works, and its use cases.
What is AWS Lambda?
AWS Lambda is a serverless compute service that allows developers to run code without provisioning or managing servers. With Lambda, you only need to provide the code, and AWS handles the infrastructure, scaling, and resource management automatically.
Key Benefits of AWS Lambda:
- Focus solely on writing and deploying application code.
- AWS handles launching, scaling, patching, and updating resources.
- Ideal for event-driven applications or tasks with variable workloads
Where to Use AWS Lambda
AWS Lambda is best suited for event-driven tasks, where specific triggers initiate the execution of code. For example:
- Backend processing for web and mobile applications.
- Image processing, such as resizing, filtering, or compression.
- Automated data processing tasks triggered by changes in a database or object storage (e.g., S3 buckets).
How AWS Lambda Works
Lambda functions are triggered by event sources such as file uploads, database updates, or HTTP requests. Let’s walk through an example where an image upload to an S3 bucket triggers a Lambda function:
- Event Trigger: A user uploads an image to an S3 bucket.
- Notification Setup: The S3 bucket has a notification configured to trigger a Lambda function.
- Lambda Execution Role: IAM (Identity and Access Management) roles provide Lambda the necessary permissions to access resources (e.g., S3).
- Code Execution: The Lambda function executes tasks such as resizing or filtering the uploaded image.
Why Choose AWS Lambda?
When architecting solutions, you have several compute options:
- AWS EC2:
Requires manual setup and management of virtual machines, software, and infrastructure.
- AWS Elastic Beanstalk:
Best for deploying full applications, not suitable for backend-only tasks.
- AWS OpsWorks:
Focuses on application lifecycle management, which may not be needed for simple backend tasks.
- AWS Lambda:
A lightweight, serverless option where you only upload your code, and AWS handles everything else.
Example Use Case:
- News Feed Processing:
- During high traffic (e.g., mornings), Lambda scales to handle spikes.
- During low traffic (e.g., nights), Lambda incurs no cost due to inactivity.
- However, for continuously running workloads, EC2 might be a better choice.
Limitations of AWS Lambda
While Lambda is powerful, it has some architectural and hardware limitations:
- Disk Size: Limited to 512 MB.
- Memory: Configurable between 128 MB and 1536 MB.
- Execution Timeout: Maximum of 15 minutes.
- Request Payload: Maximum size of 6 MB for synchronous requests and 128 KB for event payloads.
Pricing
AWS Lambda follows a pay-as-you-go model:
- Number of Requests:
- First 1 million requests are free.
- $0.20 per 1 million requests thereafter.
- Execution Time:
- Calculated based on memory allocation and execution duration.
- $0.00001667 per GB-second of usage.
Creating a Lambda Function
Here’s a step-by-step guide to creating a Lambda function that logs a message when an object is added to an S3 bucket:
- Navigate to AWS Lambda Console:
Under the "Compute" section in the AWS Management Console, select AWS Lambda.
- Create a New Lambda Function:
Click "Create Function" and select a blueprint (e.g., blank function).
- Set the Trigger:
Choose S3 as the trigger source and specify the bucket and event (e.g., object creation).
- Configure the Lambda Function:
- Provide a name and description for the function.
- Select a runtime (e.g., Node.js, Python).
- Use the inline editor to add the following code:javascript
Copy code
exports.handler = (event, context, callback) => {
- console.log("An object added to S3");
- };
- Set up an execution role with appropriate permissions (e.g., S3 object read-only access).
- Deploy and Test:
- Upload a file to the specified S3 bucket.
- Verify that the Lambda function logs the event in CloudWatch.
Conclusion
AWS Lambda simplifies application development by eliminating the need to manage infrastructure. It’s ideal for event-driven applications, variable workloads, and automating backend processes. However, for long-running or continuous workloads, EC2 might be a better fit.
About Naresh I Technologies:
Naresh I Technologies is the number one computer training institute in Hyderabad, ranking among the top five institutes in India. We provide comprehensive AWS training, including practical labs and real-world projects. Whether you’re a fresher or a professional, our AWS certification courses cater to all levels. Contact us today to start your AWS training journey!