This guide will walk you through setting up Google Cloud Pub/Sub step-by-step, including creating a service account, assigning roles, creating a topic, publishing messages, subscribing to them, and verifying the setup. A flowchart is also included to visualize the process.
gcloud iam service-accounts create pubsub-sa \
--display-name "Pub/Sub Service Account"
✅ Expected Output:
- A confirmation message will come
List all service accounts
gcloud iam service-accounts list
✅ Expected Output:
- A table displaying the service account's email, unique ID, and description.
Example:
NAME EMAIL DISABLED
pubsub-sa pubsub-sa@your-project-id.iam.gserviceaccount.com False
You need to assign permissions to the service account for Pub/Sub.
Assign the pubsub.publisher
role:
gcloud projects add-iam-policy-binding [PROJECT_ID] \
--member="serviceAccount:[SERVICE_ACCOUNT_EMAIL]" \
--role="roles/pubsub.publisher"
Assign the pubsub.subscriber
role:
gcloud projects add-iam-policy-binding [PROJECT_ID] \
--member="serviceAccount:[SERVICE_ACCOUNT_EMAIL]" \
--role="roles/pubsub.subscriber"
(If needed for managing topics, subscriptions, and permissions):
gcloud projects add-iam-policy-binding [PROJECT_ID] \
--member="serviceAccount:[SERVICE_ACCOUNT_EMAIL]" \
--role="roles/pubsub.admin"
✅ Expected Output:
- A message confirming the IAM policy update.
Example:
Updated IAM policy for project [devops01-450518].
bindings:
- members:
- serviceAccount:pubsub-sa@your-project-id.iam.gserviceaccount.com
role: roles/pubsub.admin
etag: BwYv0gyiths=
version: 1
Create a new topic where messages will be published:
gcloud pubsub topics create my-topic
✅ Expected Output:
- A confirmation message showing the topic was created.
Example:
Created topic [projects/your-project-id/topics/my-topic].
Send a message to the topic:
gcloud pubsub topics publish my-topic --message "Hello, Pub/Sub!"
✅ Expected Output:
- A confirmation message showing the message was published.
Example:
Message published.
Create a subscription to receive messages from the topic:
gcloud pubsub subscriptions create my-subscription \
--topic=my-topic
✅ Expected Output:
- A confirmation message showing the subscription was created.
Example:
Created subscription [projects/your-project-id/subscriptions/my-subscription].
Retrieve messages from the subscription:
gcloud pubsub subscriptions pull my-subscription --auto-ack
✅ Expected Output:
- The message content will be displayed in the terminal.
Example:
Received message: ID=123456789123456789
Data: 'Hello, Pub/Sub!'
To verify the roles and permissions:
gcloud projects get-iam-policy [PROJECT_ID]
✅ Expected Output:
- A list showing the assigned roles to the service account.
Example:
bindings:
- members:
- serviceAccount:pubsub-sa@your-project-id.iam.gserviceaccount.com
role: roles/pubsub.publisher
- members:
- serviceAccount:pubsub-sa@your-project-id.iam.gserviceaccount.com
role: roles/pubsub.subscriber
Here’s how the entire process works:
- Create a Service Account.
- Assign Roles (Publisher/Subscriber/Admin).
- Create a Topic.
- Create a Subscription linked to the topic.
- Publish Messages to the topic.
- Pull Messages from the subscription.
- Verify the setup and roles.
graph TD;
A[Create Service Account] --> B[Assign Roles];
B --> C[Create Topic];
C --> D[Create Subscription];
D --> E[Publish Message];
E --> F[Pull Message];
F --> G[Verify Setup];