Skip to content
Back to Cloud Services

GCP Cloud Functions

Event-driven serverless functions — HTTP, Cloud Storage, Pub/Sub, Firestore triggers.

GCPIntermediate

Why

Cloud Functions run on events without servers — HTTP, Storage, Pub/Sub, Firestore. 2M invocations/mo free. Auto-scales, pay per 100ms.

Gen 2 uses Cloud Run underneath.

When

Use for event-driven: webhooks, file processing, DB triggers, scheduled tasks. Use Cloud Run for containers, App Engine for web apps.

How

1. Deploy — gcloud functions deploy myFn --runtime nodejs20 --trigger-http 2. HTTP trigger — public URL, returns response 3. Event triggers — --trigger-bucket, --trigger-topic, --trigger-firestore 4. Gen 2 — --gen2, uses Cloud Run, more features (concurrency, timeout) 5. Env vars — --set-env-vars KEY=VAL 6. Secrets — Secret Manager, mount as env 7. Timeout — max 60min (Gen 2), 9min (Gen 1) 8. Memory — 128MB-8GB, affects cost

Example

gcloud functions deploy processUpload \ --runtime nodejs20 --gen2 \ --trigger-bucket my-bucket \ --entry-point processUpload \ --set-env-vars BUCKET=processed \ --memory 512MB --timeout 120s // index.js export const processUpload = async (event) => { const { bucket, name } = event.data; console.log(`Processing ${name} from ${bucket}`); // resize image, write to processed bucket };

Common Problems

  • ⚠Gen 1 vs Gen 2 — Gen 2 is Cloud Run, more features, prefer for new
  • ⚠Cold start 1-3s — use min-instances (Gen 2) for latency
  • ⚠60min timeout max — for longer, use Cloud Run job or Cloud Composer

Best Practices

  • ✓Use Gen 2 — Cloud Run underneath, more features, better perf
  • ✓Set min-instances for prod — avoids cold start, costs more but stable
  • ✓Use Secret Manager — not env vars for secrets, IAM-controlled

FAQ

What is GCP Cloud Functions and what problem does it solve?

Event-driven serverless functions — HTTP, Cloud Storage, Pub/Sub, Firestore triggers. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with GCP Cloud Functions in 5 minutes?

Two steps to run it: (1) Deploy — gcloud functions deploy myFn --runtime nodejs20 --trigger-http; (2) HTTP trigger — public URL, returns response. Validate on the free tier first, then wire into production.

What is the #1 pitfall with GCP Cloud Functions?

The most frequent issue is: Gen 1 vs Gen 2 — Gen 2 is Cloud Run, more features, prefer for new. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for GCP Cloud Functions?

Best practices for GCP Cloud Functions include: Use Gen 2 — Cloud Run underneath, more features, better perf; Set min-instances for prod — avoids cold start, costs more but stable; Use Secret Manager — not env vars for secrets, IAM-controlled. The first one — "Use Gen 2 — Cloud Run underneath, more features, better perf" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with GCP Cloud Functions?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most GCP Cloud Functions options offer a free tier — validate within free quota before scaling.