You can invoke Cloud Run services and functions by setting up a Pub/Sub trigger.
When configuring the trigger, you link the trigger to a specific Pub/Sub topic. This ensures your service or function is called automatically whenever a message is published to that topic.
This guide covers how to configure direct Pub/Sub triggers in Cloud Run.
Before you begin
Make sure you have set up a new project for Cloud Run as described in the setup page.
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, Cloud Logging, and Pub/Sub APIs:
Required roles
Grant the required IAM roles and permissions.
Required roles for the deployer account
To get the permissions that you need to trigger from Pub/Sub events, ask your administrator to grant you the following IAM roles on your project:
- Cloud Run Admin (
roles/run.admin) - Pub/Sub Editor (
roles/pubsub.editor) - Logs View Accessor (
roles/logging.viewAccessor) - Project IAM Admin (
roles/resourcemanager.projectIamAdmin) - Service Account Admin (
roles/iam.serviceAccountAdmin) - Service Account User (
roles/iam.serviceAccountUser) - Service Usage Admin (
roles/serviceusage.serviceUsageAdmin)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Note that by default, Cloud Build permissions include permissions to upload and download Artifact Registry artifacts.
Create a Pub/Sub trigger
Console
In the Cloud de Confiance console, go to the Cloud Run Services page:
Click the service or function you want to create a Pub/Sub trigger for.
Select the Triggers tab.
Click Add Pub/Sub subscription.
In the Direct Pub/Sub subscription panel:
- Select the topic if you have already created one, or click Create new topic to create a new topic.
- In the Service account field, select the service account that the subscription uses to invoke your service. If the console lists missing roles, click Grant all.
- For the Service URL path, enter the path on your service
that should receive messages, such as
/(default) or/pubsub. The console adds this path to your service's URL.
Click Save.
gcloud
To create a trigger for your service or function using the gcloud CLI:
Deploy your service or function if you haven't already done so.
If you have an existing Pub/Sub topic, skip this step. Otherwise, create a Pub/Sub topic:
gcloud pubsub topics create TOPIC_NAME
Configure the required roles for the Pub/Sub service agent:
Required roles for the Pub/Sub service agent
The following steps describe how to create and use a service account that can be used by the Pub/Sub subscription to invoke your service or function. These steps are required when using the Google Cloud CLI to create a Pub/Sub trigger command. If you are using the Cloud de Confiance console to create the trigger, you can skip these steps.
-
Create or select a service account to represent the Pub/Sub subscription identity.
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME \ --display-name "Cloud Run Pub/Sub Invoker"
Replace
SERVICE_ACCOUNT_NAMEwith a name unique for your service account. -
Give the invoker service account permission to invoke your service:
gcloud run services add-iam-policy-binding SERVICE_NAME \ --member=serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.s3ns.iam.gserviceaccount.com \ --role=roles/run.invoker
It can take several minutes for the IAM changes to propagate. In the meantime, you might see
HTTP 403errors in the service logs.
-
Run the following command to create a Pub/Sub subscription with the service account:
gcloud pubsub subscriptions create SUBSCRIPTION_NAME \ --topic TOPIC_NAME \ --ack-deadline=600 \ --push-endpoint=SERVICE_URL/ \ --push-auth-service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.s3ns.iam.gserviceaccount.com
Replace the following:
SUBSCRIPTION_NAME: the name of the subscription you want to create.TOPIC_NAME: the name of the topic you previously created.SERVICE_URL: the HTTPS URL provided on deploying the service. This URL works even if you have also added a domain mapping.SERVICE_ACCOUNT_NAME: the name of the service account you created earlier.PROJECT_ID: your Cloud de Confiance by S3NS Cloud de Confiance project ID.
Notes:
- The
--ack-deadline=600flag sets the acknowledgment deadline to the 10-minute maximum. For more information, see Set the Pub/Sub acknowledgment deadline. - The
--push-auth-service-accountflag activates the Pub/Sub push functionality for Authentication and authorization. - Your Cloud Run service domain is automatically registered for use with Pub/Sub subscriptions.
- For Cloud Run only, there is a built-in authentication check that the token is valid and an authorization check that the service account has permission to invoke the Cloud Run service or function.
Next steps
- Learn how to trigger services from Pub/Sub.
- Learn how to configure retries.