Quickstart: Events for Cloud Run for Anthos

This page shows you how to set up Events for Cloud Run for Anthos and receive events from Cloud Storage.

In this quickstart, you:

  1. Create a GKE cluster and enable Cloud Run for Anthos.
  2. Initialize Events for Cloud Run for Anthos.
  3. Create an events broker to route events from the source to receiver.
  4. Deploy a Cloud Run for Anthos service that receives events.
  5. Set up Cloud Storage to generate events.
  6. Generate and view an event.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Install and initialize the Cloud SDK.
  5. Make sure you are using the latest version of the gcloud command-line tool.
  6. Set gcloud command-line tool to use your GCP project:

    gcloud config set project PROJECT-ID

  7. Install the gcloud command-line tool beta components:

    gcloud components install beta

  8. Enable services used in this quickstart:

    gcloud services enable cloudapis.googleapis.com
    gcloud services enable container.googleapis.com
    gcloud services enable containerregistry.googleapis.com
    gcloud services enable cloudbuild.googleapis.com
  9. Set gcloud command-line tool configuration variables:
    gcloud config set run/cluster events-cluster
    gcloud config set run/cluster_location us-central1
    gcloud config set run/platform gke

  10. Optional: You can check the configuration settings using the gcloud command-line tool by typing:

    gcloud config list

    The output is similar to the following:

    cluster = events-cluster
    cluster_location = us-central1
    platform = gke

Creating a GKE cluster with Cloud Run for Anthos enabled

  1. Create a Google Kubernetes Engine cluster for Cloud Run for Anthos with the CloudRun, HttpLoadBalancing and HorizontalPodAutoscaling addons enabled:

    gcloud beta container clusters create events-cluster \
      --addons=HttpLoadBalancing,HorizontalPodAutoscaling,CloudRun \
      --machine-type=n1-standard-4 \
      --enable-autoscaling --min-nodes=3 --max-nodes=10 \
      --no-issue-client-certificate --num-nodes=3  \
      --enable-stackdriver-kubernetes \
      --scopes=cloud-platform,logging-write,monitoring-write,pubsub \
      --zone us-central1 \
  2. Wait a couple of minutes for the cluster creation to complete. During the creation process, you might see warnings that you can safely ignore. When cluster creation has been completed, the output is similar to the following:

    Creating cluster events-cluster...done.
    Created [https://container.googleapis.com/v1beta1/ \

    where my-project is your project ID. You have just created a new Google Kubernetes Engine cluster named events-cluster in the project my-project.

Initializing Events for Cloud Run for Anthos

To initialize Events for Cloud Run for Anthos.

  1. Initialize Events for Cloud Run for Anthos for Cloud Run for Anthos using the gcloud command-line tool:

    gcloud beta events init --platform gke
    1. When prompted, select events-cluster as the GKE cluster.

    2. Enter Y to all the prompts asking to create service accounts, bind them to specific roles, generating new keys, and enable necessary services.

    This creates pods in two Kubernetes namespaces called cloud-run-events and knative-eventing.

  2. Optional: Check successful pod creation:

    kubectl get pods -n cloud-run-events

    The output is similar to the following:

      NAME                         READY   STATUS    RESTARTS   AGE
      controller-9cc679b67-2952n   1/1     Running   0          22s
      webhook-8576c4cfcb-dhz82     1/1     Running   0          16m

    To check pods knative-eventing namespace, type:

    kubectl get pods -n knative-eventing

    The output is similar to the following:

      NAME                                   READY   STATUS    RESTARTS   AGE
      eventing-controller-77f46f6cf8-kj9ck   1/1     Running   0          17m
      eventing-webhook-5bc787965f-hcmwg      1/1     Running   0          17m

Creating an event broker

In Events for Cloud Run for Anthos, brokers route events from the source to a destination service. In this quickstart you will create a broker that routes events from Cloud Storage to a Cloud Run for Anthos service.

To create a broker:

  1. Create an events namespace:

    kubectl create namespace events
  2. Initialize the namespace with the default secret:

    gcloud beta events namespaces init events \
  3. Create the event broker:

    gcloud beta events brokers create default \
      --namespace events
  4. Optional: After a few minutes, you can check the status of your broker by running:

    kubectl get brokers \
      -n events

Deploying a Cloud Run for Anthos service to receive events

Deploy a Cloud Run for Anthos service that receives events from the broker and logs it.

To deploy the Cloud Run for Anthos service:

  1. Clone the repository:


    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    cd python-docs-samples/eventarc/audit-storage


    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    cd java-docs-samples/eventarc/audit-storage


    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    cd golang-samples/eventarc/audit_storage


    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
    cd nodejs-docs-samples/eventarc/audit-storage


    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
    cd dotnet-docs-samples/eventarc/audit-storage
  2. Build the container and upload it to Cloud Build:

    gcloud builds submit \
     --tag gcr.io/$(gcloud config get-value project)/events-quickstart-container
  3. Deploy the container image to Cloud Run for Anthos:

    gcloud run deploy events-quickstart-receiver \
       --namespace=events \
       --image gcr.io/$(gcloud config get-value project)/events-quickstart-container

    When you see the service URL, it has successfully deployed.

Setting up Cloud Storage

Cloud Storage generates events when there are changes to the storage bucket and send them to the event broker. To set up Cloud Storage:

  1. Create a Cloud Storage bucket using the gsutil command-line tool:

    gsutil mb \
        -p $(gcloud config get-value project) \
        -l us-central1 \
        gs://"events-quickstart-$(gcloud config get-value project)"

    In this example, the Cloud Storage bucket is created in the us-central1 region.

  2. Retrieve the Cloud Storage service account:

    curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      "https://storage.googleapis.com/storage/v1/projects/$(gcloud config get-value project)/serviceAccount"
  3. Give the Cloud Storage service account publish rights to Pub/Sub:

    gcloud projects add-iam-policy-binding $(gcloud config get-value project) \
        --member=serviceAccount:CLOUD-STORAGE-SERVICE-ACCOUNT \
        --role roles/pubsub.publisher

Creating a Cloud Storage trigger

When you create a trigger, you must specify the Cloud Run for Anthos service that receives events from Cloud Storage.

  1. Create the Cloud Storage trigger:

    gcloud beta events triggers create trigger-storage \
      --namespace events \
      --target-service events-quickstart-receiver \
      --type=google.cloud.storage.object.v1.finalized \
      --parameters bucket=events-quickstart-$(gcloud config get-value project)

    This creates a trigger for your events-quickstart-bucket Cloud Storage bucket.

  2. Optional: You can check the status of the trigger by running:

    gcloud beta events triggers list \
  3. Note that while trigger creation is complete and ready, it can take up to 10 minutes for it to propagate and filter Cloud Storage events.

Generating and viewing an event

  1. To generate an event, create a text file with the filename random.txt containing the text "Hello World", and upload it to Cloud Storage using the gsutil command-line tool.

    echo "Hello World" > random.txt
    gsutil cp random.txt gs://events-quickstart-$(gcloud config get-value project)/random.txt

    Your upload generates an event and the Cloud Run for Anthos service logs the event's message.

  2. To view the message in Cloud Logging:

    Go to the Logs Viewer page

Congratulations! You have successfully deployed Events for Cloud Run for Anthos, triggered an event and logged its contents.

Clean up

To stop billing for all resources used within your test Cloud project, delete the project:

  1. In the Cloud Console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next