How hemanth-yamsani/serverless-image-processing-gcp works
Purpose and entry points
This repository provisions a fully serverless GCP image-processing pipeline with Terraform. Deployment enters through the root Terraform configuration (providers.tf, backend.tf, variables.tf); runtime entry points are the two Python Cloud Functions: src/main.py and src-notification/main.py.
Infrastructure composition
Terraform creates the storage, messaging, functions, triggers, and required platform enablement:
• GCS input/output buckets and object-finalize notification: gcs-buckets.tf
• Upload and resize-success Pub/Sub topics: pubsub.tf
• Required GCP APIs and dependency timing: apis.tf, time-delay.tf
• Gen2 function deployments and Eventarc wiring: cloud-function.tf, notification-function.tf
Source deployment artifacts are represented by CreateThumbnail.zip and CreateNotification.zip, with corresponding unpacked source directories.
Ingestion and processing flow
An image upload enters the input Cloud Storage bucket. Its OBJECT_FINALIZE notification publishes to the upload-event Pub/Sub topic. Eventarc delivers that event to the image-resizing Gen2 function configured in cloud-function.tf.
The processing function in src/main.py decodes the incoming CloudEvent/Pub/Sub payload, downloads the referenced GCS object, uses Pillow to resize it to 50%, writes the derived image to the output bucket, then publishes a success JSON event. Python dependencies, including image-processing/runtime libraries, are defined in src/requirements.txt.
Notification flow
The success topic separates image processing from downstream notification concerns. The notification Gen2 function deployment is defined in notification-function.tf; its runtime handler is src-notification/main.py.
This function decodes the resize-success message and emits a structured Cloud Logging record. Its dependencies are isolated in src-notification/requirements.txt. This boundary allows new delivery channels such as webhooks or chat/email integrations without coupling them to the resize function.
Security and execution identities
Both functions run under a dedicated service account rather than a default runtime identity. Account creation and least-privilege access bindings are defined in iam-service-account.tf and iam-policy-bindings.tf, covering GCS read/write, Pub/Sub publishing, and logging. Platform service-agent permissions needed by Cloud Functions, Eventarc, and related managed services are handled in service-agent-iam.tf.
Runtime and external services
Material runtime is Python 3.11 on Cloud Functions Gen2, backed by Cloud Run revisions. External managed services are Google Cloud Storage, Pub/Sub, Eventarc, Cloud Logging, Cloud Build/function deployment infrastructure, IAM, and Terraform’s Google provider. Project, region, bucket names, and topic names are parameterized through variables.tf and the deployer-supplied terraform.tfvars derived from terraform.tfvars.example.
Main entry points: cloud-function.tf Resize function deployment · providers.tf Terraform root · notification-function.tf Notification deployment · iam-service-account.tf Function service account
Components 19
Terraform deployment
Serverless GCP resources and permissions.
- Terraform root · Configures provider, backend, variables, and deployment inputs.
providers.tf - GCP API enablement · Platform APIs and dependency delay.
apis.tf - GCS buckets · Input and derived-image output buckets.
gcs-buckets.tf - Function service account · Dedicated least-privilege runtime identity.
iam-service-account.tf - Runtime IAM bindings · Grants storage, Pub/Sub publishing, and logging access.
iam-policy-bindings.tf - Service-agent IAM · Permissions for Cloud Functions, Eventarc, and managed services.
service-agent-iam.tf - Cloud Run revisions · Execution substrate for both functions.
Image processing
Upload-to-thumbnail runtime path.
- Upload event topic
pubsub.tf - Eventarc trigger · Routes upload events to the resize function.
cloud-function.tf - Resize function deployment · Deploys the image-processing function and its trigger.
cloud-function.tf - Resize handler · Decodes events, resizes images with Pillow, and publishes success.
src/main.py - Resize dependencies
src/requirements.txt - Resize artifact
CreateThumbnail.zip
Notifications
Post-processing event consumer.
- Resize success topic · Decouples processing from notification delivery.
pubsub.tf - Notification deployment · Deploys the success-event consumer.
notification-function.tf - Notification handler · Logs structured resize-success records.
src-notification/main.py - Notification dependencies
src-notification/requirements.txt - Notification artifact
CreateNotification.zip - Cloud Logging
Connections 24
- Terraform root → GCP API enablement: enables
- Terraform root → GCS buckets: provisions
- Terraform root → Upload event topic: provisions
- Terraform root → Resize success topic: provisions
- Terraform root → Function service account: creates
- Function service account → Runtime IAM bindings: receives access
- GCP API enablement → Service-agent IAM: managed services require
- GCS buckets → Upload event topic: OBJECT_FINALIZE publishes
- Upload event topic → Eventarc trigger: upload event
- Eventarc trigger → Resize function deployment: invokes
- Resize artifact → Resize function deployment: deployment source
- Resize handler → Resize artifact: packaged as
- Resize dependencies → Resize handler: runtime libraries
- Resize function deployment → Cloud Run revisions: runs on
- Resize function deployment → Function service account: executes as
- Resize function deployment → GCS buckets: reads input / writes output
- Resize function deployment → Resize success topic: publishes success
- Resize success topic → Notification deployment: triggers
- Notification artifact → Notification deployment: deployment source
- Notification handler → Notification artifact: packaged as
- Notification dependencies → Notification handler: runtime libraries
- Notification deployment → Cloud Run revisions: runs on
- Notification deployment → Function service account: executes as
- Notification deployment → Cloud Logging: writes structured record