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 on Google Cloud.
  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 on Google Cloud 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 Account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Console, on the project selector page, select or create a Cloud project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm 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:

    [run]
    cluster = events-cluster
    cluster_location = us-central1
    platform = gke
    

Creating a GKE cluster with Cloud Run enabled

  1. Create a Google Kubernetes Engine cluster for Cloud Run for Anthos on Google Cloud 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 --image-type=cos \
      --enable-stackdriver-kubernetes \
      --scopes=cloud-platform,logging-write,monitoring-write,pubsub \
      --zone us-central1 \
      --release-channel=rapid
    
  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/ \
    projects/my-project/zones/us-central1/clusters/events-cluster].
    

    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 on Google Cloud 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 on Google Cloud 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 \
      --copy-default-secret
    
  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 on Google Cloud service to receive events

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

To deploy the Cloud Run for Anthos on Google Cloud service:

  1. Clone the repository:

    Python

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

    Java

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

    Go

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

    Node.js

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

    C#

    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 on Google Cloud:

    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 on Google Cloud 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 \
      --namespace=events
    
  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 on Google Cloud 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 the Manage resources page

  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