When people talk about automating the tasks on cloud, one couldn't help but mention AWS Lambda. A part of Amazon Web Services, Lambda is designed to enable developers to run code without provisioning or managing servers. 'Lambda' is named after functions from lambda calculus and programming.
To understand what AWS Lambda is, we have to first understand what serverless architecture is all about.
Serverless applications in general don’t require any provisioning of servers for them to run. When we run a serverless application, we get the benefit of not worrying about OS setup, patching, or scaling of servers that we would have to consider when we run our application on a physical server.
The purpose of AWS Lambda is to build event-driven applications that can be triggered by several events in AWS.
There are three components of Lambda: A Function, A Configuration & An event source(optional).
The Function:
This is the actual code that performs the task. Let's say we want to create a Lambda function that takes two numbers as input and returns their sum. We can write this function in Python as follows:
After writing the Lambda function code, we can deploy it to AWS Lambda using the AWS Management Console, AWS CLI, or AWS SDKs. Once deployed, we can configure Lambda to trigger our function in response to specific events, such as an HTTP request, a file upload to an S3 bucket etc.
The Configuration:
This specifies how our function is executed. Consider a simple example where we have a Lambda function that responds to HTTP requests and returns a greeting message.
Here are some of the ways that show how configuration comes into play:
Runtime: We select the Python runtime for our function since we'll be writing our code in Python.
Memory Allocation: Since this function is lightweight and doesn't require much memory, you can allocate a minimal amount of memory.
Trigger: We configure the function to be triggered by an HTTP request via Amazon API Gateway. This means the function will execute whenever it receives an HTTP request from the API Gateway endpoint.
The Event Source:
This is the event that triggers the function. Suppose we're having a Lambda function that sends a welcome email to users when they sign-up for our service.
Then, the event source component of AWS Lambda comes into play in the following way:
Lambda Function:
First, we have a Lambda function written in Python that sends a welcome email.
Event Source:
The event source is the component that triggers the Lambda function. In this example, let's assume that we're using Amazon Cognito for user authentication. We can configure Cognito to trigger the function whenever a new user signs-up.
Configuration:
To configure the event source, we need to set up a trigger in Amazon Cognito. We specify our Lambda function as the target for the trigger, and Cognito automatically invokes the function whenever a new user signs-up.
Execution:
Whenever a new user signs up for our service, Cognito detects the sign-up event and triggers your Lambda function. The function then executes, sending a welcome email to the newly registered user.
Use Cases
AWS Lambda is a versatile service that can be applied to a wide range of use cases across various industries. Here are three popular use cases for Lambda:
Real-time Data Processing:
This is particularly useful for scenarios such as clickstream analysis, log processing, real-time analytics, and IoT data ingestion.
Ex.: Analyzing data from a website to track user behavior in real-time, such as identifying popular pages or personalizing content based on user actions.
Backend for Web and Mobile Applications:
Lambda functions can serve as the backend logic for web and mobile applications. They can handle various tasks such as data processing, file uploads, and notifications.
Ex.: Implementing serverless APIs using AWS API Gateway and Lambda to handle user authentication, database interactions, and business logic for a mobile app or web application.
AWS Lambda is one of the most popular services offered by Amazon Web Services to its huge global customer-base.