Python Google Cloud Functions — Core Concepts

What Google Cloud Functions Does

Google Cloud Functions (GCF) is Google’s serverless compute product for running individual functions in response to events. You deploy a Python function, Google manages the underlying infrastructure, and you pay per invocation plus compute time.

GCF currently supports two generations: 1st gen (the original, simpler model) and 2nd gen (built on Cloud Run, with more features). New projects should use 2nd gen.

Writing a Cloud Function

An HTTP-triggered function is a standard Python function that accepts a Flask Request object:

import functions_framework

@functions_framework.http
def hello(request):
    name = request.args.get("name", "World")
    return f"Hello, {name}!"

An event-triggered function uses the CloudEvents standard:

import functions_framework
from cloudevents.http import CloudEvent

@functions_framework.cloud_event
def process_storage(cloud_event: CloudEvent):
    data = cloud_event.data
    bucket = data["bucket"]
    filename = data["name"]
    print(f"New file: gs://{bucket}/{filename}")

The functions-framework library runs locally too, so you can test without deploying.

Trigger Types

TriggerWhat Fires ItGen 2 Support
HTTPDirect web request
Cloud StorageFile created/deleted/archived
Pub/SubMessage published to a topic
FirestoreDocument created/updated/deleted
EventarcAny supported Google Cloud event✅ (2nd gen only)
Cloud SchedulerCron-like scheduled jobs✅ (via Pub/Sub or HTTP)

2nd gen uses Eventarc as the universal event routing layer, which means you can trigger functions from 90+ Google Cloud event sources.

Project Layout

my-function/
├── main.py              # entry point (function lives here)
├── requirements.txt     # Python dependencies
└── tests/
    └── test_main.py     # unit tests

Deployment:

gcloud functions deploy hello \
    --gen2 \
    --runtime python312 \
    --trigger-http \
    --allow-unauthenticated \
    --region us-central1

How Scaling Works

GCF scales by creating new instances of your function. Each instance handles one request at a time (1st gen) or up to 1,000 concurrent requests (2nd gen, since it’s backed by Cloud Run).

Key settings:

  • Min instances — keep warm instances ready (eliminates cold starts, costs more)
  • Max instances — cap scaling to control costs and protect downstream services
  • Concurrency (2nd gen only) — how many requests one instance handles simultaneously

Cold Starts

When a new instance starts, Google needs to load the runtime and your dependencies. For Python, this typically adds 500ms–3 seconds. Strategies to reduce it:

  • Keep requirements.txt lean — every extra package adds import time
  • Use min instances (even 1 eliminates cold starts for low-traffic functions)
  • Lazy-import heavy libraries inside the function body
  • 2nd gen’s concurrency means fewer new instances need to start

1st Gen vs 2nd Gen

Feature1st Gen2nd Gen
Timeout9 minutes60 minutes
MemoryUp to 8 GBUp to 32 GB
Concurrency1 per instanceUp to 1,000
vCPUUp to 2Up to 8
Traffic splittingNoYes
Event sourcesLimited90+ via Eventarc

Common Misconception

“Cloud Functions can only handle simple, tiny tasks.”

With 2nd gen, functions can run for up to 60 minutes with 32 GB of memory and 8 vCPUs. That’s enough for serious data processing, ML inference, and complex workflows. The old limitations of 1st gen created this perception, but 2nd gen is a genuinely capable compute platform.

The one thing to remember: Google Cloud Functions 2nd gen combines the simplicity of writing a single Python function with the power of Cloud Run’s infrastructure — giving you both ease of use and production-grade scaling.

pythongcpserverless

See Also

  • Python Ansible Python Learn Ansible Python with a clear mental model so your Python code is easier to trust and maintain.
  • Python Aws Boto3 Learn AWS Boto3 with a clear mental model so your Python code is easier to trust and maintain.
  • Python Aws Dynamodb Python Learn AWS Dynamodb Python with a clear mental model so your Python code is easier to trust and maintain.
  • Python Aws Lambda Python Learn AWS Lambda Python with a clear mental model so your Python code is easier to trust and maintain.
  • Python Aws Lambda Use AWS Lambda with Python to remove setup chaos so Python projects stay predictable for every teammate.