Skip to content
Visual explainer 3h 30m to learn 5 steps

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.

Topic 11 of 32

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.

CompareService, job and function
Service
  • Listens on a port for HTTP requests
  • Scales on traffic
  • Has a stable URL
  • Needs invoker permission if private
Job
  • Runs tasks to completion
  • No URL, no port
  • Started by schedule, API or command
  • Can retry failed tasks
Cloud Run functions
  • Source code, not a container you build
  • Runs on Cloud Run
  • Triggered by HTTP or events
  • Good for small glue code
SequenceEventarc starts a service when a file is uploaded
Cloud Storage
Eventarc trigger
Cloud Run service
  1. 1Object finalized event
    Cloud Storage to Eventarc trigger: Object finalized event
  2. 2POST CloudEvent with trigger service account
    Eventarc trigger to Cloud Run service: POST CloudEvent with trigger service account
  3. 3Read the new file
    Cloud Run service to Cloud Storage: Read the new file
  4. 42xx response means delivered
    Cloud Run service to Eventarc trigger: 2xx response means delivered
CycleRevision lifecycle for a service
  1. 1
    Deploy new image

    Creates a new revision

  2. 2
    Send small traffic share

    For example 10 percent

  3. 3
    Watch errors and latency

    Compare revisions

  4. 4
    Promote or roll back

    Move traffic to 100 or back

  5. 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.

CompareTwo identities, two directions
Invoker (inbound)
  • Who may call the service
  • Role: roles/run.invoker
  • Sends an identity token
  • Set on the service's IAM
Runtime account (outbound)
  • 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.

  1. 1
    Deploy an authenticated service~40m
  2. 2
    Set the runtime service account~40m
  3. 3
    Tune concurrency, timeouts and min instances~40m
  4. 4
    Run a Cloud Run job~40m
  5. 5
    Trigger 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

bash
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 --wait

Deploy 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.

bash
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.

bash
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.com

Quick 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.

Question 1

1A private service returns 403 to your curl. What is missing?

Question 2

2Which Cloud Run resource fits a nightly batch task with no web address?

Question 3

3What does raising minimum instances above zero do?

Question 4

4Which identity does your code use to call Cloud Storage from a service?

Question 5

5What does an Eventarc trigger need in order to call a private service?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write the command to deploy the hello sample image as a private service in us-central1.

  2. Exercise 2medium

    Let one service account call the private service, then say how that caller proves who it is.

  3. 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.

Go deeper

ShareXLinkedInWhatsApp