Skip to content

An AWS Lambda function that automatically triggers when a new file is uploaded to an S3 bucket. It logs the uploaded filename and demonstrates event-driven architecture in AWS.

Notifications You must be signed in to change notification settings

tnhkoc/lambda-s3-file-trigger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 

Repository files navigation

AWS Lambda – S3 Trigger Integration (Python)

This project demonstrates a basic event-driven architecture using AWS Lambda and Amazon S3. When a file is uploaded to a specific S3 bucket, the Lambda function is triggered and logs the file name to Amazon CloudWatch.


βœ… Objective

  • Understand how S3 triggers work with Lambda
  • Learn how to handle AWS events in Python
  • Practice role-based permissions using IAM
  • Monitor function execution via CloudWatch Logs

πŸ› οΈ Services Used

  • Amazon S3 – Event source for uploads
  • AWS Lambda – Event-driven compute
  • IAM – Role for Lambda to access S3 and CloudWatch
  • Amazon CloudWatch – To log and view events

πŸ“ Architecture

  1. File uploaded to S3
  2. S3 sends an event to Lambda
  3. Lambda function logs the filename
  4. Output visible in CloudWatch Logs

πŸš€ Setup Instructions

1. Create an S3 Bucket

  • Go to the S3 console β†’ Create bucket
  • Example name: lambda-s3-trigger-demo1
  • Ensure ACLs are disabled and public access is blocked

πŸ“Έ Screenshot:
S3 Bucket Creation


2. Create the Lambda Function

  • Go to Lambda Console β†’ Create function
  • Runtime: Python 3.10
  • Permissions: Create a new role with basic Lambda permissions

Paste the following code:

import json

def lambda_handler(event, context):
    file_name = event['Records'][0]['s3']['object']['key']
    print(f"New file uploaded: {file_name}")
    return {
        'statusCode': 200,
        'body': json.dumps(f"Processed file: {file_name}")
    }

πŸ“Έ Screenshot:
Lambda Function Code


3. Add S3 as Trigger

  • In Lambda β†’ Click Add trigger
  • Choose Amazon S3
  • Select the bucket created (lambda-s3-trigger-demo1)
  • Event type: PUT
  • Save

⚠️ If you get a permissions error make sure to check the box: :

β€œI acknowledge that using the same S3 bucket for both input and output is not recommended...”
Otherwise, the trigger won't be created.

πŸ“Έ Screenshot:
Trigger Error


4. Upload a File to S3

  • Upload a test file (e.g., test.txt) into the S3 bucket

5. Check Logs in CloudWatch

  • Go to CloudWatch β†’ Log Groups β†’ /aws/lambda/your-function-name
  • Verify that you see a log like: New file uploaded: test.txt

πŸ“Έ Screenshot:
CloudWatch Log


πŸ“Œ Notes

  • This project demonstrates a simple event trigger setup
  • You can expand the Lambda to process file content, send notifications, or trigger other AWS services
  • Make sure the IAM role has:
    • AmazonS3ReadOnlyAccess
    • AWSLambdaBasicExecutionRole

About

An AWS Lambda function that automatically triggers when a new file is uploaded to an S3 bucket. It logs the uploaded filename and demonstrates event-driven architecture in AWS.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published