# AWS Lambda Theory and Tutorial

## **Introduction**

This guide offers a hands-on introduction to **AWS Lambda**, explaining its value in the serverless computing model and walking you through the process of building your first Lambda function. Using a sample use case, you'll learn how to trigger a Lambda function in response to an **Amazon S3** file upload event.

Whether you’re new to AWS or aiming to strengthen your cloud skills, this structured walkthrough will help you gain foundational experience with Lambda.

---

## **1\. Overview of AWS Lambda and Serverless Architecture**

### **What is AWS Lambda?**

AWS Lambda is a serverless computing service that lets you **run code in response to events**—without provisioning or managing servers. It abstracts the infrastructure layer, allowing developers to focus solely on application logic.

Although your code still runs on servers behind the scenes, AWS handles provisioning, auto-scaling, patching, and monitoring. You simply define the **function code**, memory requirements, and trigger sources.

### **Core Concepts**

* **Event-Driven Execution**: Lambda functions are triggered by events such as file uploads, API requests, or database changes.
    
* **Lightweight Functions**: Each Lambda is a focused, small block of code written in supported languages like Python, Node.js, Java, and .NET.
    
* **Fully Managed Runtime**: AWS automatically scales your function execution based on incoming events.
    

---

## **2\. Benefits of AWS Lambda**

### **Modular Architecture**

Lambda encourages small, single-purpose functions, making your codebase more organized, maintainable, and easier to debug.

### **Precise Resource Allocation**

You define the amount of memory and maximum execution time per function, optimizing performance while minimizing cost.

### **Automatic Scaling**

Lambda handles scale-out seamlessly. Whether you have one or one million requests per second, AWS automatically adjusts compute resources to meet demand.

### **Ideal Use Cases**

Event-driven workflows like media processing, real-time file analysis, and data transformations are ideal for Lambda. For example, an app like **Best Flicks**—a short-film platform—can use Lambda to create thumbnails or transcode videos upon file upload.

---

## **3\. Step-by-Step: Creating Your First Lambda Function**

### **Use Case: Process File Upload to Amazon S3**

You’ll create a Lambda function that is triggered when a file is uploaded to an S3 bucket. The function will identify the file type and log it for review.

---

### **Step 1: Create an Amazon S3 Bucket**

1. Sign in to the **AWS Management Console**.
    
2. Navigate to **Amazon S3** and select **Create Bucket**.
    
3. Choose a unique name (e.g., `best-flicks-demo-2025`) and select a region (e.g., `us-east-2` - Ohio).
    
4. Configure remaining settings as default and create the bucket.
    

> Note: The S3 bucket and Lambda function must reside in the same region for the integration to work.

---

### **Step 2: Create a Lambda Function**

1. Open the **AWS Lambda** console and choose **Create Function**.
    
2. Select **Author from scratch**.
    
3. Set a name like `process-upload-event`.
    
4. Choose the **Python 3.9** runtime (or preferred language).
    
5. Under **Permissions**, create a new execution role with the **AmazonS3ReadOnlyAccess** policy.
    

---

### **Step 3: Add Your Lambda Code**

Paste the following sample code into the inline editor:

```python
import json
import urllib.parse
import boto3

print('Loading function')
s3 = boto3.client('s3')

def lambda_handler(event, context):
    #print
    # Get the object from the event and show its content type

    bucket = event['Records'][0]['s3']['bucket']['name']
    
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

```

Click **Deploy** to save and activate your function.

---

### **Step 4: Configure the S3 Trigger**

1. In the Lambda console, go to the **Configuration** tab.
    
2. Select **Triggers &gt; Add trigger**.
    
3. Choose **S3** as the source.
    
4. Select your bucket and choose event type: **PUT - Object Created**.
    
5. Save changes.
    

Behind the scenes, AWS will:

* Configure S3 bucket notifications to invoke the Lambda function.
    
* Add permissions to your Lambda resource policy allowing S3 to call it.
    

---

### **Step 5: Upload a File and Test**

1. Go to your S3 bucket and upload an image or text file.
    
2. Navigate back to the **Lambda &gt; Monitor &gt; Logs** tab.
    
3. Open **CloudWatch Logs** to verify the function was triggered and view the printed content type.
    

You should see output such as:

```plaintext
File 'sample.jpg' in bucket 'best-flicks-demo-2025' has content type: image/jpeg
```

---

## **4\. Clean-Up: Avoid Unintended Charges**

After completing your test:

* **Delete objects** in the S3 bucket, then remove the bucket.
    
* In Lambda, select the function and choose **Actions &gt; Delete**.
    

---

## **Conclusion: Embracing Serverless Development with AWS Lambda**

AWS Lambda simplifies how you build and scale applications by eliminating server management overhead. Through modular design and automatic scaling, Lambda empowers developers to build efficient, event-driven systems.

By creating your first function tied to an S3 event, you've taken the first step toward mastering AWS's serverless ecosystem. From here, you can extend your knowledge by integrating Lambda with services like **API Gateway**, **Step Functions**, and **DynamoDB** to build production-grade solutions.

**About me:** I am a Cloud Architect who like to learn and write about Cloud concepts. If you are an organization that want to hire me then I can be contacted at [**techonlinewriter@gmail.com**](mailto:techonlinewriter@gmail.com)
