โ˜๏ธ Day 30: AWS S3 & Lambda โ€” Object Storage + Serverless Automation

Pratik DasPratik Das
4 min read

Todayโ€™s deep dive covers two fundamental AWS services:

  • Amazon S3: Scalable object storage for static files, backups, and web hosting.

  • AWS Lambda: Serverless compute service that executes code on-demand without provisioning servers.

We also showcase a real-world project where a personal portfolio website is hosted on S3, and explore how Lambda functions can be triggered by S3 events, e.g., for automation on file upload.


๐Ÿง  Theory: What Is Amazon S3?

Amazon Simple Storage Service (S3) is:

  • A durable, highly scalable object storage system.

  • Used to store files like images, videos, HTML, JSON, CSV, backups, etc.

  • Common for:

    • Hosting static websites

    • Media content delivery

    • Data lake storage

    • Backup and restore

    • Triggering serverless actions

Each object is stored in a bucket and identified by a key (file path).


๐Ÿง  Theory: What Is AWS Lambda?

AWS Lambda is a serverless compute service that:

  • Runs your code in response to triggers/events

  • Supports languages like Python, Node.js, Java, Go, etc.

  • No server management

  • You only pay for execution time

  • Common triggers:

    • S3 (file upload)

    • API Gateway

    • DynamoDB Streams

    • CloudWatch Events

    • SNS/SQS


๐Ÿ”ง AWS S3 Setup โ€“ Step-by-Step (Static Website Hosting)

โœ… Step 1: Create S3 Bucket

aws s3 mb s3://my-portfolio-website

โœ… Step 2: Enable Static Website Hosting

From AWS Console โ†’ S3 โ†’ Your Bucket โ†’ Properties
Enable โ€œStatic Website Hostingโ€

  • Index document: index.html

  • Error document: error.html (optional)

โœ… Step 3: Set Bucket Policy for Public Read

{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "PublicReadGetObject",
    "Effect": "Allow",
    "Principal": "*",
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::my-portfolio-website/*"
  }]
}

Apply this in the Permissions โ†’ Bucket Policy section.

โœ… Step 4: Upload Files

aws s3 sync ./portfolio-website s3://my-portfolio-website

Your site is now live at:

http://my-portfolio-website.s3-website-<region>.amazonaws.com

โœจ Project Highlight: ๐Ÿš€ Personal Portfolio Hosted on S3

๐Ÿ“Œ What You Built:

A responsive one-page portfolio featuring:

  • About

  • Skills

  • Projects

  • Contact section

๐Ÿงฑ Folder Structure:

portfolio-website/
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ style.css
โ”œโ”€โ”€ images/
โ””โ”€โ”€ scripts/

๐ŸŒ Hosted at:

http://your-bucket-name.s3-website-region.amazonaws.com

No server needed. No EC2. Just fast, scalable S3.


๐Ÿ” AWS Lambda Triggered by S3 Upload

๐ŸŽฏ Goal:

Trigger a Lambda function when a new file is uploaded to a specific S3 bucket.


๐Ÿชœ Setup: Lambda + S3 Trigger (Python Example)

โœ… Step 1: Create Lambda Function

From AWS Console โ†’ Lambda โ†’ Create function

  • Runtime: Python 3.9

  • Permissions: Create new role with basic Lambda permissions

Function code (example):

import json

def lambda_handler(event, context):
    for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']
        print(f"File uploaded: s3://{bucket}/{key}")
    return {
        'statusCode': 200,
        'body': json.dumps('Success')
    }

โœ… Step 2: Add Trigger from S3

From Lambda Console:

  • Add trigger โ†’ Choose S3

  • Choose your S3 bucket

  • Event type: PUT (Object created)

  • Prefix (optional): e.g., uploads/

  • Suffix (optional): e.g., .jpg

Grant S3 permission to invoke Lambda.


๐Ÿ“ฆ Use Case Examples

Use CaseTriggerAction
Image uploadsS3 PUTLambda resizes images and stores thumbnails
CSV uploadsS3 PUTLambda processes and stores data in DynamoDB
Webhook-like automationS3 PUTLambda sends SNS or Slack alerts
Static logsLambda โ†’ S3Write logs from serverless apps

๐Ÿงช Sample CLI Commands

Create S3 Bucket

aws s3 mb s3://trigger-demo-bucket

Upload a Test File

aws s3 cp test.txt s3://trigger-demo-bucket/uploads/

Lambda will be triggered automatically.


๐Ÿ›ก๏ธ Best Practices

  • Enable versioning in S3

  • Use S3 Lifecycle Policies for auto-deletion

  • Use environment variables in Lambda

  • Use CloudWatch for monitoring Lambda execution


๐Ÿ“˜ Summary

FeatureDescription
S3Scalable object storage for static websites, media, backups
LambdaServerless compute to run event-driven code
TriggerS3 can trigger Lambda on file uploads
Your ProjectDeployed static portfolio on S3

๐Ÿ”ฎ Whatโ€™s Next?

๐Ÿ”ฎ Whatโ€™s Next? โ€“ Day 31 Preview
Tomorrowโ€™s learning lineup is all about powerful AWS services that handle data, routing, and app deployment:

๐Ÿ”ง AWS CLI โ€“ Automate and control your AWS services using command-line tools

๐Ÿ—ƒ๏ธ DynamoDB โ€“ Explore AWS's fully managed NoSQL database
โœ”๏ธ Fast, scalable, and serverless
โœ”๏ธ Ideal for key-value and document-based workloads

๐ŸŒ Route 53 โ€“ AWS's highly available DNS service
โœ”๏ธ Route traffic globally
โœ”๏ธ Register custom domains and apply routing policies

๐Ÿš€ Elastic Beanstalk โ€“ Simplified app deployment
โœ”๏ธ Deploy web apps without worrying about infrastructure
โœ”๏ธ Automatically handles provisioning, load balancing, and scaling

Stay tuned for hands-on setups, real use cases, and clear breakdowns!

0
Subscribe to my newsletter

Read articles from Pratik Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Das
Pratik Das