How to Create Subscription in Google Cloud Pub/Sub
To create a subscription in Google Cloud Pub/Sub, use the
gcloud pubsub subscriptions create command or the client library method createSubscription. You must specify the subscription name and the topic it subscribes to.Syntax
The basic syntax to create a subscription using the gcloud command-line tool is:
gcloud pubsub subscriptions create SUBSCRIPTION_NAME --topic=TOPIC_NAME
Here, SUBSCRIPTION_NAME is the name you want for your subscription, and TOPIC_NAME is the existing topic you want to subscribe to.
In client libraries, the method usually looks like createSubscription(subscriptionName) called on a topic object, where you provide the subscription name.
bash
gcloud pubsub subscriptions create SUBSCRIPTION_NAME --topic=TOPIC_NAME
Example
This example shows how to create a subscription named my-subscription to the topic my-topic using the gcloud CLI and Node.js client library.
bash and javascript
gcloud pubsub subscriptions create my-subscription --topic=my-topic // Node.js example const {PubSub} = require('@google-cloud/pubsub'); const pubSubClient = new PubSub(); async function createSubscription() { const topicName = 'my-topic'; const subscriptionName = 'my-subscription'; await pubSubClient.topic(topicName).createSubscription(subscriptionName); console.log(`Subscription ${subscriptionName} created.`); } createSubscription().catch(console.error);
Output
Subscription my-subscription created.
Common Pitfalls
Common mistakes when creating subscriptions include:
- Trying to create a subscription for a topic that does not exist.
- Using invalid characters or names for subscriptions or topics.
- Not having the right permissions to create subscriptions.
- Confusing subscription names with topic names.
Always verify the topic exists before creating a subscription and ensure your account has pubsub.subscriptions.create permission.
bash
gcloud pubsub subscriptions create my-subscription --topic=nonexistent-topic # Error: NOT_FOUND: Topic does not exist # Correct way: gcloud pubsub topics create my-topic gcloud pubsub subscriptions create my-subscription --topic=my-topic
Quick Reference
| Command or Parameter | Description |
|---|---|
| gcloud pubsub subscriptions create SUBSCRIPTION_NAME --topic=TOPIC_NAME | Create a new subscription to a topic |
| SUBSCRIPTION_NAME | Name of the subscription to create |
| TOPIC_NAME | Name of the existing topic to subscribe to |
| --ack-deadline | Optional: Set acknowledgment deadline in seconds |
| --push-endpoint | Optional: Set push endpoint URL for push subscriptions |
Key Takeaways
Use the gcloud command or client libraries to create Pub/Sub subscriptions by specifying subscription and topic names.
Ensure the topic exists before creating a subscription to avoid errors.
Check you have the necessary permissions to create subscriptions in your Google Cloud project.
Subscription names must be unique within the project and follow naming rules.
Use optional flags like acknowledgment deadline or push endpoint to customize subscription behavior.