How to create a Pub/Sub on Google Cloud (GCP)

You can create a Pub/Sub topic on Google Cloud with code or through the GCP Console, here’s how to do it
0 Shares
0
0
0

This post will help you create a Pub/Sub topic on Google Cloud using two approaches:

  1. Cloud Console
  2. Code
Code snippet

Permissions

To create or manage Pub/Sub topics you need to ask your GCP admin to concede you the role of Pub/Sub editor (roles/pubsub.editor) on a project level. More details on how to do that can be found here.

Code approach

For this approach to work, you’ll need to create a service account, generate and download a json key with your credentials and reference it in your code.

To do that, you’ll go to the service account tab on your GCP console, click on the desired service account and click on ADD KEY >> Create New Key. Then, you’ll download it on a JSON format and save the file on the folder you’ll be writing your script in.

Creating a key for a service account

If you don’t have a service account, you’ll need to create one. To do that, go to the service account tab on your GCP console, click on CREATE SERVICE ACCOUNT. Then, give it a name and the Pub/Sub editor permission.

Creating service account

Now that you already have permission, it’s code time.

The first thing you’ll need to do is to install the pubsub_v1 library. So open your terminal and type the following:

pip3 install google-cloud-pubsub

After installed, you’ll paste the following code. In this code, you’ll have to make 3 modifications:

  • Change [your_filename] with the name of your key file that you downloaded (your key file should be in the same directory as your script);
  • Change [your_project_id] with your project id (it’s usually displayed on the top left corner of your GCP console;
  • Change [your_topic_id] with how you want to name your PubSub.

import os 
from google.cloud import pubsub_v1 #pip3 install google-cloud-pubsub

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '[your_filename]'))

project_id = "[your_project_id]"
topic_id = "[your_topic_id]"

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)

topic = publisher.create_topic(request={"name": topic_path})

print(f"Created topic: {topic.name}")

After running the code, your Pub/Sub topic will be created and you can see it on your GCP Console.

Cloud Console approach

To create a Pub/Sub topic through the Console, you won’t need to create a service account, but you’ll need to have the Pub/Sub editor permission linked to your account.

If you already have the permission granted, go to the Pub/Sub tab and click on CREATE TOPIC. Then, you’ll name your topic, hit CREATE and you’re all set.

Creating a Pub/Sub topic through GCP console

If you need to create a standalone Pub/Sub topic, the easiest way is to do it through GCP console, as it’s pretty straightforward. However, when creating multiple topics, the code could be more handy as you can use a loop to create topics at scale.

0 Shares
You May Also Like

DataComo instalar o Apache Airflow

Neste artigo, vamos nos aprofundar em como instalar e configurar o Airflow no seu ambiente local. Esta abordagem também funcionará caso você precise ou queira instalar o Airflow em uma máquina virtual, tal como o Amazon EC2 ou Google Compute Engine.

DataThe role of an Analytics Engineer

Analytics engineers are responsible for building and maintaining data pipelines, which are essential for orchestrating the flow of data from various sources to destinations in a format that is readily accessible for decision-making.