Deploy Cloud Run services and jobs with Eventarc
Deploy an authenticated Cloud Run service, compare it with a job and functions, and tune identity, concurrency, revisions and timeouts.
By Invicly Tech, , in Google Cloud Engineering and Services. About 9 minutes to read.
In short
Cloud Run runs containers without servers to manage. A service answers requests and scales with traffic, even down to zero. A job runs a container to completion, such as a batch task. Eventarc can start a service when something happens, like a file upload.
Think of it like this: A Cloud Run service is a shop that opens extra counters when queues grow. A job is a delivery crew that does one task and goes home.
Explained simply
Cloud Run runs your container for you. You hand Google a container image, and it gives you a web address. You do not create servers or patch an operating system.
When requests come in, Cloud Run starts as many copies as it needs. When nobody calls, it can shrink to zero copies, so you pay nothing for idle time. The trade-off is a short wait, called a cold start, for the first request after a quiet period.
Not all work is a web request. A Cloud Run job runs a container once and stops when the work is done. Good examples are a nightly report or a data clean-up. A job has no web address.
Eventarc connects events to services. For example, when a file lands in a Cloud Storage bucket, Eventarc can call your service. Every piece uses a service account, so you decide exactly who may call whom.
Press Play on a diagram to walk through it one step at a time.
- Listens on a port for HTTP requests
- Scales on traffic
- Has a stable URL
- Needs invoker permission if private
- Runs tasks to completion
- No URL, no port
- Started by schedule, API or command
- Can retry failed tasks
- Source code, not a container you build
- Runs on Cloud Run
- Triggered by HTTP or events
- Good for small glue code
- Cloud Storage to Eventarc trigger: Object finalized event1Object finalized event
- Eventarc trigger to Cloud Run service: POST CloudEvent with trigger service account2POST CloudEvent with trigger service account
- Cloud Run service to Cloud Storage: Read the new file3Read the new file
- Cloud Run service to Eventarc trigger: 2xx response means delivered42xx response means delivered
- 1Deploy new image
Creates a new revision
- 2Send small traffic share
For example 10 percent
- 3Watch errors and latency
Compare revisions
- 4Promote or roll back
Move traffic to 100 or back
- Back to step 1
In depth
An authenticated service
Deploy with --no-allow-unauthenticated so that Google checks every call. Callers need the Cloud Run Invoker role (roles/run.invoker) on the service and must send an identity token in the Authorization header. A person can get one with gcloud auth print-identity-token. Another service gets one from its own service account. Without the token, the caller sees a 403 Forbidden or 401 error, not your app's response. This keeps internal APIs off the open internet without extra gateways.
- Grant run.invoker to the smallest set of callers.
- Never use allow-unauthenticated for internal tools.
- Test with curl and an identity token.
Runtime identity
The runtime service account is the identity your code uses when it calls Google APIs, such as reading a secret or writing to a bucket. Set it with --service-account. If you skip it, Cloud Run uses the default Compute Engine service account, which can be too powerful. Create one per service. Do not confuse it with the invoker: invokers call your service; the runtime account is what your service uses to call others.
- Who may call the service
- Role: roles/run.invoker
- Sends an identity token
- Set on the service's IAM
- Who the service acts as
- Set with --service-account
- Roles grant API access
- Used with the metadata server
Concurrency, timeouts and minimum instances
Concurrency is how many requests an instance handles at once; the default is 80 and the maximum is 1000. Lower it for code that is not thread-safe or uses a lot of memory per request. The request timeout default is 300 seconds and can be raised to 3600. Minimum instances keep copies warm, which avoids cold starts for latency-sensitive apps but costs money while idle. Maximum instances cap cost and protect downstream databases. Check the current defaults and limits on the Cloud Run docs before relying on them.
- Higher concurrency means fewer instances and lower cost.
- Min instances above 0 removes most cold starts.
- Max instances protects a small database.
Jobs and Eventarc triggers
A job has tasks. Each task is one container run, and tasks can run in parallel. You set the number of tasks, the retry count and a per-task timeout. Run it with gcloud run jobs execute, from Cloud Scheduler, or from a workflow. For events, an Eventarc trigger has a filter (the event type and its details), a destination (your service) and a service account that is allowed to invoke it. The trigger service account needs run.invoker on the destination service, and some event sources need extra roles that the docs list.
Learn it in 5 short steps
About 40m each. Track them with a timer in your plan.
- 1Deploy an authenticated service~40m
- 2Set the runtime service account~40m
- 3Tune concurrency, timeouts and min instances~40m
- 4Run a Cloud Run job~40m
- 5Trigger a service with Eventarc~40m
Key points
- Deploy private by default: no unauthenticated access, and grant roles/run.invoker to callers.
- Set a runtime service account so the container has only the access it needs.
- Tune concurrency (requests per instance), request timeout and minimum instances for latency and cost.
- Every deploy makes a new immutable revision; you can split traffic between revisions.
- Eventarc triggers deliver events to a service using its own service account.
Key terms
Tap a card to flip it. 0/5 known
Examples
gcloud run deploy hello --image=us-docker.pkg.dev/cloudrun/container/hello \
--region=us-central1 --no-allow-unauthenticated \
--service-account=hello-sa@<project-id>.iam.gserviceaccount.com
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
https://<service-url>
gcloud run jobs create nightly --image=<image> --region=us-central1 --tasks=1
gcloud run jobs execute nightly --region=us-central1 --waitDeploy a private service, call it with an identity token, and run a job. Names are placeholders.
Deploy a private service and call it
Use Google's sample hello image so you need no build. Deploy privately, then call it with and without a token. The call without a token returns 403, and with a token returns the page. Cost is near zero when idle. Cleanup: gcloud run services delete hello.
gcloud run deploy hello --image=us-docker.pkg.dev/cloudrun/container/hello \
--region=us-central1 --no-allow-unauthenticated
URL=$(gcloud run services describe hello --region=us-central1 --format='value(status.url)')
curl -s -o /dev/null -w '%{http_code}\n' "$URL"
curl -s -o /dev/null -w '%{http_code}\n' \
-H "Authorization: Bearer $(gcloud auth print-identity-token)" "$URL"Trigger a service from a bucket upload
Create an Eventarc trigger that calls the service when a file is finalized in a bucket. Use a trigger service account with run.invoker on the service and the eventarc.eventReceiver role. Evidence is a log entry for each uploaded file. Rollback: delete the trigger. Check the docs for the exact filter and extra roles for your event source.
gcloud eventarc triggers create upload-trigger --location=us-central1 \
--destination-run-service=hello --destination-run-region=us-central1 \
--event-filters="type=google.cloud.storage.object.v1.finalized" \
--event-filters="bucket=<bucket-name>" \
--service-account=trigger-sa@<project-id>.iam.gserviceaccount.comQuick reference
- Private deploy: gcloud run deploy NAME --image IMG --no-allow-unauthenticated.
- Caller role: roles/run.invoker; header: Authorization Bearer plus an identity token.
- Identity token: gcloud auth print-identity-token.
- Runtime identity: --service-account; separate from the invoker.
- Concurrency default 80, request timeout default 300 seconds.
- Jobs: gcloud run jobs create, then gcloud run jobs execute --wait.
- Each deploy makes an immutable revision; split traffic with --to-revisions.
- Min instances above 0 cost money while idle.
Common mistakes
- Using --allow-unauthenticated for an internal API just to make testing easier, then leaving it.
- Setting minimum instances above zero everywhere and paying for idle capacity.
- Forgetting that a container must listen on the port in the PORT variable and start quickly.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write the command to deploy the hello sample image as a private service in us-central1.
- Exercise 2medium
Let one service account call the private service, then say how that caller proves who it is.
- Exercise 3hard
Design a safe rollout of a new revision and describe how you would roll it back.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is Cloud Run only for web apps?
- No. Services handle requests, but jobs run batch work and functions handle small event code on the same platform.
- What is a cold start?
- It is the extra delay when Cloud Run must start a new instance. Minimum instances reduce it.
- Cloud Run service or Cloud Run job for a scheduled task?
- Use a job when the work runs to completion, and trigger it on a schedule. Use a service when something must call it over HTTP.