Skip to content

NucleusEngineering/cloud-run-gcs-interceptor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloud Run GCS Image Resizing Proxy

A high-performance, secure, and cost-effective Go-based image resizing proxy designed to run on Google Cloud Run. It intercepts requests for resized images (e.g., image_128x128.jpg), resizes them on-the-fly from original images in Google Cloud Storage (GCS), and caches the result in-place in the same GCS bucket and via Cloud CDN.

Architecture

graph TD
    User([User / CDN Client]) -->|HTTP GET| CDN[Google Cloud CDN]
    CDN -->|Cache Hit| User
    CDN -->|Cache Miss| LB[External Application Load Balancer]
    LB -->|Serverless NEG| CR[Cloud Run Service]
    CR -->|Read / Write| GCS[(GCS Bucket: Original & Resized Images)]
Loading

Features

  • On-the-Fly Resizing: Automatically resizes images to supported square dimensions (64, 128, 256, 512).
  • Supported Formats: Handles jpg, jpeg, and png formats.
  • In-Place Caching: Resized images are saved back to the original GCS bucket under the requested path. Subsequent requests are served directly from GCS/CDN without invoking the resizing logic.
  • Optimized for Cloud CDN: Seamlessly integrates with Google Cloud CDN to serve cached images at the edge, reducing latency and compute costs.
  • Secure Distroless Container: Built using a multi-stage Docker build and runs on Google's minimal distroless/static image, reducing the attack surface.

Local Development

Prerequisites

  • Go 1.25+ installed locally.
  • Google Cloud SDK (gcloud CLI) installed and authenticated.

Running the Server Locally

  1. Authenticate your local machine to obtain Application Default Credentials (ADC):

    gcloud auth application-default login
  2. Run the Go application by specifying the target GCS bucket and your GCP project:

    ORIGINAL_BUCKET="your-gcs-bucket-name" GOOGLE_CLOUD_PROJECT="your-gcp-project-id" go run .
  3. Test it by opening your browser or using curl:

    • Original image: http://localhost:8080/image.jpg
    • Resized image: http://localhost:8080/image_128x128.jpg

Testing

Unit Tests

Run the unit tests to verify the parsing and resizing logic:

go test -v .

Load Testing

A k6 load testing script is included in load-test.js to simulate realistic traffic (75% cache hits, 20% resizes, 5% 404s).

You can run it using Docker:

docker run --rm -i -e TARGET_HOST="http://<YOUR_LOAD_BALANCER_IP>" grafana/k6 run - <load-test.js

Deployment to Google Cloud

The project includes an automated deployment script deploy.sh that:

  1. Enables necessary GCP APIs (Cloud Run, Cloud Build, Compute Engine, Artifact Registry).
  2. Creates an Artifact Registry repository.
  3. Builds and pushes the Docker container using Google Cloud Build.
  4. Provisions the GCS Bucket, Cloud Run service, Serverless NEG, Cloud CDN, and Load Balancer via Terraform.

Triggering the Deployment

Run the script by specifying your target GCP project ID:

GCP_PROJECT="your-gcp-project-id" ./deploy.sh

Cost Estimation

For a detailed analysis of monthly costs at 100 Million requests per month (approximately $5.30 per million requests at a 95% CDN cache hit rate), see the cost_estimation.md file.

About

Intercept requests to GCS via LB, checks whether resized image exists, if not, resizes and responds 200OK

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors