Skip to content

enparadigm/cmykTorgb-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Œ 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.

βœ… Core Features

  • βœ… 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 conversionTimeSec and isRGBProcessed as 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

βœ… AWS Setup

  • βœ… 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

    C. Set Environment Variables

    In Lambda β†’ Configuration β†’ Environment variables, add:

    Key Value
    TARGET_BUCKET_NAME your S3 bucket name
    CDN_BASE_URL your CloudFront URL (e.g., https://cdn.example.com)
    CLOUDFRONT_DISTRIBUTION_ID Your CloudFront distribution ID

D. Store Slack Token in Secrets Manager

  1. Go to Secrets Manager > Store a new secret.

  2. Choose Other type of secret.

  3. Set the key-value pair:

    {
      "slack_api_token": "xoxb-XXXXXXXXXX"
    }
    

    Name it exactly: SLACK_CMYKTORGB_ALERT_API_TOKEN

πŸ” S3 Event Trigger Setup

  1. Go to S3 > Your Bucket > Properties > Event notifications
  2. Add a new event notification:
    • Event type: PUT (ObjectCreated)
    • Prefix filter: smartsell/pages_1/
    • Destination: Lambda β†’ Select your function

πŸ§ͺ Debugging & Observability

  • View logs in CloudWatch Logs β†’ Linked from the Lambda function page.
  • Failures trigger a Slack message to #cmyktorgb-alerts with:
    • File name
    • CDN URL
    • Retry count
    • Error message

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages