Planning and implementing a cloud solution · Topic 4 of 9
Serverless and Event-Driven Deployments
Deploying to Cloud Run and Cloud Run functions, and processing Pub/Sub, Cloud Storage and other Google Cloud events with Eventarc, for the ACE exam.
- 4 min
- Includes a diagram
- Cloud Run: services (requests) and jobs (run to completion); each deploy creates a revision.
- Cloud Run functions (previously Cloud Functions):
gcloud functions deploywith HTTP, bucket or topic triggers. - Eventarc routes events from Pub/Sub, Cloud Storage, audit logs and Google services to targets.
What it is
Serverless platforms run your code without servers to manage and scale automatically, including to zero. Cloud Run runs containers; Cloud Run functions runs single-purpose functions (previously named Cloud Functions). Both can react to events, such as a Pub/Sub message or a new object in Cloud Storage, using Eventarc or built-in triggers.
How it works
Deploying to Cloud Run
# deploy a container image
gcloud run deploy orders --image=us-docker.pkg.dev/PROJECT/repo/orders:1.0 \
--region=us-central1 --allow-unauthenticated
# or build from source code in the current folder
gcloud run deploy orders --source=. --region=us-central1
| Cloud Run concept | Meaning |
|---|---|
| Service | Long-running, request-driven container with an HTTPS URL |
| Job | Runs a container to completion, for batch tasks |
| Revision | An immutable snapshot of a service's code and configuration; each deploy creates one |
| Concurrency | How many requests one instance handles at once |
| Min and max instances | Keep warm instances to avoid cold starts; cap scale and cost |
| Authentication | --allow-unauthenticated for public services; otherwise callers need roles/run.invoker |
Deploying Cloud Run functions
# HTTP function
gcloud functions deploy hello --gen2 --runtime=python312 --region=us-central1 \
--source=. --entry-point=hello --trigger-http
# function triggered by a new object in a bucket
gcloud functions deploy thumbnail --gen2 --runtime=python312 --region=us-central1 \
--source=. --entry-point=make_thumbnail --trigger-bucket=uploads-bucket
# function triggered by Pub/Sub messages
gcloud functions deploy process --gen2 --runtime=python312 --region=us-central1 \
--source=. --entry-point=process --trigger-topic=orders
Functions deployed this way run on Cloud Run infrastructure, so they share its scaling and revisions.
Processing events with Eventarc
Eventarc routes events from sources to targets such as Cloud Run services:
| Event source | Example |
|---|---|
| Pub/Sub | Messages published to a topic |
| Cloud Storage | Object finalized (created), deleted, archived or metadata updated |
| Cloud Audit Logs | Almost any Google Cloud API action, such as a VM being created |
| Direct events from Google services | For example Firestore document changes |
gcloud eventarc triggers create new-upload \
--location=us-central1 --destination-run-service=thumbnailer \
--destination-run-region=us-central1 \
--event-filters="type=google.cloud.storage.object.v1.finalized" \
--event-filters="bucket=uploads-bucket" \
--service-account=eventarc-sa@PROJECT.iam.gserviceaccount.com
The trigger's service account needs permission to invoke the target (such as roles/run.invoker) and to receive events.
"Run code whenever a file is uploaded to a bucket" is a Cloud Storage object finalized event, through Eventarc or a function's bucket trigger.
Pub/Sub push and pull
Pub/Sub can also push messages to an HTTPS endpoint such as a Cloud Run service, or subscribers can pull them. Push suits serverless targets; pull suits workers that control their own pace.
A worked scenario
A photo app stores uploads in a bucket. An Eventarc trigger on the object finalized event invokes a Cloud Run service that creates thumbnails. Order messages on a Pub/Sub topic trigger a Cloud Run function that writes to Firestore. The public API runs as a Cloud Run service with --min-instances=1 to avoid cold starts, and a nightly cleanup runs as a Cloud Run job.
How the exam asks about this
- "Deploy a stateless container with no infrastructure to manage."
gcloud run deploy. - "Process each new file uploaded to a bucket." A Cloud Storage finalized event with Eventarc or a function bucket trigger.
- "React when someone creates a VM." An Eventarc trigger on Cloud Audit Logs.
- "Avoid cold start latency." Set minimum instances.
- "Only authenticated callers may invoke the service." Do not allow unauthenticated; grant
roles/run.invoker.
What to remember for the exam
- Cloud Run: services (requests) and jobs (run to completion); each deploy creates a revision.
- Cloud Run functions (previously Cloud Functions):
gcloud functions deploywith HTTP, bucket or topic triggers. - Eventarc routes events from Pub/Sub, Cloud Storage, audit logs and Google services to targets.
roles/run.invokercontrols who can call a private service.- Min instances reduce cold starts; max instances cap cost.
Serverless instances are ephemeral. Do not store state on the instance's file system; use a database or Cloud Storage.
Common mistakes
Choosing a VM to process occasional events. Serverless scales to zero and costs nothing when idle.
Forgetting the trigger's service account permissions. Eventarc needs to invoke the target.
Using a service for batch work that runs to completion. Cloud Run jobs fit better.
Check yourself
Which Cloud Run resource runs a container until a task completes?
A Cloud Run job.
Which event type fires when a new object is written to a bucket?
The Cloud Storage object finalized event.
Which role allows a caller to invoke a private Cloud Run service?
roles/run.invoker.
Topic 4 of 9 in Planning and implementing a cloud solution
Test yourself on Planning and implementing a cloud solution
The full Google Cloud Associate Cloud Engineer question bank has 661 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Google Cloud. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.