Skip to content

AWS Lambda function for image processing and PDF conversion with DDoS protection and Docker support

License

Notifications You must be signed in to change notification settings

chrdek/PicProcessor

Repository files navigation

Image Processor Lambda Function 🖼️

Generated with Amazon Q 🤖 🦆

This project contains an AWS Lambda function built with Node.js 17+ that processes images from an S3 bucket and returns them through API Gateway. The implementation includes robust DDoS protection, rate limiting, PDF conversion, and Docker support for local development and testing.

Project Structure

PicProcessor/ ├── src/ # Source code for the Lambda function │ ├── index.js # Main Lambda handler with rate limiting │ ├── imageProcessor.js # Image processing logic │ ├── pdfConverter.js # PDF conversion functionality │ └── local.js # Local development server ├── tests/ # Unit tests using Mocha and Chai │ ├── index.test.js # Tests for the Lambda handler │ ├── imageProcessor.test.js # Tests for the image processor │ └── pdfConverter.test.js # Tests for the PDF converter ├── localstack/ # LocalStack initialization scripts │ └── init-s3.sh # Script to initialize S3 bucket ├── Dockerfile.lambda # Dockerfile for AWS Lambda deployment ├── Dockerfile.dev # Dockerfile for local development ├── docker-compose.yml # Docker Compose configuration ├── docker-run.sh # Shell script to run Lambda in Docker (Unix) ├── docker-run.bat # Batch script to run Lambda in Docker (Windows) ├── .dockerignore # Files to exclude from Docker build ├── template.yaml # AWS SAM template with DDoS protection ├── .eslintrc.js # ESLint configuration ├── .gitignore # Git ignore file ├── .mocharc.js # Mocha configuration ├── package.json # Node.js dependencies and scripts └── README.md # This file 

Coding Style and Standards

This project follows these coding standards and best practices:

  • ECMAScript 2022: Utilizes modern JavaScript features available in Node.js 17+
  • Modular Architecture: Separates concerns between Lambda handler, image processing, and local development
  • Error Handling: Comprehensive error handling with appropriate HTTP status codes
  • Input Validation: Strict validation of all input parameters
  • Security First: Multiple layers of protection against DDoS and other attacks
  • Testing: Comprehensive unit tests with Mocha and Chai
  • Docker Best Practices: Multi-stage builds and minimal container sizes
  • AWS Best Practices: Follows AWS Lambda and API Gateway best practices

Prerequisites

  • Node.js 17 or higher
  • Docker and Docker Compose
  • AWS CLI configured with appropriate credentials (for deployment)
  • AWS SAM CLI (for deployment)

Installation

# Install dependencies npm install # Run tests npm test # Start local development server npm start

Docker Setup

This project includes Docker support for both local development and AWS Lambda deployment.

Running with Docker Compose

The easiest way to run the entire stack locally:

# Start the entire stack (Lambda, API Gateway, S3) npm run docker:compose # Or directly with docker-compose docker-compose up

Running Lambda Function Only

To run just the Lambda function in a container:

# Build and run with npm scripts npm run docker:build npm run docker:run # Or use the convenience scripts # On Unix/Linux/macOS ./docker-run.sh # On Windows docker-run.bat

This will build and run the Lambda container, exposing it at: http://localhost:9000/2015-03-31/functions/function/invocations

Docker Images

The project includes two Dockerfiles:

  1. Dockerfile.lambda - Uses the AWS Lambda base image for compatibility with the Lambda runtime
  2. Dockerfile.dev - Standard Node.js image for local development

Local Development with Docker

  1. Start the full stack:

    npm run docker:compose 
  2. Test the API:

    curl -X POST http://localhost:3000/process-image \ -H "Content-Type: application/json" \ -d '{"bucket":"test-images","key":"sample-image.jpg"}' 
  3. Access LocalStack S3:

    aws --endpoint-url=http://localhost:4566 s3 ls s3://test-images/ 
  4. Run tests in Docker:

    npm run docker:test 

Deploying to AWS

Using AWS SAM with Docker

  1. Build the Docker image:

    sam build --use-container 
  2. Deploy to AWS:

    sam deploy --guided 

Manual Deployment with Docker

  1. Build the Docker image:

    docker build -t pic-processor-lambda -f Dockerfile.lambda . 
  2. Push to Amazon ECR:

    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com docker tag pic-processor-lambda:latest YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/pic-processor-lambda:latest docker push YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/pic-processor-lambda:latest 
  3. Create Lambda function from container image:

    aws lambda create-function \ --function-name pic-processor \ --package-type Image \ --code ImageUri=YOUR_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/pic-processor-lambda:latest \ --role YOUR_LAMBDA_EXECUTION_ROLE_ARN 

DDoS Protection and Rate Limiting

This project implements multiple layers of protection against DDoS attacks and abuse:

  1. AWS WAF Integration
  2. API Gateway Throttling
  3. API Key Authentication
  4. Lambda Concurrency Limits
  5. Application-Level Rate Limiting
  6. Input Validation

For more details, see the "DDoS Protection and Rate Limiting" section in the full documentation.

Usage

API Endpoint

The Lambda function is exposed through an API Gateway endpoint that accepts POST requests to the /process-image path.

Request Format

Send a POST request to the API endpoint with your API key and a JSON body containing:

{ "bucket": "your-s3-bucket-name", "key": "path/to/image.jpg", "options": { "resize": true, "width": 500, "height": 300, "format": "webp", "quality": 90, "grayscale": true, "blur": false, "convertToPdf": false, "pdfOptions": { "title": "My Image", "author": "Image Processor", "subject": "Converted Image", "keywords": ["image", "converted"] } } }

Image Processing Features

The service provides a comprehensive set of image processing capabilities:

Basic Image Transformations

  • Resize: Change image dimensions while maintaining aspect ratio
  • Format Conversion: Convert between formats (JPEG, PNG, WebP, AVIF, etc.)
  • Quality Adjustment: Compress images by adjusting quality level

Image Enhancement

  • Grayscale: Convert images to black and white
  • Blur: Apply Gaussian blur effect with adjustable intensity

PDF Conversion

  • Image to PDF: Convert any image to a PDF document
  • PDF Metadata: Add title, author, subject, and keywords to generated PDFs
  • Custom Page Size: PDF pages automatically sized to fit the image dimensions

Example: Basic Image Resizing

{ "bucket": "your-bucket", "key": "image.jpg", "options": { "resize": true, "width": 800, "height": 600 } }

Example: Convert to WebP with Quality Setting

{ "bucket": "your-bucket", "key": "image.jpg", "options": { "format": "webp", "quality": 85 } }

Example: Convert Image to PDF

{ "bucket": "your-bucket", "key": "image.jpg", "options": { "convertToPdf": true, "pdfOptions": { "title": "My Document", "author": "Image Processor", "subject": "Converted Image", "keywords": ["image", "pdf", "converted"] } } }

Dependencies

Production Dependencies

  • aws-sdk: AWS SDK for JavaScript
  • sharp: High-performance image processing library that handles resizing, format conversion, and image manipulation
  • pdf-lib: Pure JavaScript PDF generation and manipulation library
  • express: Web framework for local development server
  • cors: Cross-origin resource sharing middleware
  • body-parser: Request body parsing middleware
  • multer: Middleware for handling multipart/form-data
  • uuid: For generating unique identifiers

Development Dependencies

  • mocha: JavaScript test framework
  • chai: BDD/TDD assertion library
  • sinon: Test spies, stubs and mocks
  • eslint: JavaScript linter
  • supertest: HTTP assertion library
  • nodemon: Auto-restart during development
  • dotenv: Environment variable management
  • aws-sam-cli: AWS SAM CLI for local testing

License

ISC

About

AWS Lambda function for image processing and PDF conversion with DDoS protection and Docker support

Topics

Resources

License

Stars

Watchers

Forks