Learn about troubleshooting steps that you might find helpful if you run into problems using Pub/Sub.
Cannot create a topic
Verify that you have the necessary
permissions.
To create a Pub/Sub topic, you need
the Pub/Sub Editor (roles/pubsub.editor
) Identity and Access Management
role on the project. If you don't have this role, contact your administrator.
For more troubleshooting information regarding topics, see the following pages:
Cannot create a subscription
Check that you have done the following:
Verify that you have the necessary permissions. To create a Pub/Sub subscription, you need the Pub/Sub Editor (roles/pubsub.editor) IAM role on the project. If you don't have this role, contact your administrator.
Specified a name for the subscription.
Specified the name of an existing topic to which you want to attach the subscription.
If creating a push subscription, specified
https://
in lower case (nothttp://
orHTTPS://
) as the protocol for your receiving URL in thepushEndpoint
field.
For more troubleshooting information about subscriptions, see the following pages:
Troubleshoot permission issues
Pub/Sub permissions control which users and service accounts can perform actions on your Pub/Sub resources. When permissions are misconfigured, it can lead to permission denied errors and disrupt message flow. Audit logs provide a detailed record of all permission changes, allowing you to identify the source of these issues.
To troubleshoot Pub/Sub permission issues with audit logs:
Get the required permissions to view Logs Explorer.
For more information, see Before you begin.
In the Google Cloud console, go to the Logs Explorer page.
Select an existing Google Cloud project, folder, or organization.
Here is a list of filters that you can use to find relevant logs:
resource.type="pubsub_topic" OR resource.type="pubsub_subscription"
: Use this query as a starting point when you're troubleshooting any issue that might involve changes to topic or subscription configurations, or access control. You can combine it with other filters to further refine your search.protoPayload.methodName="google.iam.v1.SetIamPolicy"
: Use this query when you suspect that an issue is caused by incorrect or missing permissions. It helps you track who made changes to the IAM policy and what those changes were. This can be useful for troubleshooting issues like users unable to publish to topics or subscribe to subscriptions, applications denied access to Pub/Sub resources, or unexpected changes in access control.protoPayload.status.code=7
: Use this query when you encounter errors explicitly related to permissions. This helps you pinpoint which actions are failing and who is attempting them. You can combine this query with the previous ones to identify the specific resource and IAM policy change that might be causing the permission denial.
Analyze the logs to determine factors such as the timestamp of the event, the principal who made the change, and the type of changes that were made.
Based on the information gathered from the audit logs, you can take corrective actions.
Subscription got deleted
Pub/Sub subscriptions can be deleted in two primary ways:
A user or service account with sufficient permissions intentionally deletes the subscription.
A subscription is automatically deleted after a period of inactivity, which is 31 days by default. For more information about the subscription expiration policy, see Expiration period.
To troubleshoot a deleted subscription, perform the following steps:
In the Google Cloud console, go to the Pub/Sub subscriptions page and verify that the subscription is no longer listed. For more information on how to list subscriptions, see List a subscription.
Check the audit logs. Navigate to Logs Explorer. Use the filter
protoPayload.methodName="google.pubsub.v1.Subscriber.DeleteSubscription"
to find deleted subscriptions. Examine the logs to determine if someone deleted the subscription or it was deleted due to inactivity.InternalExpireInactiveSubscription
indicates a subscription was deleted due to inactivity. For more information on how to use audit logs for troubleshooting, see Troubleshoot Pub/Sub issues with audit logs.
403 (Forbidden)
error
If you get this error, do the following:
- Make sure you've enabled the Pub/Sub API in the Google Cloud console.
Make sure that the principal making the request has the required permissions on the relevant Pub/Sub API resources, especially if you are using Pub/Sub API for cross-project communication.
If you're using Dataflow, make sure that both
{PROJECT_NUMBER}@cloudservices.gserviceaccount.com
and the Compute Engine Service account{PROJECT_NUMBER}-compute@developer.gserviceaccount.com
have the required permissions on the relevant Pub/Sub API resource. For more information, see Dataflow Security and Permissions.If you're using App Engine, check your project's Permissions page to see if an App Engine Service Account is listed as a Pub/Sub Editor. If it is not, add your App Engine Service Account as a Pub/Sub Editor. Normally, the App Engine Service Account is of the form
<project-id>@appspot.gserviceaccount.com
.
Other common error codes
For a list of other common error codes related to the Pub/Sub API and their descriptions, see Error Codes.
Using excessive administrative operations
If you find that you're using up too much of your
quota for administrative operations,
you might need to refactor your code. As an illustration, consider
this pseudo-code. In this example, an administrative operation (GET
)
is being used to check for the presence of a subscription before it
attempts to consume its resources. Both GET
and CREATE
are administrator operations:
if !GetSubscription my-sub {
CreateSubscription my-sub
}
Consume from subscription my-sub
A more efficient pattern is to try to consume messages from the subscription (assuming that you can be reasonably sure of the subscription's name). In this optimistic approach, you only get or create the subscription if there is an error. Consider this example:
try {
Consume from subscription my-sub
} catch NotFoundError {
CreateSubscription my-sub
Consume from subscription my-sub
}
You can use the following code samples to implement this pattern in the language of your choice:
Go
Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub Go API reference documentation.
To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub Java API reference documentation.
To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub Node.js API reference documentation.
To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub Python API reference documentation.
To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C++
Before trying this sample, follow the C++ setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub C++ API reference documentation.
To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js (TypeScript)
Before trying this sample, follow the Node.js setup instructions in the Pub/Sub quickstart using client libraries. For more information, see the Pub/Sub Node.js API reference documentation.
To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.