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.
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 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
- Node.js 17 or higher
- Docker and Docker Compose
- AWS CLI configured with appropriate credentials (for deployment)
- AWS SAM CLI (for deployment)
# Install dependencies npm install # Run tests npm test # Start local development server npm startThis project includes Docker support for both local development and AWS Lambda deployment.
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 upTo 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.batThis will build and run the Lambda container, exposing it at: http://localhost:9000/2015-03-31/functions/function/invocations
The project includes two Dockerfiles:
- Dockerfile.lambda - Uses the AWS Lambda base image for compatibility with the Lambda runtime
- Dockerfile.dev - Standard Node.js image for local development
-
Start the full stack:
npm run docker:compose -
Test the API:
curl -X POST http://localhost:3000/process-image \ -H "Content-Type: application/json" \ -d '{"bucket":"test-images","key":"sample-image.jpg"}' -
Access LocalStack S3:
aws --endpoint-url=http://localhost:4566 s3 ls s3://test-images/ -
Run tests in Docker:
npm run docker:test
-
Build the Docker image:
sam build --use-container -
Deploy to AWS:
sam deploy --guided
-
Build the Docker image:
docker build -t pic-processor-lambda -f Dockerfile.lambda . -
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 -
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
This project implements multiple layers of protection against DDoS attacks and abuse:
- AWS WAF Integration
- API Gateway Throttling
- API Key Authentication
- Lambda Concurrency Limits
- Application-Level Rate Limiting
- Input Validation
For more details, see the "DDoS Protection and Rate Limiting" section in the full documentation.
The Lambda function is exposed through an API Gateway endpoint that accepts POST requests to the /process-image path.
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"] } } }The service provides a comprehensive set of image processing capabilities:
- 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
- Grayscale: Convert images to black and white
- Blur: Apply Gaussian blur effect with adjustable intensity
- 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
{ "bucket": "your-bucket", "key": "image.jpg", "options": { "resize": true, "width": 800, "height": 600 } }{ "bucket": "your-bucket", "key": "image.jpg", "options": { "format": "webp", "quality": 85 } }{ "bucket": "your-bucket", "key": "image.jpg", "options": { "convertToPdf": true, "pdfOptions": { "title": "My Document", "author": "Image Processor", "subject": "Converted Image", "keywords": ["image", "pdf", "converted"] } } }- 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
- 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
ISC