Quickstart: Receive events using Cloud Audit Logs (gcloud command-line tool)

This quickstart shows you how to receive events from Cloud Storage in an unauthenticated Cloud Run service using Eventarc.

You can complete this quickstart using the gcloud command-line tool. For instructions using the console, see Receive events using Cloud Audit Logs (Google Cloud Console).

In this quickstart, you:

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

  2. Deploy an event receiver service to Cloud Run.

  3. Create an event trigger.

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

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.

  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. Enable the Cloud Run, Cloud Logging, Cloud Build, Pub/Sub, 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 confirm that billing is enabled for your project.

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

    Enable the APIs

  8. Update gcloud components:
    gcloud components update
  9. Log in using your account:

    gcloud auth login
    

  10. Set the configuration variables used in this quickstart:

    gcloud config set project PROJECT_ID
    gcloud config set run/region us-central1
    gcloud config set run/platform managed
    gcloud config set eventarc/location us-central1
    

  11. Enable Cloud Audit Logs Admin Read, Data Read, and Data Write Log Types in Google Cloud Storage.

    Note that at the project level, you need the roles/owner Identity and Access Management role to configure Data Access audit logs for your Google Cloud resources.

    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
      
    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.
  12. Grant the eventarc.eventReceiver role to the Compute Engine service account:
    
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
        --role='roles/eventarc.eventReceiver'
    

  13. If you enabled the Pub/Sub service account on or before April 8, 2021, grant the iam.serviceAccountTokenCreator role to the Pub/Sub service account:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:service-PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com"\
        --role='roles/iam.serviceAccountTokenCreator'
    

  14. Download and install the Git source code management tool.

Create a Cloud Storage bucket

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

gsutil mb -l us-central1 gs://events-quickstart-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:

  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 \
        --allow-unauthenticated
    

    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.

  1. Create a trigger that filters Cloud Storage events and that uses the Google Cloud project Compute Engine default service account:

      gcloud eventarc triggers create events-quickstart-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=PROJECT_NUMBER-compute@developer.gserviceaccount.com
    

    This creates a trigger called events-quickstart-trigger.

  2. To confirm events-quickstart-trigger was successfully created, run:

     gcloud eventarc triggers list --location=us-central1
    

    events-quickstart-trigger is displayed listed with a target of helloworld-events.

Generate and view an event

  1. To generate an event:

    Upload a text file to Cloud Storage:

     echo "Hello World" > random.txt
     gsutil cp random.txt gs://events-quickstart-PROJECT_ID/random.txt
    

    The upload generates an event and the Cloud Run service logs the event's message.

  2. To view the event-related log entries created by your service:

    1. gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=helloworld-events AND NOT resource.type=build"
      
    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-quickstart-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