AWS Builder Center
Understanding AWS Lambda by Building a Real-World Serverless Project

Understanding AWS Lambda by Building a Real-World Serverless Project

Learn how to build an event-driven file upload monitoring system using AWS Lambda, Amazon S3, and Amazon CloudWatch. This hands-on tutorial explains serverless computing, S3 event notifications, Python Lambda functions, IAM permissions, and CloudWatch monitoring through a practical project.

Introduction

Imagine uploading a file to cloud storage and having another process automatically execute whenever that upload happens.
You don't need to keep a server running, write a script that continuously checks for new files, or manually trigger the process.
This is where AWS Lambda becomes useful.
AWS Lambda is a serverless compute service that allows developers to run code in response to events without managing the underlying servers. AWS handles infrastructure provisioning, scaling, and execution environments, allowing developers to focus on application logic.
But understanding Lambda through definitions alone isn't enough. The best way to understand it is to build something.
In this article, we'll build a simple file upload monitoring system using:
  • Amazon S3: To store uploaded files.
  • AWS Lambda: To execute Python code automatically.
  • Amazon CloudWatch: To monitor execution logs.
By the end, you'll understand how event-driven architecture works and how different AWS services communicate with each other.

1. Understanding the Architecture

Before writing code, let's understand what we're building.
Our application will follow a simple workflow:
  1. A user uploads a file to an Amazon S3 bucket.
  1. S3 detects the upload event.
  1. S3 sends an event notification to AWS Lambda.
  1. Lambda executes our Python function.
  1. The function extracts information about the uploaded file.
  1. Execution details are recorded in Amazon CloudWatch Logs.
The important concept here is that Lambda doesn't need to continuously check S3 for new files.
Instead, S3 notifies Lambda when a specific event occurs.
This is an example of event-driven architecture.

2. Creating an Amazon S3 Bucket

First, we need somewhere to store our files.
Follow these steps:
  1. Open the AWS Management Console.
  1. Navigate to Amazon S3.
  1. Select Create bucket.
  1. Enter a globally unique bucket name.
  1. Select your preferred AWS Region.
  1. Keep Block Public Access enabled.
  1. Create the bucket.
For this project, we'll use the bucket only for storing files and generating upload events.
There is no reason to make it publicly accessible.

3. Creating an AWS Lambda Function

Now let's create the function that will process our upload events.
Follow these steps:
  1. Navigate to AWS Lambda.
  1. Select Create function.
  1. Choose Author from scratch.
  1. Enter the function name:
 S3FileUploadMonitor 
  1. Select Python 3.14 as the runtime.
  1. Choose the default execution architecture.
  1. Create the function.
AWS automatically creates an execution role for the function with basic permissions to send logs to CloudWatch.
This is important because Lambda needs appropriate IAM permissions to interact with other AWS services.
For this project, the basic logging permissions are sufficient.

4. Writing the Python Code

Now comes the interesting part.
We'll write a Python function that receives an S3 event and extracts information about the uploaded file.
Replace the default Lambda code with:
import json

def lambda_handler(event, context):

print("New S3 upload event received!")

for record in event['Records']:

bucket = record['s3']['bucket']['name']
file_name = record['s3']['object']['key']
file_size = record['s3']['object']['size']

print("Bucket:", bucket)
print("File Name:", file_name)
print("File Size:", file_size, "bytes")

return {
'statusCode': 200,
'body': json.dumps('File upload processed successfully!')
}
Let's understand what's happening.
lambda_handler(event, context)
This is the entry point of our Lambda function.
  •  event  contains information about what triggered the function.
  •  context  provides information about the current execution environment.
event['Records']
An S3 event can contain one or more event records. Each record provides information about an event that occurred.
bucket
Extracts the name of the S3 bucket where the file was uploaded.
file_name
Extracts the uploaded object's key.
file_size
Retrieves the object's size in bytes from the event notification.
Finally, the function prints these details, which we can inspect using CloudWatch Logs.
After adding the code, select Deploy.

5. Connecting S3 to Lambda

Our Lambda function is ready, but it doesn't yet know when an upload happens.
We need to configure an event notification.
Follow these steps:
  1. Return to your Amazon S3 bucket.
  1. Open the Properties tab.
  1. Find Event notifications.
  1. Select Create event notification.
  1. Enter a name, such as  FileUploadTrigger .
  1. Under Event types, select All object create events.
  1. Under Destination, select Lambda function.
  1. Choose  S3FileUploadMonitor .
  1. Save the configuration.
AWS will configure the required permission for S3 to invoke your Lambda function through the console workflow.
Now the two services are connected.
Whenever a new object is uploaded to the configured bucket, S3 can invoke our Lambda function.

6. Testing the Project

It's time to test our implementation.
Go back to your S3 bucket and upload a test file.
For example:
 sample-document.pdf 
After uploading the file:
  1. Open AWS Lambda.
  1. Select your function.
  1. Navigate to the Monitor tab.
  1. Select View CloudWatch logs.
  1. Open the latest log stream.
You should see output similar to:
New S3 upload event received!

Bucket: my-project-bucket
File Name: sample-document.pdf
File Size: 204800 bytes
The exact bucket name and file size will depend on your upload.
Congratulations!
You've successfully created an event-driven application using Amazon S3 and AWS Lambda.
No continuously running server was required.

7. Understanding What We Actually Built

Although this is a relatively small project, it demonstrates several important cloud computing concepts.
Event-driven computing
Our application responds to an event instead of continuously checking for changes.
Serverless architecture
We didn't provision or maintain an EC2 instance to execute our Python code.
Service integration
We connected two AWS services using an event notification mechanism.
Identity and Access Management
The Lambda execution role controls what our function is allowed to do, while the Lambda resource-based permission allows S3 to invoke it.
Monitoring and observability
CloudWatch Logs provides visibility into function execution, helping developers troubleshoot problems.
These concepts are relevant to much larger cloud applications.

8. Improving the Project

Our current implementation only records information about uploaded files.
However, we can extend it in several ways.
Here are some interesting improvements:
Enhancement
AWS Service
Purpose
Send upload notifications
Amazon SNS
Notify administrators about new uploads
Store file metadata
Amazon DynamoDB
Maintain a record of uploaded files
Automatically scan files
Amazon SQS + Lambda
Build an asynchronous processing workflow
Generate file reports
Amazon EventBridge
Schedule periodic reporting
Create file thumbnails
Amazon S3 + Lambda
Automate image processing
These improvements can turn a simple demonstration into a more complete serverless application.

9. Important Considerations

Before using this architecture in production, keep a few things in mind:
  • IAM permissions: Follow the principle of least privilege. Give Lambda only the permissions it needs.
  • Error handling: Add proper exception handling and consider retry behavior.
  • Duplicate events: Design event processing to handle duplicate notifications safely.
  • Monitoring: Monitor Lambda errors, duration, and invocation metrics.
  • Costs: Understand Lambda invocation and execution pricing, along with S3 storage and request charges.
  • Security: Keep buckets private and avoid exposing sensitive information in application logs.
Also, avoid configuring a Lambda function to write files back into the same bucket using the same upload trigger without appropriate filtering. Otherwise, you could accidentally create a recursive invocation loop.

Conclusion

AWS Lambda is more than just a service for executing small pieces of code.
It provides a foundation for building event-driven applications without having to manage traditional server infrastructure.
Through this project, we explored how Amazon S3 can trigger a Lambda function, how Python processes event data, and how CloudWatch helps us monitor execution.
The most important takeaway is that cloud services become much more powerful when we understand how to connect them.
If you're learning AWS, don't stop at understanding individual services. Start combining them to solve practical problems.
Build something small, understand how it works, and then gradually make it better.
That's how you develop real cloud engineering skills.


Disclaimer: This article represents an educational demonstration. AWS service configurations, permissions, and pricing should be reviewed before adapting the project for production use.
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