π Purpose
This Lambda function is triggered when an image is uploaded to a specific path in an S3 bucket (ss-au-bank-preprod/smartsell/pages_1/). It checks if the image is in Non-RGB color space and, if so, converts it to RGB, uploads it back to S3 (overwriting the original), and invalidates the CloudFront CDN cache for the updated file.
- β Detect Non-RGB images using PIL (Pillow)
- β Convert Non-RGB images to RGB
- β Re-upload image to S3 at the same key a
- β Invalidate CloudFront cache
- β
Record
conversionTimeSecandisRGBProcessedas an S3 tag - β Send Slack alerts on failure with relevant metadata
βοΈ Environment Variables
| Variable | Description |
|---|---|
CDN_BASE_URL |
Base URL of the CDN (used for Slack notifications) |
CLOUDFRONT_DISTRIBUTION_ID |
CloudFront distribution ID for cache invalidation |
TARGET_BUCKET_NAME |
S3 bucket name for triggering lambda |
π Secrets
| Secret Name | Description |
|---|---|
SLACK_CMYKTORGB_ALERT_API_TOKEN = {slack_api_token:βtoken_valueβ} |
|
Slack Bot OAuth token to send alerts to Slack channel #Non-RGBtorgb-alerts |
π¨ Failure Scenarios & Slack Alerts
| Scenario | Slack Alert Triggered | Slack Message Preview |
|---|---|---|
File not in smartsell/pages_1/ |
β No | No alert β file ignored silently |
File is already tagged isRGBProcessed: true |
β No | No alert β file considered already processed |
| File retry count >= 3 while image conversion | β Yes | Max retries reached for file: {filename} |
| S3 download failure | β Yes | Error while downloading file from S3: {filename} |
| Image is invalid or corrupted (Pillow error) | β Yes | Library related issues occurred while processing file: {filename} |
| Image is not Non-RGB (e.g., already RGB) | β No | No alert β image skipped as it's not in Non-RGB mode |
| Conversion succeeded | β No | No alert β success |
| Upload back to S3 fails | β Yes | Failed to upload RGB image to S3: {filename} |
| Tagging fails | β Yes | Failed to tag image on S3: {filename} |
| CloudFront invalidation fails | β Yes | CloudFront invalidation failed for: {cdn_url} |
| Any unexpected runtime exception | β Yes | {exception error message} (dynamically included) |
Slack message example:
Non-RGB to RGB Conversion Failed
Original File: abc123.pdf
CDN URL: https://cdn.example.com/smartsell/pages_1/abc123.pdf
Retries: 3
Error: Max retries reached
System Error: Reason for the error
-
β Assumes your S3 bucket and CloudFront distribution are already set up.
-
A. Create a Lambda Function
- Go to AWS Lambda β Create function β Author from scratch.
- Assign a name, e.g.,
Non-RGB-to-rgb-converter. - Choose Python 3.9+ as the runtime.
- Upload the zipped deployment package or deploy via SAM/Serverless Framework.
- Add Pillow and Requests dependencies as layers
-
B. Create & Attach an IAM Role to Lambda
- You will need to create an IAM Role and attach it to the Lambda function with these permissions:
S3 Access:
- Permission to:
- Get objects
- Put objects
- Get and put object tags
- Only for files under:
your-bucket-name/smartsell/pages_1/
CloudFront Access:
- Permission to create cache invalidations
Secrets Manager Access:
- Permission to read one secret:
- Name:
SLACK_Non-RGBTORGB_ALERT_API_TOKEN
- Name:
In Lambda β Configuration β Environment variables, add:
Key Value TARGET_BUCKET_NAMEyour S3 bucket name CDN_BASE_URLyour CloudFront URL (e.g., https://cdn.example.com)CLOUDFRONT_DISTRIBUTION_IDYour CloudFront distribution ID -
-
Go to Secrets Manager > Store a new secret.
-
Choose Other type of secret.
-
Set the key-value pair:
{ "slack_api_token": "xoxb-XXXXXXXXXX" }Name it exactly:
SLACK_CMYKTORGB_ALERT_API_TOKEN
- Go to S3 > Your Bucket > Properties > Event notifications
- Add a new event notification:
- Event type:
PUT(ObjectCreated) - Prefix filter:
smartsell/pages_1/ - Destination: Lambda β Select your function
- Event type:
- View logs in CloudWatch Logs β Linked from the Lambda function page.
- Failures trigger a Slack message to
#cmyktorgb-alertswith:- File name
- CDN URL
- Retry count
- Error message