gcloud CLI, and pull those messages from a pull subscription. It covers best practices for labels, retention, and message attributes so you can organize and filter messages efficiently.
1. Create a Topic in the GCP Console
- Open the GCP Console and search for “Pub/Sub”.
- From the Pub/Sub home page, click Create topic and choose a meaningful Topic ID (for this demo we use
kodekloud-demo-topic). Keep defaults unless you need a schema, ingestion setting, or export configuration. - Scroll down and click Create. Pub/Sub manages the underlying infrastructure for you.


2. Create a Subscription
From the left-hand menu click Subscriptions → Create subscription.- Subscription ID: e.g.
kodekloud-pull-sub - Topic:
kodekloud-demo-topic - Delivery type: Pull (this demo uses Pull)

Pull subscriptions require the client to explicitly request messages. Push subscriptions send messages to a configured endpoint. Choose the mode that suits your processing pattern.
3. Publish Messages with Cloud Shell and gcloud
Open Cloud Shell and set environment variables:publish_demo.sh to publish three JSON messages with attributes:
4. Pull Messages from the Subscription (Console)
- In the Pub/Sub Console go to Subscriptions → select
kodekloud-pull-sub. - Open Messages → Pull to trigger a pull request and view messages.
env: demo, team: kodekloud). You can use these attributes with subscription filters or in your subscriber logic to process only relevant messages.
If you update the script to change attributes (for example team=kodekloud → team=raghu) and publish again, the new attribute values will appear when pulling messages. This demonstrates how attributes enable flexible metadata-based routing for multiple producers sharing a topic.
Quick Reference
| Resource | Purpose | Example CLI |
|---|---|---|
| Topic | Message hub to which publishers send messages | gcloud pubsub topics create kodekloud-demo-topic |
| Subscription | Consumer view of a topic (Pull or Push) | gcloud pubsub subscriptions create kodekloud-pull-sub --topic=kodekloud-demo-topic --ack-deadline=10 |
| Message attributes | Key/value metadata for filtering/routing | --attribute env=demo |
Summary
- Create a topic in the GCP Console; add labels for easier organization.
- Create a subscription (choose Pull or Push based on your consumer architecture).
- Publish messages using the
gcloudCLI or client libraries; include attributes for metadata and filtering. - Pull messages from pull subscriptions (console, client libraries, or
gcloud). - Use subscription filters and message attributes to route and process only relevant messages.