Receive events using Pub/Sub messages (gcloud CLI)

This quickstart shows you how to deploy an unauthenticated Cloud Run service that receives events using Pub/Sub.

In this quickstart, you:

  1. Deploy an event receiver service to Cloud Run.

  2. Create an Eventarc trigger.

  3. Publish a message to a Pub/Sub topic to generate an event, 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.

  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 the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  5. Make sure that billing is enabled for your Google Cloud project.

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

    gcloud services enable run.googleapis.com logging.googleapis.com cloudbuild.googleapis.com pubsub.googleapis.com eventarc.googleapis.com
  7. Install the Google Cloud CLI.
  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  10. Make sure that billing is enabled for your Google Cloud project.

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

    gcloud services enable run.googleapis.com logging.googleapis.com cloudbuild.googleapis.com pubsub.googleapis.com eventarc.googleapis.com
  12. Update Google Cloud CLI components:
    gcloud components update
  13. Sign in using your account:
    gcloud auth login
  14. Set the configuration variables used in this quickstart:
    gcloud config set run/region us-central1
    gcloud config set run/platform managed
    gcloud config set eventarc/location us-central1
    
  15. If you are the project creator, you are granted the basic Owner role (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 page for your event destination.

    Required permissions

    To get the permissions that you need to complete this quickstart, ask your administrator to grant you the following IAM roles on your project:

    For more information about granting roles, see Manage access.

    You might also be able to get the required permissions through custom roles or other predefined roles.

  16. 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 (roles/iam.serviceAccountTokenCreator) to the Google-managed service account. Otherwise, this role is granted by default:
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com \
        --role=roles/iam.serviceAccountTokenCreator
  17. Replace PROJECT_NUMBER with your Google Cloud project number. You can find your project number on the Welcome page of the Google Cloud console or by running the following command:

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

Deploy an event receiver to Cloud Run

Deploy a Cloud Run service that receives and logs events.

  1. Clone the 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.

    Ruby

    git clone https://github.com/GoogleCloudPlatform/ruby-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/pubsub/

    Python

    cd python-docs-samples/eventarc/pubsub/

    Go

    cd golang-samples/eventarc/pubsub/

    Java

    cd java-docs-samples/eventarc/pubsub/

    Ruby

    cd ruby-docs-samples/eventarc/pubsub/

    C#

    cd dotnet-docs-samples/eventarc/pubsub/
  3. Build the container and upload it to Cloud Build:

    gcloud builds submit --tag gcr.io/$(gcloud config get-value project)/events-pubsub
    
  4. Deploy the container image to Cloud Run:

    gcloud run deploy helloworld-events-pubsub-quickstart \
        --image gcr.io/$(gcloud config get-value project)/events-pubsub \
        --allow-unauthenticated
    

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

Create an Eventarc trigger

The event trigger sends messages to the event receiver service deployed on Cloud Run when a message is published to the Pub/Sub topic.

  1. Create a trigger to listen for Pub/Sub messages:

    New Pub/Sub topic

    gcloud eventarc triggers create events-pubsub-trigger \
        --destination-run-service=helloworld-events-pubsub-quickstart \
        --destination-run-region=us-central1 \
        --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished"
    

    This creates a new Pub/Sub topic and a trigger for it called events-pubsub-trigger.

    Existing Pub/Sub topic

    gcloud eventarc triggers create events-pubsub-trigger \
        --destination-run-service=helloworld-events-pubsub-quickstart \
        --destination-run-region=us-central1 \
        --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
        --transport-topic=projects/PROJECT_ID/topics/TOPIC_ID
    

    Replace the following:

    • PROJECT_ID: your Google Cloud project ID
    • TOPIC_ID: the ID of the existing Pub/Sub topic

    This creates a trigger called events-pubsub-trigger for the existing Pub/Sub topic.

    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:

    gcloud eventarc triggers list --location=us-central1
    

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

Generate and view a Pub/Sub topic event

You can generate an Eventarc event by publishing a message to a Pub/Sub topic.

  1. Find and set the Pub/Sub topic as an environment variable:

    export RUN_TOPIC=$(gcloud eventarc triggers describe events-pubsub-trigger \
        --format='value(transport.pubsub.topic)')
    
  2. Publish a message to the Pub/Sub topic to generate an event:

    gcloud pubsub topics publish $RUN_TOPIC --message "Runner"
    

    The event is routed to the Cloud Run service, which logs the event message.

  3. To view the event-related log entries created by your service, run the following command:

    gcloud logging read 'textPayload: "Hello, Runner!"'
    
  4. Look for a log entry similar to:

    "textPayload": "Hello, Runner! ID: 7479199264356846"
    

Congratulations! You have successfully deployed an event receiver service to Cloud Run, created an Eventarc trigger, generated an event from Pub/Sub, 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 Pub/Sub messages.

You can delete your image, delete the Pub/Sub topic, and delete the Pub/Sub subscription. To delete the Eventarc trigger:

gcloud eventarc triggers delete events-pubsub-trigger

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.

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

What's next