Create a Pub/Sub topic

Vérifier ma progression

/ 5

Create Pub/Sub Subscription

Vérifier ma progression

/ 5

Cet atelier peut intégrer des outils d'IA pour vous accompagner dans votre apprentissage.
Ce contenu n'est pas encore optimisé pour les appareils mobiles.
Pour une expérience optimale, veuillez accéder à notre site sur un ordinateur de bureau en utilisant un lien envoyé par e-mail.

GSP095

Google Cloud self-paced labs

Overview

Pub/Sub is a messaging service for exchanging event data among applications and services. By decoupling senders and receivers, it allows for secure and highly available communication between independently written applications. Pub/Sub delivers low-latency, durable messaging, and developers commonly use it to implement asynchronous workflows, distribute event notifications, and stream data from various processes or devices.

What you'll learn

In this lab, you learn how to perform the following tasks:

  • Create, delete, and list Pub/Sub topics and subscriptions.
  • Publish messages to a topic.
  • Use a pull subscriber to output topic messages.

Prerequisites

This is an introductory-level lab. It assumes little or no prior experience with Pub/Sub, and it teaches you the basics of setting up and using this Google Cloud service.

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.

This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the right is the Lab setup and access panel with the following:

    • The Open Google Cloud console button
    • The temporary credentials (username and password) that you must use for this lab
    • Other information, if needed, to step through this lab

    Note that the lab timer is located near the top of the page, showing the remaining time.

  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab setup and access panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab setup and access panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field. Navigation menu icon and Search field

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

  2. Click through the following windows:

    • Continue through the Cloud Shell information window.
    • Authorize Cloud Shell to use your credentials to make Google Cloud API calls.

When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Pub/Sub basics

As stated earlier, Pub/Sub is an asynchronous global messaging service. Three terms in Pub/Sub appear often: topics, publishing, and subscribing.

  • A topic is a shared string that allows applications to connect with one another through a common thread.
  • Publishers push (or publish) a message to a Pub/Sub topic.
  • Subscribers create a subscription to a topic, where they either pull messages from the subscription or configure webhooks for push subscriptions. Every subscriber must acknowledge each message within a configurable window of time.

In summary, a producer publishes messages to a topic, and a consumer creates a subscription to a topic to receive messages from it.

Task 1. Create and manage Pub/Sub topics

The gcloud pubsub command-line tool comes preinstalled in Cloud Shell, so no additional installations or configurations are required to get started with this service.

  1. Run the following command to create a topic called myTopic:
gcloud pubsub topics create myTopic

Click Check my progress to verify the objective.

Create a Pub/Sub topic.
  1. Create two additional topics, one called Test1 and the other called Test2:
gcloud pubsub topics create Test1 gcloud pubsub topics create Test2
  1. To see the three topics you created, run the following command:
gcloud pubsub topics list

Your output should resemble the following:

--- messageStoragePolicy: allowedPersistenceRegions: - us-central1 name: projects/qwiklabs-gcp-01-af5b4aaa2d32/topics/myTopic --- messageStoragePolicy: allowedPersistenceRegions: - us-central1 name: projects/qwiklabs-gcp-01-af5b4aaa2d32/topics/Test1 --- messageStoragePolicy: allowedPersistenceRegions: - us-central1 name: projects/qwiklabs-gcp-01-af5b4aaa2d32/topics/Test2
  1. Delete the Test1 and Test2 topics by running the following commands:
gcloud pubsub topics delete Test1 gcloud pubsub topics delete Test2
  1. Run the gcloud pubsub topics list command again to verify that you deleted the topics:
gcloud pubsub topics list

Your output should resemble the following:

--- name: projects/qwiklabs-gcp-3450558d2b043890/topics/myTopic

Task 2. Create and manage Pub/Sub subscriptions

Now that you're comfortable creating, viewing, and deleting topics, you can work with subscriptions.

  1. Run the following command to create a subscription called mySubscription to the myTopic topic:
gcloud pubsub subscriptions create --topic myTopic mySubscription

Click Check my progress to verify the objective.

Create Pub/Sub Subscription.
  1. Run the following commands to add two more subscriptions, Test1 and Test2, to myTopic:
gcloud pubsub subscriptions create --topic myTopic Test1 gcloud pubsub subscriptions create --topic myTopic Test2
  1. Run the following command to list the subscriptions to myTopic:
gcloud pubsub topics list-subscriptions myTopic

Your output should resemble the following:

--- projects/qwiklabs-gcp-3450558d2b043890/subscriptions/Test2 --- projects/qwiklabs-gcp-3450558d2b043890/subscriptions/Test1 --- projects/qwiklabs-gcp-3450558d2b043890/subscriptions/mySubscription

Test your understanding

Answer the following question to reinforce your understanding of this lab's concepts.

  1. Run the following commands to delete the Test1 and Test2 subscriptions:
gcloud pubsub subscriptions delete Test1 gcloud pubsub subscriptions delete Test2
  1. Run the list-subscriptions command again to verify that you deleted the Test1 and Test2 subscriptions:
gcloud pubsub topics list-subscriptions myTopic

Your output should resemble the following:

--- projects/qwiklabs-gcp-3450558d2b043890/subscriptions/mySubscription

Task 3. Publish and pull a single message

In this task, you publish messages to a Pub/Sub topic and pull them using your subscription.

  1. Run the following command to publish the message "Hello" to the topic you created previously (myTopic):
gcloud pubsub topics publish myTopic --message "Hello"
  1. Run the following commands to publish a few more messages to myTopic, replacing <YOUR NAME> with your name and <FOOD> with a food you like to eat:
gcloud pubsub topics publish myTopic --message "Publisher's name is <YOUR NAME>" gcloud pubsub topics publish myTopic --message "Publisher likes to eat <FOOD>" gcloud pubsub topics publish myTopic --message "Publisher thinks Pub/Sub is awesome"

Next, use the pull command to get the messages from your topic. The pull command is subscription-based, so it works with the mySubscription subscription that you set up earlier for the myTopic topic.

  1. Run the following command to pull the messages you published from the Pub/Sub topic:
gcloud pubsub subscriptions pull mySubscription --auto-ack

Your output should resemble the following:

Three-column table with the headings: Data, Message_ID, and Attributes. The Data column contains the following: Publisher likes to eat <FOOD>.

Notice that even though you published four messages to your topic, the command outputs only one message. Note the following behaviors of the pull command:

  • Using the pull command without any flags outputs only one message, even if you are subscribed to a topic that holds more messages.
  • After a subscription-based pull command outputs an individual message, you cannot access that message again with the pull command.
  1. Run the previous pull command three more times to output the remaining messages that you published earlier.

  2. Run the command a fourth time:

gcpstaging20394_student@cloudshell:~ (qwiklabs-gcp-3450558d2b043890)$ gcloud pubsub subscriptions pull mySubscription --auto-ack Listed 0 items.

Because no messages remain in the subscription, the command returns Listed 0 items.. In the next task, you pull multiple messages from a topic by adding a flag.

Task 4. Pull all messages from a subscription

Because you pulled all of the messages from your topic in the previous task, populate myTopic with a few more messages.

  1. Run the following commands to publish three new messages to myTopic:
gcloud pubsub topics publish myTopic --message "Publisher is starting to get the hang of Pub/Sub" gcloud pubsub topics publish myTopic --message "Publisher wonders if all messages will be pulled" gcloud pubsub topics publish myTopic --message "Publisher will have to test to find out"

To output all three messages in one request, you can add a flag to your command. You might not have noticed, but you have already used a flag in this lab: the --auto-ack part of the pull command is a flag that formats your pulled messages into a table, and --limit is another flag that sets an upper limit on the number of messages to pull.

  1. Wait a minute for Pub/Sub to process the messages, and then run the pull command with the --limit flag:
gcloud pubsub subscriptions pull mySubscription --limit=3

Your output should match the following:

Three-column table with the headings: Data, Message_ID, and Attributes. The Data column contains three lines of data.

Now you know how to add flags to a Pub/Sub command to output a larger pool of messages.

Congratulations!

In this lab, you learned the basics of Pub/Sub topics, subscriptions, the pull command, and flags.

Take your next lab

This lab is part of a series of labs called Qwik Starts. These labs are designed to give you a brief introduction to the many features available with Google Cloud. Search for "Qwik Starts" in the Google Cloud Skills Boost catalog to find the next lab you want to take!

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated November 11, 2026

Lab Last Tested November 11, 2026

Copyright 2026 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Avant de commencer

  1. Les ateliers créent un projet Google Cloud et des ressources pour une durée déterminée.
  2. Les ateliers doivent être effectués dans le délai imparti et ne peuvent pas être mis en pause. Si vous quittez l'atelier, vous devrez le recommencer depuis le début.
  3. En haut à gauche de l'écran, cliquez sur Démarrer l'atelier pour commencer.

Utilisez la navigation privée

  1. Copiez le nom d'utilisateur et le mot de passe fournis pour l'atelier
  2. Cliquez sur Ouvrir la console en navigation privée

Connectez-vous à la console

  1. Connectez-vous à l'aide des identifiants qui vous ont été attribués pour l'atelier. L'utilisation d'autres identifiants peut entraîner des erreurs ou des frais.
  2. Acceptez les conditions d'utilisation et ignorez la page concernant les ressources de récupération des données.
  3. Ne cliquez pas sur Terminer l'atelier, à moins que vous n'ayez terminé l'atelier ou que vous ne vouliez le recommencer, car cela effacera votre travail et supprimera le projet.

Ce contenu n'est pas disponible pour le moment

Nous pouvons vous prévenir par e-mail lorsqu'il sera disponible

Parfait !

Nous pouvons vous contacter par e-mail s'il devient disponible

Un atelier à la fois

Confirmez pour mettre fin à tous les ateliers existants et démarrer celui-ci

Utilisez la navigation privée pour effectuer l'atelier

Le meilleur moyen d'exécuter cet atelier consiste à utiliser une fenêtre de navigation privée. Vous éviterez ainsi les conflits entre votre compte personnel et le compte temporaire de participant, qui pourraient entraîner des frais supplémentaires facturés sur votre compte personnel.