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
| Trigger | What Fires It | Gen 2 Support |
|---|---|---|
| HTTP | Direct web request | ✅ |
| Cloud Storage | File created/deleted/archived | ✅ |
| Pub/Sub | Message published to a topic | ✅ |
| Firestore | Document created/updated/deleted | ✅ |
| Eventarc | Any supported Google Cloud event | ✅ (2nd gen only) |
| Cloud Scheduler | Cron-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.txtlean — 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
| Feature | 1st Gen | 2nd Gen |
|---|---|---|
| Timeout | 9 minutes | 60 minutes |
| Memory | Up to 8 GB | Up to 32 GB |
| Concurrency | 1 per instance | Up to 1,000 |
| vCPU | Up to 2 | Up to 8 |
| Traffic splitting | No | Yes |
| Event sources | Limited | 90+ 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.
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.