A Beginner’s Guide to AWS Lambda Functions: Logging Content Type of Uploaded Files in S3 Buckets

Madhura Jayashanka
Enlear Academy
Published in
3 min readDec 24, 2023

--

Table of Contents:

  1. Introduction
  2. Prerequisites
  3. Setting up AWS Lambda Function
    - Creating a Lambda Function
    - Linking Lambda Function with S3 Bucket
  4. Logging Content Type of Uploaded Files
    - Monitoring Logs in AWS Lambda
    - Viewing Logs in CloudWatch
  5. Conclusion
  6. Resources

Introduction

AWS Lambda functions provide a serverless computing service, allowing you to run code without provisioning or managing servers. In this comprehensive guide, we will explore how to create an AWS Lambda function that triggers when a file is uploaded to an S3 bucket. We will then log the content type of the uploaded file, giving you valuable insights into the data being stored.

Prerequisites

Before we dive in, make sure you have the following:

  • An AWS account with appropriate permissions
  • Basic knowledge of AWS Lambda, S3, and CloudWatch
  • Familiarity with the Python programming language

Setting up AWS Lambda Function

To get started, follow these step-by-step instructions:

3.1 Creating a Lambda Function

  1. Open the AWS Management Console and search for AWS Lambda.
  2. Click on Create function and select a suitable blueprint. For this tutorial, choose the blueprint named “Get S3 bucket object(s) (Python 3.7)”.
  3. Provide a unique name for your function and click on “Create a new role from AWS policy templates”. Enter a role name like “function-name-role”.
  4. Click on “Create function” to proceed.

3.2 Linking Lambda Function with S3 Bucket

  1. Open a new tab and search for AWS S3 in the console.
  2. Create a new S3 bucket by following the guidelines provided by AWS.
  3. Go back to the Lambda function and click on “Add trigger” using the plus icon.
  4. Select “S3” from the trigger configuration options.
  5. Choose the bucket name you created and set the event types to “All object create events”.
  6. Enable the “Enable trigger” checkbox and click on “Add”.
  7. Verify that the Lambda function is linked to your S3 bucket by checking the “Event notification” section under the bucket’s properties.

Logging Content Type of Uploaded Files

Now that we have our Lambda function and S3 bucket set up, let’s log the content type of the uploaded files.

4.1 Monitoring Logs in AWS Lambda

  1. Go to the AWS Lambda console and navigate to your function.
  2. Click on the “Monitor” tab and locate the “Logs” section.
  3. You should be able to see the log associated with your Lambda function.
  4. Click on the log entry to view the detailed function output, including the content type of the uploaded file.

4.2 Viewing Logs in CloudWatch

  1. When you click on the log entry in the AWS Lambda console, you will be redirected to CloudWatch.
  2. In the CloudWatch console, you can see the described log function and output.
  3. Look for the content type information to gain insights into the uploaded file’s data.

Conclusion

In this tutorial, we learned how to create an AWS Lambda function that triggers when a file is uploaded to an S3 bucket. We also explored how to log the content type of the uploaded files. This information can be valuable for various use cases, such as data validation and content processing. AWS Lambda provides a powerful and scalable solution for serverless computing, making it an excellent choice for processing and analyzing data in S3 buckets.

Resources

--

--