Functions are the least code needed to react to events, such as processing every uploaded file.
Cloud Run functions (formerly Cloud Functions; the 2nd-generation runtime) let you deploy just a function. Google builds it into a container and runs it on Cloud Run, so you get the same concurrency, scaling and IAM model.
Two signatures, via the Functions Framework for your language:
- HTTP functions receive a request and return a response.
- CloudEvent functions receive an event, such as an object finalized in a bucket or a Pub/Sub message. Event delivery uses Eventarc, which supports many Google sources and also Cloud Audit Log events.
Deploy with gcloud functions deploy (or gcloud run deploy --function). Event functions should be idempotent: delivery is at-least-once, and with --retry failures are retried until success or the event expires. A bug that always throws will retry for a long time, so log and swallow permanent errors.
gcloud functions deploy make-thumbnail --gen2 --region=europe-west1 \
--runtime=python313 --source=. --entry-point=on_upload \
--trigger-bucket=shop-dev-4821-uploads \
--service-account=thumbs@shop-dev-4821.iam.gserviceaccount.comChoose a function for small, event-driven glue. When you need several routes, custom system packages or full control of the container, deploy a regular Cloud Run service instead.
Example
# main.py (requirements.txt: functions-framework)
import functions_framework
@functions_framework.http
def hello(request):
name = request.args.get("name", "world")
return f"Hello, {name}!" # GET /?name=Ada -> Hello, Ada!
@functions_framework.cloud_event
def on_upload(cloud_event):
data = cloud_event.data # Cloud Storage object metadata
if not data["name"].endswith(".jpg"):
return # ignore, do not raise: avoids retries
print(f"thumbnail for gs://{data['bucket']}/{data['name']}")
# Local test: functions-framework --target=hello --port=8080Remember
- Cloud Run functions = your function built and run on Cloud Run
- HTTP vs CloudEvent signatures; events come through Eventarc
- Event handlers must be idempotent and not raise on permanent errors
Watch out for
- Writing a thumbnail into the same bucket that triggers the function, creating an infinite loop.