🚀 About This Video
Welcome to today’s hands‑on AWS session!
In this lab, we build a real‑time, fully automated serverless workflow using Amazon S3 and AWS Lambda.
Whenever the application team uploads a .txt file into S3, a Lambda function is automatically triggered, converts the text file into a PDF document, and uploads it back to S3 — all without servers, manual steps, or waiting.
This is a perfect real‑world example of event‑driven serverless architecture used in modern cloud automation projects.
🧩 What You Will Learn
In this end‑to‑end project, we will:
✅ Create IAM Roles for Lambda
Grant Lambda secure access to S3 so it can read & write files.
✅ Create an Amazon S3 Bucket
Used as both the input (text files) and output (PDF files) bucket.
✅ Create a Lambda Layer
We package the fpdf Python library to generate PDFs inside Lambda.
✅ Build the Lambda Function
Lambda automatically converts .txt → .pdf with zero servers.
import boto3
import urllib.parse
from fpdf import FPDF
s3 = boto3.client('s3')
def _extract_bucket_key(event):
"""
Returns (bucket, key) from either:
S3 Event Notification (event['Records'][0]...)
Direct invocation: {"bucket": "...", "key": "..."}
Raises ValueError with a helpful message if not found.
"""
Case 1: Manual invocation with explicit fields
if isinstance(event, dict) and "bucket" in event and "key" in event:
return event["bucket"], event["key"]
Case 2: S3 Event notification
if isinstance(event, dict) and "Records" in event and event["Records"]:
rec = event["Records"][0]
bucket = rec["s3"]["bucket"]["name"]
key = rec["s3"]["object"]["key"]
return bucket, key
raise ValueError(
"No bucket/key found. Invoke via S3 event or pass JSON like: "
'{"bucket":"text-to-pdf-converter-s3-bucket","key":"path/file.txt"}'
)
def lambda_handler(event, context):
try:
bucket, key = _extract_bucket_key(event)
except Exception as e:
Return a clean error instead of raising KeyError
return {"status": "error", "message": str(e), "event_sample":
{"bucket": "text-to-pdf-converter-s3-bucket", "key": "incoming/sample.txt"}}
Decode URL-encoded key from S3 events
key = urllib.parse.unquote_plus(key)
Only act on .txt files
if not key.lower().endswith(".txt"):
msg = f"Skipping non-.txt object: s3://{bucket}/{key}"
print(msg)
return {"status": "skipped", "object": key, "reason": "not a .txt file"}
print(f"Processing: s3://{bucket}/{key}")
Fetch text
obj = s3.get_object(Bucket=bucket, Key=key)
text_data = obj["Body"].read().decode("utf-8", "replace")
Create PDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
FPDF core fonts are Latin-1; replace unsupported glyphs
safe_text = text_data.encode("latin-1", "replace").decode("latin-1")
pdf.multi_cell(0, 10, safe_text)
pdf_bytes = pdf.output(dest="S").encode("latin-1")
Write alongside the .txt, same path, changed extension
pdf_key = key.rsplit(".", 1)[0] + ".pdf"
s3.put_object(
Bucket=bucket,
Key=pdf_key,
Body=pdf_bytes,
ContentType="application/pdf"
)
result = f"s3://{bucket}/{pdf_key}"
print(f"PDF created at: {result}")
return {"status": "success", "pdf_key": pdf_key, "pdf_uri": result}
✅ Add S3 Event Trigger
Whenever a file is uploaded, Lambda runs instantly — fully automated.
🎯 Why This Project Is Important
This project teaches real skills used in cloud engineering:
✔ Serverless compute
✔ Event‑driven architecture
✔ Lambda Layers
✔ Real‑time automation
✔ Enterprise file‑processing workflows
This solution is perfect for Cloud Engineers, DevOps, SREs, and Architects.
💡 Use Cases
Automated document processing
File format conversion pipelines
Compliance & reporting workflows
Real‑time backend automation
Enterprise serverless solutions
⭐ Benefits of This Architecture
🔹 Fully Automated — No manual work
🔹 Fast Real‑Time File Processing
🔹 Scales Automatically
🔹 No Servers, No Maintenance
🔹 Reduces Cost & Human Errors
🔹 Ideal for Production‑Grade Apps
⛅ About This Channel
If you love AWS labs, real‑time cloud projects, serverless automation, and hands‑on tutorials, make sure to:
👉 Subscribe
👉 Like this video
👉 Comment your questions
👉 Share with your DevOps friends
Your support motivates me to create more real‑world cloud projects!
#AWS #AWSServerless #AWSLambda #AmazonS3 #CloudComputing
#DevOps #CloudEngineer #AWSTutorial #LambdaLayer #ServerlessProject
#Automation #EventDrivenArchitecture #realtimeprocessing