Google Cloud Tutorial · Compute · Lesson 13 of 27
Cloud Run Functions and Eventarc
Event-driven code on Google Cloud: Cloud Run functions, Eventarc triggers, Pub/Sub, Cloud Scheduler and Cloud Tasks.
Cloud Run functions
Cloud Run functions (formerly Cloud Functions) let you deploy a single function in Python, Node.js, Go, Java and other languages; Google builds the container and runs it on Cloud Run. Use them for small event handlers and webhooks.
Reacting to events
- Eventarc routes events to Cloud Run services or functions: an object created in a Cloud Storage bucket, a message on a Pub/Sub topic, an audit-logged API call, or events from partners, in the CloudEvents format.
- Pub/Sub fans messages out to many subscribers.
- Cloud Scheduler triggers jobs on a cron schedule.
- Cloud Tasks queues calls to one target with rate limits and retries.
- Workflows chains steps and API calls into a process.
gcloud functions deploy make-thumbnail --gen2 --runtime python312 --region europe-west1 \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=acme-uploads" --entry-point handle
Events can be delivered more than once, so handlers must be idempotent.
processed = set()
def make_thumbnail(event):
key = (event["bucket"], event["name"], event["generation"])
if key in processed:
return f"skip duplicate {event['name']}"
processed.add(key)
return f"thumbnail for {event['name']}"
deliveries = [
{"bucket": "acme-uploads", "name": "cat.jpg", "generation": 1},
{"bucket": "acme-uploads", "name": "dog.jpg", "generation": 1},
{"bucket": "acme-uploads", "name": "cat.jpg", "generation": 1}, # redelivered
{"bucket": "acme-uploads", "name": "cat.jpg", "generation": 2}, # file replaced
]
for e in deliveries:
print(make_thumbnail(e))
Output
thumbnail for cat.jpg thumbnail for dog.jpg skip duplicate cat.jpg thumbnail for cat.jpg
Test yourself
Which service would you use to call an email provider's API at most 10 times per second, with retries?
Pub/SubCloud TasksBigQueryCloud Storage
Show answer
Answer: Cloud Tasks
Cloud Tasks delivers tasks to one target with rate limits and retry policies.