Use Eventarc to receive events from Cloud Storage

This tutorial shows you how to deploy a containerized application using an authenticated Cloud Run service that receives events through Eventarc.

By specifying filters for an Eventarc trigger, you can configure the routing of events, including the event source and the event target. In this case, an update to a Cloud Storage bucket triggers the event and a request is sent to your Cloud Run service in the form of an of HTTP request.

Create an Artifact Registry standard repository

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

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

Replace REPOSITORY with a unique name for the repository.

Create a Cloud Storage bucket

Create a Cloud Storage bucket to use as the event source:

gcloud storage buckets create gs://PROJECT_ID-bucket/ --location=us-central1

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

Deploy an event receiver to Cloud Run

Deploy a Cloud Run service that receives and logs events.

  1. Clone the GitHub repository:

    Node.js

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    Python

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    Go

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

    Alternatively, you can download the sample as a zip file and extract it.

    Java

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    C#

    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

  2. Change to the directory that contains the Cloud Run sample code:

    Node.js

    cd nodejs-docs-samples/eventarc/audit-storage/

    Python

    cd python-docs-samples/eventarc/audit-storage/

    Go

    cd golang-samples/eventarc/audit_storage/

    Java

    cd java-docs-samples/eventarc/audit-storage/

    C#

    cd dotnet-docs-samples/eventarc/audit-storage/
  3. Build the container for the Cloud Run service:

    export PROJECT_ID=$(gcloud config get-value project)
    export SERVICE_NAME=helloworld-events
    gcloud builds submit --tag $REGION-docker.pkg.dev/${PROJECT_ID}/REPOSITORY/${SERVICE_NAME}:v1
  4. Deploy the container image to Cloud Run:

    gcloud run deploy ${SERVICE_NAME} \
        --image $REGION-docker.pkg.dev/${PROJECT_ID}/REPOSITORY/${SERVICE_NAME}:v1
  5. At the Allow public access to helloworld-events (y/N)? prompt, respond n for "No".

When you see the Cloud Run service URL, the deployment is complete.

Create an Eventarc trigger

The Eventarc trigger sends events from the Cloud Storage bucket to the helloworld-events Cloud Run service. The service requires authentication, and the event should be triggered by a caller that has a service account with the required IAM roles and permissions to use the resource.

  1. Create a trigger that filters Cloud Storage events:

    gcloud eventarc triggers create ${SERVICE_NAME} \
        --destination-run-service=${SERVICE_NAME} \
        --destination-run-region=${REGION} \
        --location=${REGION} \
        --event-filters="type=google.cloud.storage.object.v1.finalized" \
        --event-filters="bucket=PROJECT_ID-bucket" \
        --service-account=PROJECT_NUMBER[email protected]

    This creates a trigger called helloworld-events.

    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.

  2. Confirm that the trigger was successfully created. Note that although your trigger is created immediately, it can take up to two minutes for a trigger to be fully functional.

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

    The output should be similar to the following:

    NAME: helloworld-events
    TYPE: google.cloud.storage.object.v1.finalized
    DESTINATION: Cloud Run service: helloworld-events
    ACTIVE: Yes
    

Generate and view an event

Upload a text file to the Cloud Storage bucket to generate an event which is routed to the Cloud Run service. The Cloud Run service logs the event in the service logs.

  1. To generate an event:

    Upload a text file to Cloud Storage:

     echo "Hello World" > random.txt
     gcloud storage cp random.txt gs://PROJECT_ID-bucket/random.txt
    

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

  2. To view the log entry:

    1. Filter the log entries and return the output in JSON format:

      gcloud logging read "resource.labels.service_name=helloworld-events AND textPayload:random.txt" --format=json
      
    2. Look for a log entry similar to:

      "textPayload": "Detected change in Cloud Storage bucket: objects/random.txt"
      

Logs might take a few moments to appear. If you don't see them immediately, check again after a minute.