Skip to content

10 Lambda S3

DodaTech 1 min read

title: Lambda and S3 Integration for Serverless APIs weight: 20 date: 2026-06-28 lastmod: 2026-06-28 description: Integrate AWS Lambda with S3 for file processing including upload triggers, image resizing, file validation, presigned URLs, and event-driven workflows. tags: [api-development, serverless]


Lambda integrates with S3 for event-driven file processing, triggered on object creation or deletion events, enabling image resizing, file validation, virus scanning, and generating presigned URLs for direct client uploads.

```python
import json
import boto3
from PIL import Image
import io

s3 = boto3.client("s3")

def lambda_handler(event, context):
    for record in event["Records"]:
        bucket = record["s3"]["bucket"]["name"]
        key = record["s3"]["object"]["key"]

        # Get the uploaded image
        response = s3.get_object(Bucket=bucket, Key=key)
        image_data = response["Body"].read()

        # Resize image
        img = Image.open(io.BytesIO(image_data))
        img.thumbnail((200, 200))

        # Save resized version
        buffer = io.BytesIO()
        img.save(buffer, "JPEG")
        buffer.seek(0)

        s3.put_object(
            Bucket=bucket,
            Key=f"thumbnails/{key}",
            Body=buffer,
            ContentType="image/jpeg"
        )

    return {"statusCode": 200}

What's Next

Now learn about Lambda error handling in Building Serverless APIs.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro