Anleitung und Anforderungen für Lab-Einrichtung
Schützen Sie Ihr Konto und Ihren Fortschritt. Verwenden Sie immer den privaten Modus und Lab-Anmeldedaten, um dieses Lab auszuführen.

AHYBRID-132: Receive Pub/Sub events with Eventarc and Cloud Run

Lab 1 Stunde 30 Minuten universal_currency_alt 5 Guthabenpunkte show_chart Mittelstufe
info Dieses Lab kann KI-Tools enthalten, die den Lernprozess unterstützen.
Dieser Inhalt ist noch nicht für Mobilgeräte optimiert.
Die Lernumgebung funktioniert am besten, wenn Sie auf einem Computer über einen per E‑Mail gesendeten Link darauf zugreifen.

Overview

In this exercise, you deploy a Cloud Run service and use Eventarc to receive events from Pub/Sub. In the second part of the lab, you do the same on an Anthos GKE cluster with the Cloud Run for Anthos offering.

Objectives

In this lab, you learn how to:

  • Deploy an event receiver service to Cloud Run and Cloud Run for Anthos.
  • Create an event trigger with Eventarc.
  • Publish a message to a Pub/Sub topic to generate an event, and view it in the Cloud Run and Cloud Run for Anthos logs.

Setup and requirements

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Sign in to Google Skills using an incognito window.

  2. Note the lab's access time (for example, 1:15:00), and make sure you can finish within that time. There is no pause feature. You can restart if needed, but you have to start at the beginning.

  3. When ready, click Start lab.

  4. Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.

  5. Click Open Google Console.

  6. Click Use another account and copy/paste credentials for this lab into the prompts. If you use other credentials, you'll receive errors or incur charges.

  7. Accept the terms and skip the recovery resource page.

Task 1. Review your Anthos GKE cluster and install Cloud Run for Anthos

In this task, you review and prepare the already created Anthos GKE cluster to execute Cloud Run for Anthos. First, you verify that the GKE cluster has been registered in an Anthos Fleet. Second, you confirm that Anthos Service Mesh has been installed in the cluster as a prerequisite to installing Cloud Run for Anthos. Third, you enable and install Cloud Run for Anthos in the cluster.

  1. In the Google Cloud Console, on the Navigation menu (Navigation menu icon), click Kubernetes Engine > Clusters. Notice that a new GKE cluster has been created.

  2. Click Workloads, and verify that the cluster is running the Anthos Service Mesh components istio-ingressgateway and istiod-asm.

  3. On the Navigation menu, click Anthos > Clusters, and then verify that the cluster has been registered and appears in the list of Anthos managed clusters.

  4. Click Activate Cloud Shell (Activate Cloud Shell icon). If prompted click Continue.

  5. In Cloud Shell, set the Zone environment variable:

    C1_ZONE={{{ project_0.default_zone| "Zone added at lab start" }}}
  6. In Cloud Shell, initialize the environment variables:

export PROJECT_ID=$(gcloud config get-value project) export C1_NAME="gke" gcloud config set run/region us-central1 gcloud config set run/platform managed gcloud config set eventarc/location us-central1
  1. Get the credentials for your gke GKE cluster:
gcloud container clusters get-credentials $C1_NAME --zone $C1_ZONE --project $PROJECT_ID
  1. Enable Cloud Run for Anthos on your project:
gcloud container fleet cloudrun enable --project=$PROJECT_ID
  1. Enable the Eventarc APIs:
gcloud services enable --project=$PROJECT_ID eventarc.googleapis.com
  1. Install Cloud Run for Anthos on your cluster:
gcloud container fleet cloudrun apply --gke-cluster=$C1_ZONE/$C1_NAME

If this step fails, wait 30 seconds and try again.

Note: Enabling Cloud Run for Anthos takes several minutes; continue with the next task. Later, you review whether the installation has been successful.

Task 2. Deploy a Cloud Run application

  1. In Cloud Shell, clone the repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/eventarc/pubsub/
  1. Build the container and upload it to Cloud Build:
gcloud builds submit --tag gcr.io/$(gcloud config get-value project)/events-pubsub
  1. Deploy the container image to Cloud Run:
gcloud run deploy helloworld-events-pubsub-tutorial \ --image gcr.io/$(gcloud config get-value project)/events-pubsub \ --allow-unauthenticated \ --max-instances=1
  1. If asked to enable the API, type y.

When the service URL is displayed, the deployment is complete.

Task 3. Create an Eventarc trigger for Cloud Run

When a message is published to the Pub/Sub topic, the event trigger sends messages to the event receiver service deployed on Cloud Run.

  1. In Cloud Shell, create a trigger to listen for Pub/Sub messages:
gcloud eventarc triggers create events-pubsub-trigger \ --destination-run-service=helloworld-events-pubsub-tutorial \ --destination-run-region=us-central1 \ --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished"

This creates a new Pub/Sub topic and a trigger for it called events-pubsub-trigger. The Pub/Sub subscription persists regardless of activity and does not expire.

  1. Confirm that the trigger was successfully created:
gcloud eventarc triggers list --location=us-central1
  1. Find and set the Pub/Sub topic as an environment variable:
export RUN_TOPIC=$(gcloud eventarc triggers describe events-pubsub-trigger \ --format='value(transport.pubsub.topic)')
  1. Send a message to the Pub/Sub topic to generate an event:
gcloud pubsub topics publish $RUN_TOPIC --message "Runner"

The event is sent to the Cloud Run service, which logs the event message.

  1. To view the event message, in the Google Cloud Console, navigate to Cloud Run.

  2. Click on the helloworld-events-pubsub-tutorial service.

  3. On the Logs tab, look for the "Hello, Runner!" message.

Congratulations! You deployed a Cloud Run application and used Eventarc to trigger events from Pub/Sub.

Task 4. Prepare the environment for Eventarc and Cloud Run for Anthos

  1. Create a service account to use when creating triggers:
TRIGGER_SA=pubsub-to-anthos-trigger gcloud iam service-accounts create $TRIGGER_SA
  1. Grant appropriate roles to the new service account:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member "serviceAccount:${TRIGGER_SA}@${PROJECT_ID}.iam.gserviceaccount.com" \ --role "roles/pubsub.subscriber" gcloud projects add-iam-policy-binding $PROJECT_ID \ --member "serviceAccount:${TRIGGER_SA}@${PROJECT_ID}.iam.gserviceaccount.com" \ --role "roles/monitoring.metricWriter"
  1. Enable GKE destinations for Eventarc:
gcloud eventarc gke-destinations init
  1. At the prompt to bind the required roles, type y.

The following roles are bound:

  • roles/compute.viewer
  • roles/container.developer
  • roles/iam.serviceAccountAdmin

Task 5. Deploy the Cloud Run for Anthos application

  • Deploy the container image to Cloud Run for Anthos:

    gcloud run deploy subscriber-service \ --cluster $C1_NAME \ --cluster-location $C1_ZONE \ --platform gke \ --image gcr.io/$(gcloud config get-value project)/events-pubsub

    If the command fails, the Anthos GKE cluster is not yet ready to accept Cloud Run for Anthos services. Wait a couple of minutes and try again.

Task 6. Create an Eventarc trigger for Cloud Run on Anthos

  1. In Cloud Shell, create a trigger to listen for Pub/Sub messages:
gcloud eventarc triggers create pubsub-trigger \ --location=us-central1 \ --destination-gke-cluster=$C1_NAME \ --destination-gke-location=$C1_ZONE \ --destination-gke-namespace=default \ --destination-gke-service=subscriber-service \ --destination-gke-path=/ \ --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \ --service-account=${TRIGGER_SA}@${PROJECT_ID}.iam.gserviceaccount.com

This creates a new Pub/Sub topic and a trigger for it called pubsub-trigger. The Pub/Sub subscription persists regardless of activity and does not expire.

  1. Confirm that the trigger was successfully created:
gcloud eventarc triggers list --location=us-central1
  1. Find and set the Pub/Sub topic as an environment variable:
export RUN_TOPIC=$(gcloud eventarc triggers describe pubsub-trigger \ --location=us-central1 \ --format='value(transport.pubsub.topic)')
  1. Send a message to the Pub/Sub topic to generate an event:
gcloud pubsub topics publish $RUN_TOPIC --message "Cloud Run on Anthos"

The event is sent to the Cloud Run for Anthos service, which logs the event message.

  1. To view the event message in the service logs, on the Navigation menu, click Kubernetes Engine > Applications.

  2. Click on Go to List of Services.

  3. Click on the subscriber-service.

  4. On the Logs tab, look for the "Hello, Cloud Run on Anthos!" message.

Congratulations! You deployed a Cloud Run for Anthos application and used Eventarct to trigger events from Pub/Sub.

End your lab

When you have completed your lab, click End Lab. Google Skills removes the resources you’ve used and cleans the account for you.

You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

The number of stars indicates the following:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

You can close the dialog box if you don't want to provide feedback.

For feedback, suggestions, or corrections, please use the Support tab.

Copyright 2026 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Vorbereitung

  1. Labs erstellen ein Google Cloud-Projekt und Ressourcen für einen bestimmten Zeitraum
  2. Labs haben ein Zeitlimit und keine Pausenfunktion. Wenn Sie das Lab beenden, müssen Sie von vorne beginnen.
  3. Klicken Sie links oben auf dem Bildschirm auf Lab starten, um zu beginnen

Privates Surfen verwenden

  1. Kopieren Sie den bereitgestellten Nutzernamen und das Passwort für das Lab
  2. Klicken Sie im privaten Modus auf Konsole öffnen

In der Konsole anmelden

  1. Melden Sie sich mit Ihren Lab-Anmeldedaten an. Wenn Sie andere Anmeldedaten verwenden, kann dies zu Fehlern führen oder es fallen Kosten an.
  2. Akzeptieren Sie die Nutzungsbedingungen und überspringen Sie die Seite zur Wiederherstellung der Ressourcen
  3. Klicken Sie erst auf Lab beenden, wenn Sie das Lab abgeschlossen haben oder es neu starten möchten. Andernfalls werden Ihre bisherige Arbeit und das Projekt gelöscht.

Diese Inhalte sind derzeit nicht verfügbar

Bei Verfügbarkeit des Labs benachrichtigen wir Sie per E-Mail

Sehr gut!

Bei Verfügbarkeit kontaktieren wir Sie per E-Mail

Es ist immer nur ein Lab möglich

Bestätigen Sie, dass Sie alle vorhandenen Labs beenden und dieses Lab starten möchten

Privates Surfen für das Lab verwenden

Am besten führen Sie dieses Lab in einem Inkognito- oder privaten Browserfenster aus. So vermeiden Sie Konflikte zwischen Ihrem privaten Konto und dem Teilnehmerkonto, die zusätzliche Kosten für Ihr privates Konto verursachen könnten.