A lightweight, serverless AWS Lambda function for uploading files directly to Amazon S3 with CloudFront signed URL generation. This project provides a simple and efficient solution for handling file uploads in serverless architectures.
File uploads are a common requirement in modern web applications, but implementing a secure, scalable solution can be complex. This project was created to:
- Simplify file uploads: Provide a ready-to-use Lambda function that handles file uploads without the need for complex server infrastructure
- Leverage serverless architecture: Take advantage of AWS Lambda's scalability and cost-effectiveness for handling file uploads
- Generate secure URLs: Automatically create CloudFront signed URLs for secure file access
- Reduce boilerplate: Save development time by providing a production-ready solution that can be easily integrated into existing projects
Whether you're building a new application or need to add file upload functionality to an existing one, this project offers a straightforward, maintainable solution that follows AWS best practices.
- 📤 Direct S3 Upload: Upload files directly to Amazon S3 buckets
- 🔒 Secure Access: Generate CloudFront signed URLs for secure file access
- 🚀 Serverless: Built on AWS Lambda for automatic scaling and cost efficiency
- 📝 Multipart Support: Handles multipart/form-data file uploads
- ⚡ Lightweight: Minimal dependencies and fast execution
- 🔧 Configurable: Easy configuration through environment variables
Before using this Lambda function, ensure you have:
- An AWS account with appropriate permissions
- An S3 bucket configured for file storage
- A CloudFront distribution (optional, for signed URLs)
- AWS Lambda runtime environment (Node.js 18.x or later recommended)
- AWS credentials with permissions for:
- S3:
PutObject,PutObjectAcl - CloudFront: Access to generate signed URLs (if using CloudFront)
- S3:
- Clone this repository:
git clone https://github.com/yourusername/aws-lambda-file-upload.git
cd aws-lambda-file-upload- Install dependencies:
npm installConfigure the Lambda function using environment variables:
| Variable | Description | Required |
|---|---|---|
REACT_APP_ACCESS_KEY_ID |
AWS Access Key ID | Yes |
REACT_APP_SECRET_ACCESS_KEY |
AWS Secret Access Key | Yes |
REACT_APP_S3_BUCKET_NAME |
Name of the S3 bucket | Yes |
REACT_APP_CLOUDFRONT_ACCESS_KEY_ID |
CloudFront Key Pair ID | Yes (if using CloudFront) |
REACT_APP_CLOUDFRONT_PRIVATE_KEY_STRING |
CloudFront Private Key (base64 encoded) | Yes (if using CloudFront) |
REACT_APP_CLOUD_FRONT_ORIGIN_PATH |
CloudFront distribution URL | Yes (if using CloudFront) |
- Go to your Lambda function in the AWS Console
- Navigate to Configuration → Environment variables
- Add the required variables listed above
The function expects an event with the following structure:
{
headers: {
'content-type': 'multipart/form-data; boundary=...'
},
body: 'base64-encoded-file-data'
}// Example using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
const response = await fetch('your-api-gateway-endpoint', {
method: 'POST',
body: formData
});
const result = await response.json();
console.log('File URL:', result);The function returns a CloudFront signed URL:
"https://your-cloudfront-domain.com/1234567890"- Package your function:
zip -r function.zip index.js node_modules/- Deploy to Lambda:
aws lambda update-function-code \
--function-name your-function-name \
--zip-file fileb://function.zip# serverless.yml
service: aws-lambda-file-upload
provider:
name: aws
runtime: nodejs18.x
environment:
REACT_APP_ACCESS_KEY_ID: ${env:AWS_ACCESS_KEY_ID}
REACT_APP_SECRET_ACCESS_KEY: ${env:AWS_SECRET_ACCESS_KEY}
REACT_APP_S3_BUCKET_NAME: ${env:S3_BUCKET_NAME}
# ... other environment variables
functions:
upload:
handler: index.handler
events:
- http:
path: upload
method: postMain Lambda handler function that processes file uploads.
Parameters:
event(Object): Lambda event object containing:headers(Object): Request headers withcontent-typebody(String): Base64-encoded file data
Returns:
Promise<String>: CloudFront signed URL of the uploaded file
Throws:
- Error if S3 upload fails
- Error if file extraction fails
Contributions are welcome! This project is open source and we encourage community participation.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow existing code style and conventions
- Add comments for complex logic
- Update documentation for new features
- Ensure backward compatibility when possible
- Test your changes thoroughly
This project is open source and available under the MIT License.
Developer
- Email: devpuffer0807@gmail.com
- Telegram: @devpuffer0807
- Built with AWS SDK
- Uses parse-multipart for multipart form parsing
- CloudFront URL signing powered by aws-cloudfront-sign
⭐ If you find this project helpful, please consider giving it a star!