Skip to content

straw-hat0/serverless-tumbnail-generator

 
 

Repository files navigation

Serverless Image Resizer

Automatically resize uploaded images using AWS SAM, Lambda, S3, API Gateway, and CloudFront.

Architecture

User Upload → S3 Input Bucket → Lambda Function → S3 Output Bucket → CloudFront

Features

  • Automatic Resizing: Creates 3 sizes (thumbnail: 150x150, medium: 500x500, large: 1200x1200)
  • Multiple Formats: Supports JPEG, PNG, and other PIL-compatible formats
  • Global CDN: Optional CloudFront distribution for fast image delivery
  • REST API: Optional upload endpoint via API Gateway
  • SAM Framework: Simplified serverless deployment
  • Cross-Platform: Works on any OS without Docker

Prerequisites

  1. Install SAM CLI:

    # macOS
    brew install aws-sam-cli
    
    # Or follow: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html
  2. Install AWS CLI:

    # macOS
    brew install awscli
  3. Configure AWS Credentials:

    aws configure

Quick Start

  1. Deploy Infrastructure:

    chmod +x *.sh
    ./deploy.sh

    The script will prompt for:

    • Project name (default: serverless-image-resizer)
    • AWS region (default: us-east-1)
    • CloudFront CDN (y/n)
    • API Gateway (y/n)
  2. Note the outputs after deployment for bucket names and URLs.

  3. Test the system:

    # Upload a test image
    aws s3 cp sample-images/your-image.jpg s3://YOUR-INPUT-BUCKET/
    
    # Check results
    ./check-results.sh

Usage

Method 1: Upload via AWS CLI

# Get your input bucket name from deployment output
INPUT_BUCKET="your-input-bucket-name"

# Upload an image
aws s3 cp sample-images/your-image.jpg s3://$INPUT_BUCKET/

# Check resized images in output bucket
OUTPUT_BUCKET="your-output-bucket-name"
aws s3 ls s3://$OUTPUT_BUCKET/resized/ --recursive

Method 2: Upload via API Gateway (if enabled)

# Get API endpoint from deployment output
API_ENDPOINT="your-api-gateway-url"

# Upload image via API
curl -X POST "$API_ENDPOINT?key=test-image.jpg" \
     --data-binary @sample-images/your-image.jpg \
     --header "Content-Type: image/jpeg"

Method 3: Upload via AWS Console

  1. Go to S3 in AWS Console
  2. Find your input bucket
  3. Upload images directly through the web interface

Accessing Resized Images

Direct S3 URLs

# Replace with your output bucket name
OUTPUT_BUCKET="your-output-bucket-name"

# Access resized images
https://s3.amazonaws.com/$OUTPUT_BUCKET/resized/thumbnail/your-image.jpg
https://s3.amazonaws.com/$OUTPUT_BUCKET/resized/medium/your-image.jpg
https://s3.amazonaws.com/$OUTPUT_BUCKET/resized/large/your-image.jpg

CloudFront URLs (if enabled)

# Replace with your CloudFront domain from deployment output
CLOUDFRONT_URL="your-cloudfront-domain"

# Access via CDN (faster, cached globally)
https://$CLOUDFRONT_URL/resized/thumbnail/your-image.jpg
https://$CLOUDFRONT_URL/resized/medium/your-image.jpg
https://$CLOUDFRONT_URL/resized/large/your-image.jpg

Project Structure

├── template.yaml          # SAM template
├── src/
│   └── lambda_function.py  # Lambda function code
├── sample-images/          # Test images directory
├── deploy.sh              # Full deployment script
├── update-lambda.sh       # Quick Lambda code updates
├── check-results.sh       # Check resized images
├── cleanup.sh             # Cleanup script
└── README.md              # This file

Testing

  1. Add test images to sample-images/ directory
  2. Upload using any method above
  3. Check results:
    ./check-results.sh
  4. Verify sizes: thumbnail (150x150), medium (500x500), large (1200x1200)

Quick Results Check

After uploading images, use the results checker:

# Check if resized images were created
./check-results.sh

This will:

  • List all resized images in output bucket
  • Show direct S3 access URLs
  • Confirm processing worked correctly

Configuration

Modify src/lambda_function.py to customize:

  • Output sizes
  • Image quality
  • Supported formats
  • Processing logic

Quick Lambda Updates

For code-only changes to src/lambda_function.py:

# Fast update (no infrastructure changes)
./update-lambda.sh

Use this instead of full deployment when:

  • Modifying Lambda code only
  • Testing function logic
  • Avoiding SAM build time

For infrastructure changes, use ./deploy.sh

Monitoring

  • CloudWatch Logs: Check Lambda function logs
  • S3 Events: Monitor bucket notifications
  • Lambda Metrics: View function performance

Cleanup

⚠️ Important: Run cleanup to avoid ongoing AWS charges

chmod +x cleanup.sh
./cleanup.sh

This will:

  • Empty S3 buckets
  • Delete CloudFormation stack
  • Remove all AWS resources
  • Clean up local files

Cost Estimation

  • Lambda: ~$0.20 per 1M requests
  • S3: ~$0.023 per GB storage
  • CloudFront: ~$0.085 per GB transfer (if enabled)
  • API Gateway: ~$3.50 per 1M requests (if enabled)

Troubleshooting

Common Issues

  1. "No module named PIL": Layer not installed correctly

    • Re-run ./deploy.sh
  2. "Access Denied": Check AWS credentials

    • Run aws configure
  3. "Stack already exists": Use cleanup first

    • Run ./cleanup.sh then ./deploy.sh

Logs

# View Lambda logs
aws logs describe-log-groups --log-group-name-prefix /aws/lambda/serverless-image-resizer

# View specific log stream
aws logs get-log-events --log-group-name /aws/lambda/your-function-name --log-stream-name LATEST

Development Workflow

  1. Initial deployment: ./deploy.sh
  2. Test uploads: Use AWS CLI, API Gateway, or Console
  3. Check results: ./check-results.sh
  4. Modify Lambda code: Edit src/lambda_function.py
  5. Quick update: ./update-lambda.sh
  6. Test again: Upload and check results
  7. Infrastructure changes: ./deploy.sh
  8. Cleanup when done: ./cleanup.sh

Use Cases

  • Social Media: Profile picture resizing
  • E-commerce: Product image thumbnails
  • Portfolios: Performance optimization
  • Content Management: Automatic image processing
  • Mobile Apps: Multiple resolution support

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Shell 78.8%
  • Python 21.2%