Quickstart: Receive events using Cloud Audit Logs (Google Cloud Console)

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 Google Cloud Console. For instructions using the gcloud command-line tool, see Receive events using Cloud Audit Logs (gcloud tool).

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

  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

Create a Cloud Storage bucket

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

  1. Go to Cloud Storage in the Cloud Console.

    Go to Cloud Storage

  2. Click Create bucket.

    New bucket.

  3. Enter your bucket information and click Continue to complete each step:

    • Enter a unique Name. For example, eventarcbucket.
    • Select Region as the Location Type.
    • Select us-central1 (Iowa) as the Location.
    • Select Standard for default storage class.
    • Select Uniform for Access control.
  4. Click Create.

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. Fork the sample repository to your GitHub account:

    Go

    1. On GitHub, navigate to GoogleCloudPlatform/golang-samples.

    2. Click Fork.

      Screenshot of the Fork button

    3. 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.

      Screenshot of the Fork button

    3. 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.

      Screenshot of the Fork button

    3. 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.

      Screenshot of the Fork button

    3. 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.

      Screenshot of the Fork button

    3. If prompted, select the location where you want to fork the repository.

  2. Go to Cloud Run in the Cloud Console.

    Go to Cloud Run

  3. Click Create service to display the Create service form.

    create-service-page

  4. Select Cloud Run.

  5. Select us-central1(Iowa) as the Region where you want your service located.

  6. Enter the desired service name. For example, helloworld-events.

  7. Click Next.

  8. Select Continuously deploy new revisions from a source repository.

  9. Click Set up with Cloud Build to open the Set up with Cloud Build form.

    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 ^master$.

    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, click Next.

  11. In the Configure how this service is triggered section:

    • Select Allow all traffic.

    • Select Allow unauthenticated invocations.

  12. Optionally, click Add Eventarc trigger and create a trigger or create a trigger after you create a service. For more information on creating a trigger, see Create an Eventarc trigger.

  13. Click Create.

Create an Eventarc trigger

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

  1. Go to Cloud Run in the Cloud Console.

    Go to Cloud Run

  2. From the list of services, click the service you created, to go to its Service details page.

  3. Click the Triggers tab, and click Add Eventarc trigger.

  4. In the Pick an event drop-down list, select Cloud Storage > storage.objects.create.

  5. To enable the required audit log types for the storage.googleapis.com service, click Enable All .

  6. In the Receive events from field, select Single region and then select us-central1 as the region to receive events from.

  7. If prompted, grant the eventarc.eventReceiver role to the Compute Engine service account and the iam.serviceAccountTokenCreator role to the Pub/Sub service account.

    Setting up a new trigger

  8. Click Save.

Generate and view an event

  1. To generate an event:

    1. Create a text file with the file name random.txt and the text "Hello World".

    2. Go to Cloud Storage in the 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.

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

  2. To view the log entry:

    1. Go to Cloud Run in the 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:

       Detected change in Cloud Storage bucket: storage.googleapis.com/projects/_/buckets/eventarcbucket/objects/random.txt
       

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:

  1. Go to Cloud Run in the Cloud Console.

    Go to Cloud Run

  2. From the list of services, click the service you created, to go to its Service details page.

  3. Click the Triggers tab, and click Delete 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.

  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