Receive a Cloud Audit Logs event

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

This tutorial shows you how to deploy an authenticated Cloud Run service that receives events from Cloud Storage using Cloud Audit Logs. Use this tutorial to deploy production workloads. The Eventarc trigger filters events based on Cloud Audit Logs entries. For more information, see Determine event filters for Cloud Audit Logs.

You can complete this tutorial using either the Google Cloud console or the Google Cloud CLI.

Objectives

In this tutorial, you will:

  1. Create a Cloud Storage bucket to be the event source.

  2. Deploy an event receiver service to Cloud Run.

  3. Create an Eventarc trigger.

  4. Generate an event by uploading a file to the Cloud Storage bucket, and view it in the Cloud Run logs.

Costs

This tutorial uses the following billable components of Google Cloud:

To generate a cost estimate based on your projected usage, use the pricing calculator. New Google Cloud users might be eligible for a free trial.

Before you begin

Some of the steps in this document might not work correctly if your organization applies constraints to your Google Cloud environment. In that case, you might not be able to complete tasks like creating public IP addresses or service account keys. If you make a request that returns an error about constraints, see how to Develop applications in a constrained Google Cloud environment.

Complete the following steps using the Google Cloud console or the gcloud CLI:

Console

  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 check if billing is enabled on a project.

  4. Enable the Cloud Build, Logging, Pub/Sub, Cloud Run, Cloud Storage, and Eventarc APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  7. Enable the Cloud Build, Logging, Pub/Sub, Cloud Run, Cloud Storage, and Eventarc APIs.

    Enable the APIs

  8. In the Google Cloud console, go to the Service accounts page.

    Go to Service accounts
  9. Click Create service account.
  10. Enter a service account name to display in the Google Cloud console.

    The Google Cloud console generates a service account ID based on this name. Edit the ID if necessary. You cannot change the ID later.

  11. Optional: Enter a description of the service account.
  12. If you do not want to set access controls now, click Done to finish creating the service account.

    To set access controls now, click Create and continue and continue to the next step.

  13. Choose the Cloud Run Invoker and Eventarc Event Receiver Identity and Access Management roles to grant to the service account on the project.
  14. When you are done adding roles, click Continue and Done to finish creating the service account.
  15. To grant IAM roles to a principal, do the following:
    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Find the row containing the principal's email address, click Edit principal in that row, and click Add another role.
    3. From the drop-down list, add Eventarc Admin and Service Account User roles.
    4. Click Save. The principal is granted the Identity and Access Management roles on the resource.

gcloud

  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. Enable the Cloud Build, Logging, Pub/Sub, Cloud Run, Cloud Storage, and Eventarc APIs:

    gcloud services enable cloudbuild.googleapis.com 
          logging.googleapis.com  pubsub.googleapis.com  run.googleapis.com 
          storage.googleapis.com  and eventarc.googleapis.com
  6. Install and initialize the Google Cloud CLI.
  7. 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
  8. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  9. Enable the Cloud Build, Logging, Pub/Sub, Cloud Run, Cloud Storage, and Eventarc APIs:

    gcloud services enable cloudbuild.googleapis.com 
          logging.googleapis.com  pubsub.googleapis.com  run.googleapis.com 
          storage.googleapis.com  and eventarc.googleapis.com
  10. Update gcloud components:
    gcloud components update
  11. Log in using your account:
    gcloud auth login
    
  12. Set the configuration variables used in this tutorial:
    gcloud config set run/region us-central1
    gcloud config set run/platform managed
    gcloud config set eventarc/location us-central1
    
  13. Enable Cloud Audit Logs Admin Read, Data Read, and Data Write Log Types in Google Cloud Storage: Go to Cloud Audit Logs
  14. Create a service account for the project:
    gcloud iam service-accounts create sample-service-account \
      --description="A sample service account" \
      --display-name="Sample service account"
    After you create a service account, it can take up to seven minutes before you can use the service account. If you try to use a service account immediately after you create it, and you receive an error, wait at least 60 seconds and try again.
  15. To confirm that sample-service-account has been created, run:
    gcloud iam service-accounts list
    The output should be similar to the following:
    DISPLAY NAME                     EMAIL                                                               DISABLED
    Default compute service account  PROJECT_NUMBER-compute@developer.gserviceaccount.com                False
    Sample service account           sample-service-account@PROJECT_ID.iam.gserviceaccount.com           False
  16. Grant the following roles to the service account:

      run.invoker

      gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:sample-service-account@PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/run.invoker"

      Replace PROJECT_ID with the Google Cloud project ID.

      eventarc.eventReceiver

      gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:sample-service-account@PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/eventarc.eventReceiver"

      Replace PROJECT_ID with the Google Cloud project ID.

  17. Grant the following IAM roles to the principal:

      eventarc.admin

      gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="PRINCIPAL" \
        --role='roles/eventarc.admin'

      Replace the following values:

      • PROJECT_ID: the Google Cloud project ID.
      • PRINCIPAL: a valid identity to which you want to grant the role.

          For example:

        • Google account email: user:test-user@gmail.com
        • Google group: group:admins@example.com
        • Service account: serviceAccount:test123@example.domain.com
        • Google Workspace or Cloud Identity domain: domain:example.domain.com

      iam.serviceAccountUser

      gcloud iam service-accounts add-iam-policy-binding \
        sample-service-account@PROJECT_ID.iam.gserviceaccount.com \
        --member="PRINCIPAL" \
        --role="roles/iam.serviceAccountUser"

      Replace the following values:

      • PROJECT_ID: the Google Cloud project ID.
      • PRINCIPAL: a valid identity to which you want to grant the role.

          For example:

        • Google account email: user:test-user@gmail.com
        • Google group: group:admins@example.com
        • Service account: serviceAccount:test123@example.domain.com
        • Google Workspace or Cloud Identity domain: domain:example.domain.com

Create a Cloud Storage bucket

This tutorial uses Cloud Storage as the event source. To create a storage bucket:

Console

  1. Go to Cloud Storage in the Google Cloud console.

    Go to Cloud Storage

  2. Click Create bucket.
  3. New bucket.
  4. Enter your bucket information and click Continue to complete each step:
    1. Enter a unique Name. For example, eventarcbucket.
    2. Select Region as the Location Type.
    3. Select us-central1 (Iowa) as the Location.
    4. Select Standard for default storage class.
    5. Select Uniform for Access control.
  5. Click Create.

gcloud

gsutil mb -l us-central1 gs://events-tutorial-PROJECT_ID/

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

Deploy the event receiver service to Cloud Run

Deploy a Cloud Run service that receives and logs events. To deploy the sample event receiver service:

Console

  1. Clone the sample repository to your GitHub account:
  2. Go

    1. On GitHub, navigate to GoogleCloudPlatform/golang-samples
    2. Click Fork.
    3. Screenshot of the Fork button.
    4. If prompted, select the location where you want to fork the repository.

    Java

    1. On GitHub, navigate to GoogleCloudPlatform/java-docs-samples.
    2. Click Fork.
    3. Screenshot of the Fork button.
    4. If prompted, select the location where you want to fork the repository.

    .NET

    1. On GitHub, navigate to GoogleCloudPlatform/dotnet-docs-samples.
    2. Click Fork.
    3. Screenshot of the Fork button.
    4. If prompted, select the location where you want to fork the repository.

    Node.js

    1. On GitHub, navigate to GoogleCloudPlatform/nodejs-docs-samples.
    2. Click Fork.
    3. Screenshot of the Fork button.
    4. If prompted, select the location where you want to fork the repository.

    Python

    1. On GitHub, navigate to GoogleCloudPlatform/python-docs-samples.
    2. Click Fork.
    3. Screenshot of the Fork button.
    4. If prompted, select the location where you want to fork the repository.
  3. Go to Cloud Run in the Google Cloud console.
  4. Go to Cloud Run

  5. Click Create service to display the Create service form.
  6. create-service-page.
  7. Select Continuously deploy new revisions from a source repository.
  8. Click Set up with Cloud Build to open the Set up with Cloud Build form.
  9. Set up with Cloud Build page.

    In the Set up with Cloud Build form:

    1. If prompted, enable the Cloud Build API and Container Analysis API.
    2. Select GitHub as the Repository Provider.
    3. If prompted, click Install Google Cloud Build.
    4. Select the GitHub repository you forked as the Repository.
    5. Click Next.
    6. In the Branch field, enter ^main$.
    7. Select Dockerfile as the Build Type and provide the source location of the Dockerfile: eventarc/audit-storage/Dockerfile or eventarc/audit_storage/Dockerfile(Go)
    8. Click Save.
  10. In the Create service form, enter the desired service name. For example, helloworld-events.
  11. Select us-central1(Iowa) as the Region where you want your service located.
  12. Select any of the Ingress options based on the ingress traffic you want to allow on the Cloud Run service.
  13. Select Require authentication
  14. Click Create.

gcloud

  1. Clone the repository:

    Go

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

    Java

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

    .NET

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

    Node.js

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

    Python

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    cd python-docs-samples/eventarc/audit-storage
  2. Build the container and upload it to Cloud Build:
    gcloud builds submit --tag gcr.io/PROJECT_ID/helloworld-events
    
  3. Deploy the container image to Cloud Run:
    gcloud run deploy helloworld-events \
      --image gcr.io/PROJECT_ID/helloworld-events
    

    At the prompt to allow unauthenticated invocations, enter n.

    When the deployment succeeds, the command line displays the service URL.

Now that you have deployed your event receiver service called helloworld-events to Cloud Run, you can set up your trigger.

Create an Eventarc trigger

The Eventarc trigger will send events from the Cloud Storage bucket to the helloworld-events Cloud Run service.

Console

  1. In the Google Cloud console, go to the Eventarc page.

    Go to Eventarc

  2. Click Create trigger.
  3. Type a Trigger name.

    This is the ID of the trigger and it must start with a letter. It can contain up to 63 lowercase letters, numbers, or hyphens.

  4. In the Event provider list, select Cloud Storage as the source of events.
  5. In the Event list, select storage.objects.create.
  6. In the Region list, select us-central1 as the region to receive events from.
  7. Select the Service account you created. For example, SERVICE_ACCOUNT_ID@PROJECT_ID.iam.gserviceaccount.com.
  8. In the Event destination list, select Cloud Run.
  9. In the Select a Cloud Run service list, select helloworld-events.
  10. Click Create.
  11. Note that you can also create an Eventarc trigger using the Cloud Run console page.

gcloud

  1. Create a trigger that filters Cloud Storage events and that uses the service account you created:
    gcloud eventarc triggers create events-tutorial-trigger \
      --destination-run-service=helloworld-events \
      --destination-run-region=us-central1 \
      --event-filters="type=google.cloud.audit.log.v1.written" \
      --event-filters="serviceName=storage.googleapis.com" \
      --event-filters="methodName=storage.objects.create" \
      --service-account=sample-service-account@PROJECT_ID.iam.gserviceaccount.com
    Replace PROJECT_ID with the Google Cloud project ID.

    Where:

    • type: specifies that audit logs are created when the trigger's filter criteria is met.
    • serviceName: the service that writes the audit log, which is Cloud Storage.
    • methodName: the operation that is being audited, which is storage.objects.create.
    This creates a trigger called events-tutorial-trigger.
  2. To confirm events-tutorial-trigger was successfully created, run:
    gcloud eventarc triggers list --location=us-central1
  3. The trigger, events-tutorial-trigger, is listed with a target of helloworld-events.

Generate and view an event

  1. To generate an event:

    Console

    1. Create a text file with the file name random.txt and the text "Hello World".
    2. Go to Cloud Storage in the Google Cloud console.

      Go to Cloud Storage

    3. Select the storage bucket you created.
    4. In the Objects tab, click Upload files and upload the random.txt file.

    gcloud

    Upload a text file to Cloud Storage:

    echo "Hello World" > random.txt
    gsutil cp random.txt gs://events-tutorial-PROJECT_ID/random.txt
  2. To view the log entry:
  3. Console

    1. Go to Cloud Run in the Google Cloud console.

      Go to Cloud Run

    2. From the list of services, click the name of the service you created to go to its Service details page.
    3. Click the Logs tab, to get the request and container logs for all revisions of this service. You can filter by log severity level.
    4. Look for a log entry similar to: Trigger logs.

    gcloud

    1. gcloud logging read "resource.labels.service_name=helloworld-events AND textPayload:random.txt" --format=json
    2. Look for a log entry similar to:
      Detected change in Cloud Storage bucket: storage.googleapis.com/projects/_/buckets/BUCKET_NAME/objects/random.txt
      where BUCKET_NAME is the name of the Cloud Storage bucket.

Congratulations! You have successfully deployed an event receiver service to Cloud Run, created an Eventarc trigger, generated an event from Cloud Storage, and viewed it in the Cloud Run logs.

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, and for storing files in your Cloud Storage bucket.

You can delete your image and delete your storage bucket. To delete the Eventarc trigger:

gcloud eventarc triggers delete events-tutorial-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.

    Delete a Cloud project:

    gcloud projects delete PROJECT_ID

What's next