AWS Builder Center

AWS lambda

AWS lambda explained for explanation for beginners to easy understanding

student

AWS Lambda Explained for Beginners with a Simple Example

Introduction

When we build applications, we usually need a server to run our code. Managing servers, installing software, handling scaling, and maintaining infrastructure can take time and effort.
AWS Lambda provides a different approach. It allows us to run code without managing servers ourselves. We can upload our code to Lambda, define when it should run, and AWS takes care of the underlying infrastructure.
In this article, we will understand AWS Lambda in simple terms and create a basic example using Python.

What is AWS Lambda?

AWS Lambda is a serverless compute service provided by Amazon Web Services (AWS).
With Lambda, we can run code in response to events without having to create or manage a server.
For example, imagine an application where a user uploads an image. Instead of keeping a server running continuously, we can configure a Lambda function to automatically execute whenever a new image is uploaded.
The basic idea is:
Event → Lambda Function → Result
Lambda can be triggered by many AWS services and applications, such as Amazon S3, Amazon API Gateway, Amazon DynamoDB, and event schedules.

Why is Lambda Called Serverless?

The term serverless does not mean that servers do not exist.
Servers are still used behind the scenes, but AWS manages them for us.
With traditional server-based applications, developers may need to think about:
  • Server provisioning
  • Operating systems
  • Scaling
  • Infrastructure maintenance
  • Availability
With AWS Lambda, AWS manages much of this infrastructure, allowing developers to focus mainly on their application code.

How AWS Lambda Works

A Lambda function generally works in the following way:
  1. An event occurs.
  2. The event triggers the Lambda function.
  3. AWS provides the required computing environment.
  4. The Lambda function executes the code.
  5. The function returns a result or performs an action.
For example:
User uploads a file → Amazon S3 detects the upload → Lambda is triggered → Lambda processes the file
This event-driven approach makes Lambda useful for many cloud applications.

Creating a Simple Lambda Function

Let's create a simple Lambda function using Python.
The function will receive an event and return a greeting message.

Step 1: Open AWS Lambda

Sign in to the AWS Management Console and search for Lambda.
Open the Lambda service and select Create function.
Choose:
  • Author from scratch
  • Function name: hello-lambda
  • Runtime: Python
Then create the function.

Step 2: Write the Lambda Code

Inside the Lambda code editor, we can use the following Python code:
1
2
3
4
5
6
7
def lambda_handler(event, context):
name = event.get("name", "AWS Builder")

return {
"statusCode": 200,
"body": f"Hello, {name}! Welcome to AWS Lambda."
}
Here, lambda_handler is the function that AWS Lambda invokes.
The event parameter contains information passed to the function.
The context parameter provides information about the Lambda execution environment.
In this example, we try to obtain a value called name from the event. If a name is not provided, the function uses "AWS Builder" as the default value.

Step 3: Test the Function

We can test the Lambda function using a sample event.
For example:
1
2
3
{
"name": "Sanjana"
}
When the function is executed, the result will look similar to:
1
2
3
4
{
"statusCode": 200,
"body": "Hello, Sanjana! Welcome to AWS Lambda."
}
This simple example demonstrates the basic idea behind a Lambda function: an event provides input, Lambda executes our code, and the function produces a result.

Understanding the Important Parts

Let's understand the code more clearly.

event

The event object contains input information sent to the Lambda function.
For example:
1
event.get("name")
gets the value of name from the event.

context

The context parameter contains information about the current Lambda execution.
It can provide information such as the remaining execution time and the AWS request ID.

Return Value

The function returns a Python dictionary containing:
1
2
3
4
{
"statusCode": 200,
"body": "..."
}
The exact response structure depends on how the Lambda function is being used. For example, when Lambda is integrated with API Gateway, this type of response can be useful for returning an HTTP response.

A Real-World Example

The previous example is simple, but Lambda becomes more interesting when connected with other AWS services.
Consider an online shopping application.
A possible workflow could be:
Customer places an order
↓
Amazon API Gateway receives the request
↓
AWS Lambda processes the order
↓
Amazon DynamoDB stores order information
↓
Another Lambda function sends a notification
This architecture allows different parts of an application to respond to events independently.

Advantages of AWS Lambda

1. No Server Management

Developers do not have to manage the underlying servers.

2. Automatic Scaling

Lambda can automatically run functions in response to incoming events and scale execution based on demand.

3. Event-Driven

Lambda works well with event-driven applications because functions can be triggered by events from AWS services and applications.

4. Pay for Usage

Lambda pricing is based on factors such as the number of requests and compute duration, rather than requiring a continuously running traditional server.

5. Supports Multiple Programming Languages

AWS Lambda supports several runtimes, including Python, Java, JavaScript/Node.js, and others.

When Should We Use AWS Lambda?

Lambda can be useful when an application needs to execute relatively small pieces of code in response to events.
Some examples include:
  • Processing uploaded files
  • Creating APIs
  • Running scheduled tasks
  • Processing application events
  • Automating cloud operations
  • Backend processing for applications
  • Data processing workflows
However, Lambda is not automatically the best choice for every application. Applications that require long-running processes or specific server-level control may require other AWS services or architectures.

What I Learned from This Example

As a beginner, the most important concept I learned is that AWS Lambda allows us to concentrate on what the code should do instead of managing the server on which the code runs.
The simple Python example also helped me understand the relationship between an event, a Lambda function, and its output.
Once this basic concept is clear, Lambda can be combined with services such as S3, API Gateway, DynamoDB, and CloudWatch to build more complete cloud applications.

Conclusion

AWS Lambda is an important AWS service for building event-driven and serverless applications.
The basic workflow is simple:
Event → Lambda → Code Execution → Result
For beginners, creating a small Python Lambda function is a good way to understand serverless computing before moving on to more advanced architectures.
My next step would be to connect this Lambda function with another AWS service, such as Amazon S3 or API Gateway, and build a small practical project around it.
Have you tried AWS Lambda before? What would you like to build with Lambda as a beginner? Share your ideas in the comments!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article