# Receive events using Cloud Audit Logs (gcloud CLI)

<br />

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


> [!NOTE]
> **Note:** All Cloud Run services are deployed privately by default, which means that they can't be accessed without providing authentication credentials in the request. For projects with [domain restricted sharing organization policies](https://docs.cloud.google.com/resource-manager/docs/organization-policy/restricting-domains), use an authenticated Cloud Run target service rather than one that allows [public access](https://docs.cloud.google.com/run/docs/authenticating/public.md.txt).

<br />

You can complete this quickstart using the Google Cloud CLI. For instructions
using the console, see
[Create a trigger using the Google Cloud console](https://docs.cloud.google.com/eventarc/standard/docs/run/route-trigger-cloud-audit-logs.md.txt#console_1).

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

Security constraints defined by your organization might prevent you from completing the
following steps. For troubleshooting information, see
[Develop applications in a constrained Google Cloud environment](https://docs.cloud.google.com/resource-manager/docs/organization-policy/develop-apps-constrained-environment).

1. Update `gcloud` components:

   ```bash
   gcloud components update
   ```
2. Sign in using your account:

   ```
   gcloud auth login
   ```
3. Set the configuration variables used in this quickstart:

   ```
   export REGION=us-central1
   gcloud config set run/region ${REGION}
   gcloud config set run/platform managed
   gcloud config set eventarc/location ${REGION}
   ```
4. If you are the project creator, you are granted the
   [basic Owner role](https://docs.cloud.google.com/iam/docs/roles-overview.md.txt#basic)
   (`roles/owner`). By default, this Identity and Access Management (IAM) role
   includes the permissions necessary for full access to most Google Cloud
   resources and you can skip this step.

   If you are not the project creator, required permissions must be granted on
   the project to the appropriate *principal*. For example, a principal can be
   a Google Account (for end users) or a service account (for applications and
   compute workloads). For more information, see the
   [Roles and permissions](https://docs.cloud.google.com/eventarc/docs/all-roles-permissions.md.txt) page
   for your event destination.

   Note that by default,
   [Cloud Build
   permissions include permissions to upload and download Artifact Registry
   artifacts](https://docs.cloud.google.com/artifact-registry/docs/access-control#gcp).

   #### Required permissions


   To get the permissions that
   you need to complete this tutorial,

   ask your administrator to grant you the
   following IAM roles on your project:

   - [Cloud Build Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/cloudbuild.md.txt#cloudbuild.builds.editor) (`roles/cloudbuild.builds.editor`)
   - [Cloud Run Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.admin) (`roles/run.admin`)
   - [Eventarc Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/eventarc.md.txt#eventarc.admin) (`roles/eventarc.admin`)
   - [Logs View Accessor](https://docs.cloud.google.com/iam/docs/roles-permissions/logging.md.txt#logging.viewAccessor) (`roles/logging.viewAccessor`)
   - [Project IAM Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/resourcemanager.md.txt#resourcemanager.projectIamAdmin) (`roles/resourcemanager.projectIamAdmin`)
   - [Service Account Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountAdmin) (`roles/iam.serviceAccountAdmin`)
   - [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`)
   - [Service Usage Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/serviceusage.md.txt#serviceusage.serviceUsageAdmin) (`roles/serviceusage.serviceUsageAdmin`)
   - [Storage Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/storage.md.txt#storage.admin) (`roles/storage.admin`)


   For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


   You might also be able to get
   the required permissions through [custom
   roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
   roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).
5. 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.

      <br />

      > [!CAUTION]
      > **Caution:** You must preserve the `bindings:` and `etag:` sections without changes. Failure to do so might cause your project to become unusable.

      ```
      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.
6. Make note of the [Compute Engine
   default service account](https://docs.cloud.google.com/iam/docs/service-account-types#default) as you will attach it to an Eventarc trigger to represent
   the identity of the trigger for testing purposes. This service account is automatically created
   after enabling or using a Google Cloud service that uses Compute Engine, and with the
   following email format:

   ```bash
   PROJECT_NUMBER-compute@developer.gserviceaccount.com
   ```

   Replace `PROJECT_NUMBER` with your Google Cloud
   project number. You can find your project number on the
   [Welcome](https://console.cloud.google.com/welcome)
   page of the Google Cloud console or by running the following command:

   ```bash
   gcloud projects describe PROJECT_ID --format='value(projectNumber)'
   ```

   For production environments, we strongly recommend
   [creating a new service account](https://docs.cloud.google.com/iam/docs/service-accounts-create.md.txt)
   and granting it one or more IAM roles that contain the
   [minimum permissions required](https://docs.cloud.google.com/iam/docs/best-practices-service-accounts.md.txt#limit-service-account-privileges)
   and follow the principle of [least privilege](https://docs.cloud.google.com/iam/docs/using-iam-securely.md.txt#least_privilege).

   > [!NOTE]
   > **Note:**
   >
   >
   > The [`iam.automaticIamGrantsForDefaultServiceAccounts` organization policy constraint](https://docs.cloud.google.com/resource-manager/docs/organization-policy/restricting-service-accounts#disable_service_account_default_grants)
   > prevents the Editor role from being automatically granted to default service accounts. If you
   > created your organization after May 3, 2024, this constraint is enforced by
   > default.
   >
   >
   > We strongly recommend that you enforce this constraint to disable the automatic role grant. If you
   > disable the automatic role grant, you must decide which roles to grant to the default service
   > accounts, and then [grant these roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt)
   > yourself.
   >
   >
   > If the default service account already has the Editor role, we recommend that you replace the
   > Editor role with less permissive roles.To safely modify the service account's roles, use [Policy Simulator](https://docs.cloud.google.com/policy-intelligence/docs/simulate-iam-policies.md.txt) to see the impact of
   > the change, and then [grant and revoke the
   > appropriate roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access).

7. Grant the [Eventarc
   Event Receiver role](https://docs.cloud.google.com/iam/docs/roles-permissions/eventarc#eventarc.eventReceiver) (`roles/eventarc.eventReceiver`) on the project to the Compute Engine default service account so that the Eventarc trigger can receive events from event providers.

   ```bash
   gcloud projects add-iam-policy-binding PROJECT_ID \
       --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
       --role=roles/eventarc.eventReceiver
   ```
8. If you enabled the Cloud Pub/Sub service agent on or before April 8, 2021, to support authenticated Pub/Sub push requests, grant the [Service
   Account Token Creator role](https://docs.cloud.google.com/iam/docs/roles-permissions/iam#iam.serviceAccountTokenCreator) (`roles/iam.serviceAccountTokenCreator`) to the service agent. Otherwise, this role is granted by default:

   ```bash
   gcloud projects add-iam-policy-binding PROJECT_ID \
       --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com \
       --role=roles/iam.serviceAccountTokenCreator
   ```
9. [Download and install](https://git-scm.com/downloads) the Git source code management tool.

## Create an Artifact Registry standard repository

Create an Artifact Registry standard repository to store your container image:

```bash
gcloud artifacts repositories create REPOSITORY \
    --repository-format=docker \
    --location=$REGION
```

Replace `REPOSITORY` with a unique name for the
repository.

## Create a Cloud Storage bucket

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

```bash
gcloud storage buckets create gs://events-quickstart-PROJECT_ID/ --location=${REGION}
```

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 GitHub repository:

   ### Go

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

   ### Java

   ```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

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

   ### Python

   ```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:

   ```bash
   gcloud builds submit --tag $REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/helloworld-events:v1
   ```
3. Deploy the container image to Cloud Run:

   ```bash
   gcloud run deploy helloworld-events \
       --image $REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/helloworld-events:v1 \
       --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:

   ```bash
   gcloud eventarc triggers create events-quickstart-trigger \
       --destination-run-service=helloworld-events \
       --destination-run-region=${REGION} \
       --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`.


   Note that when creating an Eventarc trigger for the first
   time in a Google Cloud project, there might be a delay in provisioning
   the Eventarc service agent. This issue can usually be resolved
   by attempting to create the trigger again. For more information, see
   [Permission denied errors](https://docs.cloud.google.com/eventarc/docs/troubleshooting.md.txt#trigger-error).

   <br />

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

   <br />

   ```bash
   gcloud eventarc triggers list --location=${REGION}
   ```

   <br />

   The `events-quickstart-trigger` is listed with a destination that is the
   Cloud Run service, `helloworld-events`.

> [!NOTE]
> **Note:** Although your trigger is created immediately, it can take up to two minutes for triggers to be fully functional.

## Generate and view an event

1. To generate an event, upload a text file to Cloud Storage:

   ```
    echo "Hello World" > random.txt
    gcloud storage 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, run the
   following command:

   ```
   gcloud logging read 'textPayload: "Detected change in Cloud Storage bucket"'
   ```
3. 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.

   > [!NOTE]
   > **Note:** If you don't see a log entry, wait a few minutes and retry the `gcloud logging read` command. The trigger can take up to two minutes after creation to start filtering events.

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 Artifact Registry](https://cloud.google.com/artifact-registry/pricing), [storing files in your Cloud Storage bucket](https://cloud.google.com/storage/pricing), and [Eventarc resources](https://cloud.google.com/eventarc/pricing).

<br />

You can:

1. [Delete your container image](https://docs.cloud.google.com/artifact-registry/docs/docker/manage-images.md.txt#deleting_images).

2. [Delete your storage bucket](https://docs.cloud.google.com/storage/docs/deleting-buckets.md.txt).

3. [Delete the Eventarc trigger](https://docs.cloud.google.com/eventarc/docs/managing-triggers.md.txt#deleting_triggers).

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

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.

Delete a Google Cloud project:

```
gcloud projects delete PROJECT_ID
```

## What's next

- [Receive a Cloud Audit Logs event for production workloads](https://docs.cloud.google.com/eventarc/standard/docs/run/cal.md.txt)
- [Build containers](https://docs.cloud.google.com/run/docs/building/containers.md.txt)
- [Determine event filters for Cloud Audit Logs](https://docs.cloud.google.com/eventarc/docs/determining-filters-cal.md.txt)
- [Develop Cloud Run services](https://docs.cloud.google.com/run/docs/developing.md.txt)