You can use Pub/Sub to push messages to the endpoint of your Cloud Run service, where the messages are subsequently delivered to containers as HTTP requests. You cannot use Pub/Sub pull subscriptions because Cloud Run only allocates CPU during the processing of a request.
You should process the message and then return a response when finished.
Leveraging service accounts and IAM permissions, you can securely and privately use Pub/Sub with Cloud Run, without having to expose your Cloud Run service publicly. Only the Pub/Sub subscription that you have set up is able to invoke your service.
Possible use cases include:
- Transforming data after receiving an event upon a file upload to a Cloud Storage bucket.
- Processing your Google Cloud's operations suite logs with Cloud Run by exporting them to Pub/Sub
- Publishing and processing your own custom events from your Cloud Run services.
This page shows how to enable your service to securely process messages pushed from a Pub/Sub subscription in the same Google Cloud project.
To integrate your service with Pub/Sub,
- Create a Pub/Sub topic.
- Add code in your Cloud Run service to respond to the Pub/Sub messages sent to the topic you created.
- Create a service account with the required permissions.
- Create a Pub/Sub subscription and associate it with the service account. This subscription will send to your service any message that is published to the topic.
Before you start
If you haven't done so already, set up your environment as described in the setup page for Cloud Run. You'll need to use the gcloud command line and a Google Cloud project to deploy your Cloud Run service to.
Adding code to handle messages from Pub/Sub
Your service must extract the message from the request and return an expected success code. The following snippets for selected languages (you can use any language) show how to do this for a simple Hello World message:
Node.js
Python
Go
Java
You must code the service to return an accurate HTTP response code. Success
codes, such as HTTP 200
or 204
, acknowledge complete processing of the
Pub/Sub message. Error codes, such as HTTP 400
or 500
, indicate
the message will be retried, as described in
Receiving messages using Push.
Create a service account for the subscription
You need to create a service account to associate with your Pub/Sub subscription, and give it the permission to invoke your Cloud Run service. Pub/Sub messages pushed to your Cloud Run service will carry the identity of this service account.
You can use an existing service account to represent the Pub/Sub subscription identity, or you can create a new one.
To create a new service account and give it permission to invoke the Cloud Run service:
Console
In the Cloud Console, go to the Create service account page.
Select a project.
Enter a service account name to display in the Cloud Console.
The Cloud Console generates a service account ID based on this name. Edit the ID if necessary. You cannot change the ID later.
Optional: Enter a description of the service account.
Click Create.
Click the Select a role field.
Under All roles, select Cloud Run > Cloud Run Invoker.
Click Done.
Command line
Create the service account:
gcloud iam service-accounts create SERVICE-ACCOUNT_NAME \ --display-name "DISPLAYED-SERVICE-ACCOUNT_NAME"
Replace
- SERVICE-ACCOUNT_NAME with a lower case name unique within
your Google Cloud project, for example
my-invoker-service-account-name
. - DISPLAYED-SERVICE-ACCOUNT-NAME with the name you want to
display for this service account, for example, in the console, for example,
My Invoker Service Account
.
- SERVICE-ACCOUNT_NAME with a lower case name unique within
your Google Cloud project, for example
For Cloud Run, give your service account permission to invoke your service:
gcloud run services add-iam-policy-binding SERVICE \ --member=serviceAccount:SERVICE-ACCOUNT_NAME@PROJECT-ID.iam.gserviceaccount.com \ --role=roles/run.invoker
Replace
- SERVICE with the name of the service you want to be invoked by Pub/Sub.
- SERVICE-ACCOUNT_NAME with the name of the service account.
- PROJECT-ID with your Google Cloud project ID.
Creating a Pub/Sub topic
Requests to your service are triggered by messages published to a Pub/Sub topic, so you'll need to create a topic:
Console
Visit the Pub/Sub topics page in the Cloud Console.
Click Create a topic.
Enter a unique Name for your topic, for example, MyTopic.
Command line
gcloud pubsub topics create TOPIC-NAME
Replace TOPIC-NAME with a topic name unique within your Google Cloud project.
Create a push subscription and associate it with the service account
After you create the Pub/Sub topic, you must subscribe your service to receive messages sent to a topic, and you must associate the subscription with the service account you created for your service. You can use either the Cloud Console or the gcloud command line:
Console
Go to the Pub/Sub topics page.
Click the topic you want to subscribe to.
Click Create Subscription to display the subscription form:
In the form,
- Specify the push delivery type.
- For Endpoints URL, specify your service's URL, which is displayed in the service detail page.
- In the Service Account dropdown, select the service account that you created with the required permissions.
- Set subscription expiration and acknowledgement deadline as desired.
- Click Create.
The subscription is complete. Messages posted to the topic will now be pushed into your service.
Command line
Allow Pub/Sub to create authentication tokens in your project:
gcloud projects add-iam-policy-binding PROJECT-ID \ --member=serviceAccount:service-PROJECT-NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com \ --role=roles/iam.serviceAccountTokenCreator
Replace
- PROJECT-ID with your Google Cloud project ID.
PROJECT-NUMBER with your Google Cloud project number.
Project ID and project number are listed in the Project info panel in the Cloud Console for your project.
Create a Pub/Sub subscription with the service account that you created with the required permissions:
gcloud beta pubsub subscriptions create SUBSCRIPTION-ID --topic TOPIC-NAME \ --push-endpoint=SERVICE-URL/ \ --push-auth-service-account=SERVICE-ACCOUNT-NAME@PROJECT-ID.iam.gserviceaccount.com
Replace
- TOPIC-NAME with the topic you previously created.
- SERVICE-URL with the HTTPS URL that was provided when
you deployed the service. You can find it by using the command
gcloud run services describe
, specifying the name of your service: look for the return line starting withdomain
. - PROJECT-ID with your Google Cloud project ID.
The
--push-auth-service-account
flag activates the Pub/Sub push functionality for Authentication and authorizationThe subscription is complete. Messages posted to the topic will now be pushed into your service. You can push a test message to the topic using the command:
gcloud pubsub topics publish TOPIC --message "hello"
Replace TOPIC with the name of the topic you created.
What's next
- See the Cloud Run tutorial for Pub/Sub for a complete sample application.
- See the Cloud Run tutorial for Cloud Storage for a sample using Pub/Sub to drive asynchronous image processing.
- See the Pub/Sub documentation for more details on Pub/Sub.