Receiving a Pub/Sub event

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

Objectives

In this tutorial, you will:

  1. Deploy an event receiver service to Cloud Run.

  2. Create an events trigger.

  3. Publish a message to a Pub/Sub topic to generate an event, and view it in the Cloud Run logs.

Costs

This tutorial uses billable components of Google Cloud, including:

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

New Google Cloud users might be eligible for a free trial.

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

  5. Install and initialize the Cloud SDK.
  6. Update gcloud components:
    gcloud components update
  7. Log in using your account:
    gcloud auth login
  8. Set the configuration variables used in this tutorial:
    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
    
    Where PROJECT_ID is your Google Cloud project ID.

Deploying an event receiver to Cloud Run

Deploy a Cloud Run service that receives and logs events.

To deploy the sample event receiver service:

  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-tutorial \
        --image gcr.io/$(gcloud config get-value project)/events-pubsub \
        --allow-unauthenticated
    

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

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

To create an Eventarc Pub/Sub trigger:

  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-tutorial \
      --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-tutorial \
      --destination-run-region=us-central1 \
      --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
      --transport-topic=projects/PROJECT_ID/topics/TOPIC_NAME
    

    Replace the following:

    • PROJECT_ID is your Google Cloud project ID
    • TOPIC_NAME is the name of the existing Pub/Sub topic.

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

  2. Confirm the trigger was successfully created:

    gcloud eventarc triggers list --location=us-central1
    
  3. 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)')
    
  4. Send a message to the Pub/Sub topic to generate an event:

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

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

  5. To view the event message, go to the Cloud Run service logs:

    1. Go to the Google Cloud Console
    2. Click the helloworld-events-pubsub-tutorial service.
    3. Select the Logs tab.

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

  6. Look for the "Hello Runner!" message.

Cleaning up

If you created a new project for this tutorial, delete the project. If you used an existing project and wish to keep it without the changes added in this tutorial, delete resources created for the tutorial.

Deleting the project

The easiest way to eliminate billing is to delete the project that you created for the tutorial.

To delete the 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.

Deleting tutorial resources

  1. Delete the Cloud Run service you deployed in this tutorial:

    gcloud run services delete SERVICE-NAME

    Where SERVICE-NAME is your chosen service name.

    You can also delete Cloud Run services from the Google Cloud Console.

  2. Remove the gcloud default configurations you added during tutorial setup.

    Remove the region setting:

     gcloud config unset run/region
    
  3. Remove the project configuration:

     gcloud config unset project
    
  4. Delete other Google Cloud resources created in this tutorial:

    • Delete the trigger:
      gcloud eventarc triggers delete TRIGGER_NAME
      
      Replace TRIGGER_NAME with the name of your trigger.

What's next