Skip to main content

AWS Lambda Template Processor

It is an AWS Lambda function that processes Base64-encoded ZIP files and automatically uploads them to an Amazon S3 bucket.

Description

This Lambda function is designed to receive Base64-encoded ZIP files through HTTP events, extract their content, and upload each individual file to the specified S3 bucket. It is ideal for processing templates or compressed files in an automated way.

Features

  • Processing of Base64-encoded ZIP files
  • Automatic extraction of files from ZIP
  • Individual upload of files to Amazon S3 maintaining the original folder structure
  • Error handling
  • Structured JSON responses
  • Input validation

Architecture

HTTP Request (Base64 ZIP) → AWS Lambda → Extract ZIP → Upload to S3

Project Structure

├── upload_template.py    # Main Lambda function
├── config.py # AWS S3 configuration
├── build_response.py # Utility to build HTTP responses
└── README.md # Project documentation

Configuration

Required Environment Variables

VariableDescriptionExample
S3_BUCKET_NAMEName of the S3 bucket where files will be uploadedbucket-templates

Required IAM Permissions

The Lambda function needs the following permissions:

{
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
],
"Resource": "arn:aws:s3:::BUCKET_NAME/*"
}
]
}

Input Format

Required ZIP File Structure

For template insertion to work correctly, the ZIP file must follow this specific hierarchy, so it can be properly integrated with the email sending flow:

my_zip.zip
├── 📁 welcome-email/
│ ├── 📄 index.html
│ ├── 📁 images/
│ │ ├── header.png
│ │ ├── footer.png
│ │ └── logo.png
├── 📁 newsletter/
├── 📄 index.html
└── 📁 images/
└── banner.jpg

Important rules:

  • Each template must be in its own folder with the name index.html
  • Resource files (images) must be in subfolders
  • Keep .zip files under 1MB

Request Body

The function expects an event with the following structure:

{
"isBase64Encoded": true,
"body": "UEsDBAoAAAAAAI..."
}
  • isBase64Encoded: Must be true
  • body: Base64-encoded ZIP file

Testing with Postman

Postman Configuration

  1. Method: POST

  2. URL: The API Gateway endpoint

  3. Headers:

    Content-Type: application/zip
  4. Body:

    • Select binary
    • Choose your .zip file directly
  5. Send the request

Example Usage with cURL

# Send ZIP file directly
curl -X POST https://your-api-gateway-url \
-H "Content-Type: application/zip" \
--data-binary @my_file.zip

Response Format

Successful Response (200)

{
"statusCode": 200,
"body": "{\"status\": \"success\", \"message\": \"ZIP file processed and uploaded successfully.\"}"
}

Error Response (400)

{
"statusCode": 400,
"body": "{\"status\": \"error\", \"message\": \"Request body must be Base64 encoded.\"}"
}

Error Response (500)

{
"statusCode": 500,
"body": "{\"status\": \"error\", \"message\": \"Error processing ZIP file\"}"
}

Verify Results in S3

After executing the function, verify in your S3 bucket that the files have been uploaded correctly maintaining the folder structure:

s3://your-bucket/
├── welcome-email/
│ ├── index.html
│ └── images/
│ ├── header.png
│ ├── footer.png
│ └── logo.png
└── newsletter/
├── template.html
└── assets/
└── banner.jpg

🔍 Monitoring and Logs

CloudWatch Logs

Function logs are available in CloudWatch under:

/aws/lambda/Osborn-TemplateProcessor

Important Metrics

  • Invocations: Number of times the function is executed
  • Duration: Average execution time
  • Errors: Number of errors per minute
  • Throttles: Number of throttles per minute

Limitations and Considerations

  • Maximum size: 10MB for input payload (API Gateway limit), currently a maximum of 1MB will be maintained for testing and performance purposes
  • Timeout: Configure appropriate timeout according to file size
  • Memory: Adjust memory according to ZIP complexity
  • Concurrency: Consider concurrency limits for large files