Skip to content

ahiho/terraform-aws-image-resize

Repository files navigation

Image Resizing Terraform Module

Solution Architecture

alt text

Installation

Requirements providers

Name Version
aws >= 5.31.0, < 6.0

Install with a new bucket

  1. Install AWS SAM CLI

Install SAM CLI instructions

  1. Build source code
      make build
    
  2. Provision Instructions: Copy and paste into your Terraform configuration, insert the variables, and run terraform init.
    module "image_resize" {
      source = "git::https://github.com/ahiho/terraform-aws-image-resize.git?ref=v1.0.0"
      --- variables ---
    }
    

Install with an existing bucket

  1. Set create_new_bucket=false
  2. Make sure that your s3 bucket delegates all needed permissions to the s3 access point created by this stack. Example: `
      statement {
      sid    = "AccessPoint"
      effect = "Allow"
      principals {
        type        = "AWS"
        identifiers = ["*"]
      }
      actions = ["*"]
      resources = [<your s3 bucket ARN>]
      condition {
        test     = "StringEquals"
        variable = "s3:DataAccessPointAccount"
        values   = [<your account id>]
      }
    }
    

Usage

Distribution query parameters

Headers Description Valid Values Type Default
t image transformation mode fit, crop string crop
w specifies image width 100..4100 int 640
h specifies image height 100..4100 int 400
o get the original image true, false boolean false
b specifies image blur 0..50 boolean 0
q specifies image quality low, medium, high, best string high
- - l, m, med, h, b - -

Lambda function spec

The Lambda function using these environments

  • IMAGE_BUCKET_REGION (required): The region where the image bucket is located.
  • IMAGE_BUCKET_NAME (required): The name of the image bucket.
  • ROUNDING_VALUE: The rounding value to be used during image resizing.
  • LOG_LEVEL: The log level of the Lambda function. Valid values are DEBUG.
  • MIN_WIDTH: The default min width value
  • MIN_HEIGHT: The default min height value
  • MAX_WIDTH: The default max width value
  • MAX_HEIGHT: The default max width value
  • DEFAULT_WIDTH: The default width value
  • DEFAULT_HEIGHT: The default height value
  • DEFAULT_QUALITY: The default quality value. Valid values are l, m, h, b.
  • DEFAULT_TRANSFORM: The default width value. Valid values are f', c.

The Lambda function follows a structured workflow to process image resizing:

  1. Extracts the S3 URL from the event object.
  2. Retrieves the original image from the S3 URL.
  3. Checks if a resized image already exists. If it does, the returns the resized image; otherwise, it proceeds to the next step.
  4. Applies the requested image transformation (e.g., fit, crop) based on the provided query parameters.
  5. Puts the resized image to S3 bucket.
  6. Returns the resized image.

How to test

  1. Get distribution URL from AWS console. Example https://xxxxxxxxxxxxxx.cloudfront.net
  2. Put image to S3 bucket. Example test.png
  3. Combine distribution URL and image key together. Example https://xxxxxxxxxxxxxx.cloudfront.net/test.png

References