> ## Documentation Index
> Fetch the complete documentation index at: https://cloud-architect.ipoint-labs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction to Amazon Lambda

> Hands-on lab · 30m

Launch this lab in the IP Lab Portal, then follow the steps below in the AWS console.

<a className="ip-lab-portal-btn" href="https://labs.intellectualpoint.com/labs/introduction-to-amazon-lambda" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### Lab details

1. This lab walks you through the creation and usage a serverless AWS service called AWS Lambda. In this lab, we will create a sample Lambda function to be triggered on an S3 Object upload event. The lambda function will make a copy of that object and place it in a different S3 bucket.
2. Duration: **30 minutes**
3. AWS Region: **US East (N. Virginia) us-east-1**

### Architecture diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/001.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=3ee230ff240f1834bb8aa7ff7c581522" alt="" width="1800" height="1600" data-path="images/labs/introduction-to-amazon-lambda/001.png" />

### Task details

1. Sign in to AWS Management Console.
2. Create two S3 buckets.
3. Create an IAM Policy.
4. Create an IAM Role.
5. Create a Lambda function to copy the object from one bucket to another bucket.
6. Adding triggers to Lambda function.
7. Test the Lambda Function.
8. Validation of the lab.

### Launching the lab environment

1. To launch the lab environment, Click on the **Launch lab** button.
2. Please wait until the cloud environment is provisioned. It will take less than a minute to provision.
3. Once the Lab is started, you will be provided with **IAM user name**, **Password**, **Access** **Key**, and **Secret** **Access** **Key**.

<Note>
  You can only start one lab at any given time
</Note>

## Files you need

Download these before you start — the lab cannot be completed without them.

* [Smiley Image](/images/labs/downloads/introduction-to-amazon-lambda/smiley-image.jpeg)

## Lab guide

### Lab steps

#### Task 1: Sign in to AWS Management Console

1. Click on the **Open console** button, and you will get redirected to AWS Console in a new browser tab.
2. On the AWS sign-in page,

* Leave the Account ID as default. Never edit/remove the 12 digit Account ID present in the AWS Console. otherwise, you cannot proceed with the lab.
* Now copy your **User Name** and **Password** in the Lab Console to the **IAM Username and Password** in AWS Console and click on the **Sign in** button.

3. Once Signed In to the AWS Management Console, Make the default AWS Region as **US East (N. Virginia) us-east-1.**

#### Task 2: Create Two Amazon S3 Buckets

In this task, we will create two AWS S3 buckets i.e the source bucket and the destination bucket by providing the required configurations like name, region etc.

1. Navigate to the **Services** menu in the top, then click on **S3** in the **Storage** section.
2. Click on **Create Bucket** button.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/002.jpg?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=6d8a19305663711f578d847814d6cd12" alt="" width="2355" height="554" data-path="images/labs/introduction-to-amazon-lambda/002.jpg" />

3. Bucket Name: Enter ***mysourcebucket1234567***

<Note>
  Every S3 bucket name is unique globally, so create the bucket with a name not currently in use.
</Note>

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/003.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=0e9869f8ecfcc3c16385de3ed2e0c82d" alt="" width="1222" height="361" data-path="images/labs/introduction-to-amazon-lambda/003.png" />

4. Leave other settings as default and click on the **Create bucket** button.

5. Copy the name of the bucket and save it in a text file for later use.

**6**. **Create Destination Bucket**

* Click on **Create bucket** button.
* Bucket Name: Enter ***mydestinationbucket1234567***

  > **Note:** Every S3 bucket name is unique globally, so create the bucket with a name not currently in use.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/004.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=529639ae3590e703ff9a8f8a5a92dd76" alt="" width="1222" height="359" data-path="images/labs/introduction-to-amazon-lambda/004.png" />

7. Leave other settings as default and click on the **Create bucket** button.

8. Copy the name of the bucket and save it in a text file for later use.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/005.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=389f3f5e4a0c0128811f445d9ea796af" alt="" width="1222" height="431" data-path="images/labs/introduction-to-amazon-lambda/005.png" />

9. Now we have two S3 buckets (Source and Destination). We will make use of our AWS Lambda function to copy the content from source bucket to destination bucket.

#### Task 3: Create a Lambda Function

1. Make sure you are in the **US East (N. Virginia)** region.
2. Go to the **Services** menu and click on **Lambda** under **Compute** section.
3. Click on the **Create a function** button.

   * Choose **Author from scratch**
   * Function name : Enter ***mylambdafunction***
4. Runtime : Select **Python 3.13**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/006.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=54266adde4456de6b3db49a982331351" alt="" width="1222" height="450" data-path="images/labs/introduction-to-amazon-lambda/006.png" />

* Expand **Advanced Settings**, then **Enable Custom execution role** option.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/007.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=6b955184dd36cc71b23d8946c71549a0" alt="" width="1880" height="832" data-path="images/labs/introduction-to-amazon-lambda/007.png" />

Select **whiz\_lambda\_role-XXXX** from the list. Click on Save.

4. Click on the **Create function** button.

5. If you scroll down a little bit, you can see the **Code source** section. Here we need to write a Python function that copies the object from the source bucket and paste it into the destination bucket.

6. Remove the existing code in AWS lambda lambda\_function.py. Copy the below code and paste it into your **lambda\_function.py** file.

   ```
   import boto3

   def lambda_handler(event, context):
       s3 = boto3.client('s3')
       source_bucket = "Source_Bucket_name"
       destination_bucket = "Destination_Bucket_name"

       # Extract the object key from the event
       object_key = event['Records'][0]['s3']['object']['key']

       # URL encode the source object key
       copy_source = {'Bucket': source_bucket, 'Key': object_key}

       # Set up the parameters for the copy operation
       copy_params = {
           'Bucket': destination_bucket,
           'CopySource': copy_source,
           'Key': object_key
       }

       try:
           # Perform the copy operation
           result = s3.copy_object(**copy_params)
           print("S3 object copy successful. Response:", result)
       except Exception as e:
           print("Error copying object:", str(e))
   ```

7. You need to change the **source** and **destination bucket name** in the index.js file **line number 5 & 6** which we copied earlier.

8. Save the function by clicking on **Deploy** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/008.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=b46cbabfd74701aafbb96e5183a9bcb5" alt="" width="1222" height="681" data-path="images/labs/introduction-to-amazon-lambda/008.png" />

#### Task 4: Adding Triggers to Lambda Function

1. Scroll up to go to **Function overview** and click on **+ Add trigger** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/009.jpg?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=2ca1bbab3619d2c35d896ef90ba36ff8" alt="" width="1913" height="684" data-path="images/labs/introduction-to-amazon-lambda/009.jpg" />

2. Scroll down the list and select **S3** from the trigger list. Once you select S3, a form will appear. Enter these details:

* Bucket: Select your source bucket - **mysourcebucket1234567**
* Event type: Select **All object create events**
* Leave other fields as default.
* And check the option of **Recursive invocation** to avoid failures in case you upload multiple files at once by **Checking** the acknowledge option.
* Click on **Add** button.

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/010.jpg?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=0a7d393a268e8718987c7d1b92793841" alt="" width="2880" height="459" data-path="images/labs/introduction-to-amazon-lambda/010.jpg" />

#### Task 5: Test Lambda function

1. If you have a test image on your local machine, you can use that image.

   * Click on [Download Me](/images/labs/downloads/introduction-to-amazon-lambda/smiley-image.jpeg) to open the image in new tab, right click the image and save it to your local.

     > **Note:** Please name the image file as "image.jpeg" without using variations like "image (2).jpeg".
2. Go to S3 Bucket list and click on source bucket - **mysourcebucket1234567**.
3. Upload image to source S3 bucket. To do that:

   * Click on the **Upload** button.
   * Click on **Add files** button to add the files.
   * Select the image and click on the **Upload** to upload the image.
4. Now go back to the S3 list and open your destination bucket **- mydestinationbucket1234567.**
5. You can see a copy of your uploaded source bucket image in the destination bucket.

#### Optional: Debugging Lambda Function Using CloudWatch

If you encounter errors such as the source file not being copied to the destination S3 bucket, please debug your Lambda function by following the steps mentioned below:

1. From the AWS Management Console, navigate to the **CloudWatch** service.

2. In the left sidebar, under the **"Logs Management"** menu, select **"Log groups.**

3. Select your Lambda function's **log group,** which should be in the format **/aws/lambda/function-name.**

4. Choose a **log stream** to view its logs.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/011.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=e682ca38002b5c7cbd34a812dfe16937" alt="" width="1222" height="274" data-path="images/labs/introduction-to-amazon-lambda/011.png" />

5. In the list of **Log events,** select the right arrow next to the Timestamp to expand the details for that event.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/012.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=b921b39568b8a79a0575eb62bb910129" alt="" width="1222" height="389" data-path="images/labs/introduction-to-amazon-lambda/012.png" />

### Validation Test

1. Once the lab steps are completed, please click on the **Check my work** button on the left side panel.
2. This will validate the resources in the AWS account and displays whether you have completed this lab successfully or not.
3. Enter the source bucket name and destination bucket name in the user input fields and click the **Check my work** button.
4. Sample output :

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/introduction-to-amazon-lambda/013.gif?s=0ab90eef247946cf66d690152c64f179" alt="" width="1002" height="630" data-path="images/labs/introduction-to-amazon-lambda/013.gif" />

> **Do you know ?**
>
> AWS Lambda has a generous free tier that allows you to run up to 1 million requests and 400,000 GB-seconds of compute time per month for free, making it an ideal choice for small-scale projects and experiments.

### Completion and conclusion

1. You created two S3 buckets acting as source and destination.
2. You created an IAM policy and role that will be used for the Lambda function.
3. You have successfully created an AWS Lambda function and configured an S3 trigger.
4. You have successfully triggered the Lambda function to copy an image from the source S3 bucket to the destination S3 bucket.

### End lab

1. Sign out from the AWS Management Console.
2. Click on **End lab** button in the IP Lab Portal and wait till the process gets completed.

## What gets checked

When you press **Check my work**, the platform verifies each of these:

* **Create Private S3 bucket** — Check whether a private S3 bucket is created or not
* **check s3 object** — Check whether an object is uploaded to the S3 bucket.
* **Create an AWS Lambda Function** — Check whether a Lambda Function is created or not

## Related help

* [FAQs and Troubleshooting](/aws-cp/support/faqs-and-troubleshooting)
