Stay organized with collections Save and categorize content based on your preferences.

Receive events using Cloud Audit Logs (gcloud CLI)

This quickstart shows you how to set up a Google Kubernetes Engine (GKE) service as a destination to receive events from Cloud Storage using Eventarc.

In this quickstart, you will:

  1. Set up a service account so that Eventarc can pull events and forward it to the target.
  2. Create a Cloud Storage bucket to be the event source.
  3. Create a GKE cluster.
  4. Initialize GKE destinations in Eventarc.
  5. Deploy a GKE service that receives events.
  6. Create an Eventarc trigger that sends events from Cloud Storage to the GKE service.
  7. Upload a file to the Cloud Storage bucket to generate an event and view the event in the GKE pod logs.

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. Install and initialize the Google Cloud CLI.
  3. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select the Cloud project that you created:

      gcloud config set project PROJECT_ID
  4. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  5. Install and initialize the Google Cloud CLI.
  6. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select the Cloud project that you created:

      gcloud config set project PROJECT_ID
  7. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  8. Update gcloud components:
    gcloud components update
  9. Enable the Eventarc, Resource Manager, and Google Kubernetes Engine APIs:
    gcloud services enable eventarc.googleapis.com \
        cloudresourcemanager.googleapis.com \
        container.googleapis.com
  10. Set the configuration variables used in this quickstart:
    PROJECT_ID=$(gcloud config get-value project)
    TRIGGER_SA=eventarc-sa
    CLUSTER_NAME=events-cluster
    SERVICE_NAME=hello-gke
  11. For Google Cloud Storage, enable log types for these services: Admin Read, Data Read, and Data Write:
    1. Read your project's IAM policy and store it in a file:
      gcloud projects get-iam-policy PROJECT_ID > /tmp/policy.yaml
      
    2. Edit your policy in /tmp/policy.yaml, adding or changing only the Data Access audit logs configuration.

      auditConfigs:
      - auditLogConfigs:
        - logType: ADMIN_READ
        - logType: DATA_WRITE
        - logType: DATA_READ
        service: storage.googleapis.com
      bindings:
      - members:
        - user:EMAIL_ADDRESS
        role: roles/owner
      etag: BwW_bHKTV5U=
      version: 1
      Replace EMAIL_ADDRESS with your email address.
    3. Write your new IAM policy:

      gcloud projects set-iam-policy PROJECT_ID /tmp/policy.yaml
      If the preceding command reports a conflict with another change, then repeat these steps, starting with reading the project's IAM policy.

Set up a Google service account

Set up a user-managed service account and grant it specific roles so that the event forwarder component can pull events from Pub/Sub and forward it to the target.

  1. Create a service account called TRIGGER_SA that is used to create triggers:

    gcloud iam service-accounts create $TRIGGER_SA
  2. Grant the pubsub.subscriber and eventarc.eventReceiver roles to the 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/eventarc.eventReceiver"

Create a Cloud Storage bucket

This quickstart uses Cloud Storage as the event source. Create a Cloud Storage bucket:

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

After the event source is created, you can deploy the event receiver service on GKE.

Create a GKE cluster

A GKE cluster consists of at least one cluster control plane machine and multiple worker machines called nodes. Nodes are Compute Engine virtual machine (VM) instances that run the Kubernetes processes necessary to make them part of the cluster. You deploy applications to clusters, and the applications run on the nodes.

Create an Autopilot cluster named events-cluster:

gcloud container clusters create-auto $CLUSTER_NAME --region us-central1

It might take several minutes for the creation of the cluster to complete. Once the cluster is created, the output should be similar to the following:

Creating cluster events-cluster...done.
Created [https://container.googleapis.com/v1/projects/MY_PROJECT/zones/us-central1/clusters/events-cluster].
[...]
STATUS: RUNNING

This creates a GKE cluster named events-cluster in a project with a Google Cloud project ID of MY_PROJECT.

Enable GKE destinations

For each trigger that targets a GKE service, Eventarc creates an event forwarder component that pulls events from Pub/Sub and forwards it to the target. To create the component and manage resources in the GKE cluster, grant permissions to the Eventarc service agent:

  1. Enable GKE destinations for Eventarc:

    gcloud eventarc gke-destinations init
    
  2. At the prompt to bind the required roles, enter y.

    The following roles are bound to the service account:

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

Create a GKE service destination

Using a prebuilt image, gcr.io/cloudrun/hello, deploy a GKE service that will receive and log events:

  1. Kubernetes uses a YAML file called kubeconfig to store cluster authentication information for kubectl. Update the kubeconfig file with credentials and endpoint information to point kubectl at the GKE cluster:

    gcloud container clusters get-credentials $CLUSTER_NAME \
      --region us-central1
    
  2. Create a Kubernetes deployment:

    kubectl create deployment $SERVICE_NAME \
      --image=gcr.io/cloudrun/hello
    
  3. Expose it as a Kubernetes service:

    kubectl expose deployment $SERVICE_NAME \
      --type ClusterIP --port 80 --target-port 8080
    

Create an Eventarc trigger

When you upload a file to Cloud Storage, the Eventarc trigger sends events from Cloud Storage to the hello-gke GKE service.

  1. Create a Cloud Audit Logs trigger:

    gcloud eventarc triggers create my-gke-trigger \
      --location="us-central1" \
      --destination-gke-cluster="events-cluster" \
      --destination-gke-location="us-central1" \
      --destination-gke-namespace="default" \
      --destination-gke-service="hello-gke" \
      --destination-gke-path="/" \
      --event-filters="type=google.cloud.audit.log.v1.written" \
      --event-filters="serviceName=storage.googleapis.com" \
      --event-filters="methodName=storage.objects.create" \
      --service-account=$TRIGGER_SA@$PROJECT_ID.iam.gserviceaccount.com
    

    This creates a trigger called my-gke-trigger.

  2. Confirm that the trigger was successfully created:

    gcloud eventarc triggers list
    

    The output should be similar to the following:

    NAME: my-gke-trigger
    TYPE: google.cloud.audit.log.v1.written
    DESTINATION: GKE: hello-gke
    ACTIVE: By 20:39:43
    LOCATION: us-central1
    

Generate and view an event

Upload a text file to Cloud Storage to generate an event and trigger the GKE service. You can then view the event's message in the pod logs.

  1. Upload a text file to Cloud Storage:

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

    The upload generates an event and the GKE pod logs the event's message.

  2. To view the event message:

    1. Find the pod ID:

      kubectl get pods
      
      The output should be similar to the following:
      NAME                                         READY   STATUS             RESTARTS   AGE
      hello-gke-645964f578-2mjjt                   1/1     Running            0          35s
      Copy the NAME of the pod to use in the next step.

    2. Check the logs of the pod:

      kubectl logs NAME
      Replace NAME with the name of the pod you copied.

    3. Look for a log entry similar to:

      2022/02/24 22:23:49 Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT
      {"severity":"INFO","eventType":"google.cloud.audit.log.v1.written","message":"Received event of type google.cloud.audit.log.v1.written. [...]}
      

Clean up

While Cloud Run does not charge when the service is not in use, you might still be charged for storing the container image in Container Registry, Eventarc resources, Pub/Sub messages, and for the GKE cluster.

You can delete your image, delete your storage bucket, and delete the GKE cluster.

To delete the Eventarc trigger:

gcloud eventarc triggers delete my-gke-trigger

Alternatively, you can delete your Google Cloud project to avoid incurring charges. Deleting your Cloud project stops billing for all the resources used within that project.

gcloud projects delete PROJECT_ID_OR_NUMBER

Replace PROJECT_ID_OR_NUMBER with the project ID or number.

What's next

Receive events using Pub/Sub (Google Cloud CLI)