menu
arrow_back

Google Cloud Pub/Sub: Qwik Start - Command Line

search favorite_border
Add to favorites
help
Help
—/10

Checkpoints

arrow_forward

Create a Pub/Sub topic

Create Pub/Sub Subscription

Google Cloud Pub/Sub: Qwik Start - Command Line

40 minutes Free

GSP095

Google Cloud Self-Paced Labs

Overview

Google Cloud 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. Google Cloud Pub/Sub delivers low-latency/durable messaging, and is commonly used by developers in implementing asynchronous workflows, distributing event notifications, and streaming data from various processes or devices.

In this lab, you will do the following:

  • Learn the basics of Pub/Sub.

  • Create, delete, and list Pub/Sub topics.

  • Create, delete, and list Pub/Sub subscriptions.

  • Publish messages to a topic.

  • Use a pull subscriber to output individual topic messages.

  • Use a pull subscriber with a flag to output multiple messages.

Prerequisites

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

Before taking this lab, consider your proficiency with Pub/Sub. Below is a list of more challenging labs that will let you apply your knowledge of Pub/Sub to different cloud services and use cases:

Once you're ready, scroll down and follow the steps to get your lab environment set up.

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 will be made available to you.

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

What you need

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
  • Time to complete the lab.

Note: If you already have your own personal Google Cloud account or project, do not use it for this lab.

Note: If you are using a Pixelbook, open an Incognito window to run this lab.

Now that you've started your lab, you'll log in to the Google Cloud Shell console, then launch the command line tool.

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 pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.

    Open Google Console

  2. Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Sign in

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

  3. In the Sign in page, paste the username that you copied from the Connection Details panel. Then copy and paste the password.

    Important: You must use the credentials from the Connection Details panel. Do not use your Qwiklabs credentials. If you have your own Google Cloud account, do not use it for this lab (avoids incurring charges).

  4. 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 Cloud Console opens in this tab.

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.

In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

Click Continue.

cloudshell_continue.png

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Cloud Shell Terminal

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

You can list the active account name with this command:

gcloud auth list

(Output)

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)

(Example output)

Credentialed accounts:
 - google1623327_student@qwiklabs.net

You can list the project ID with this command:

gcloud config list project

(Output)

[core]
project = <project_ID>

(Example output)

[core]
project = qwiklabs-gcp-44776a13dea667a6

The Pub/Sub basics

As stated earlier, Google Cloud Pub/Sub is an asynchronous global messaging service. There are three terms in Pub/Sub that 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 Cloud Pub/Sub topic.

  • Subscribers make a "subscription" to a topic where they will either pull messages from the subscription or configure webhooks for push subscriptions. Every subscriber must acknowledge each message within a configurable window of time.

To sum it up, a producer publishes messages to a topic and a consumer creates a subscription to a topic to receive messages from it.

Pub/Sub topics

Pub/Sub comes preinstalled in the Google Cloud Shell, so there are no installations or configurations required to get started with this service.

Run the following command to create a topic called myTopic:

gcloud pubsub topics create myTopic

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Create a Pub/Sub topic.

For good measure, create two more topics; one called Test1 and the other called Test2:

gcloud pubsub topics create Test1
gcloud pubsub topics create Test2

To see the three topics you just created, run the following command:

gcloud pubsub topics list

Your output should resemble the following:

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

Time to cleanup. Delete Test1 and Test2 by running the following commands:

gcloud pubsub topics delete Test1
gcloud pubsub topics delete Test2

Run the gcloud pubsub topics list command one more time to verify the topics were deleted:

gcloud pubsub topics list

You should get the following output:

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

Pub/Sub subscriptions

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

Run the following command to create a subscription called mySubscription to topic myTopic:

gcloud  pubsub subscriptions create --topic myTopic mySubscription

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will granted with an assessment score.

Create Pub/Sub Subscription.

Add another two subscriptions to myTopic. Run the following commands to make Test1 and Test2 subscriptions:

gcloud  pubsub subscriptions create --topic myTopic Test1
gcloud  pubsub subscriptions create --topic myTopic Test2

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

Below are a multiple choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.

Now delete the Test1 and Test2 subscriptions. Run the following commands:

gcloud pubsub subscriptions delete Test1
gcloud pubsub subscriptions delete Test2

See if the Test1 and Test2 subscriptions were deleted. Run the list-subscriptions command one more time:

gcloud pubsub topics list-subscriptions myTopic

You should get the following output:

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

Pub/Sub Publishing and Pulling a Single Message

Next you'll learn how to publish a message to a Pub/Sub topic.

Run the following command to publish the message "hello" to the topic you created previously (myTopic):

gcloud pubsub topics publish myTopic --message "Hello"

Publish a few more messages to myTopic. Run the following commands (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, meaning it should work because earlier you set up the subscription mySubscription to the topic myTopic.

Use the following command to pull the messages you just published from the Pub/Sub topic:

gcloud pubsub subscriptions pull mySubscription --auto-ack

Your output should resemble the following:

food.png

What's going on here? You published 4 messages to your topic, but only 1 was outputted.

Now is an important time to note a couple features of the pull command that often trip developers up:

  • Using the pull command without any flags will output only one message, even if you are subscribed to a topic that has more held in it.
  • Once an individual message has been outputted from a particular subscription-based pull command, you cannot access that message again with the pull command.

To see what the second bullet is talking about, run the last command three more times. You will see that it will output the other messages you published before.

Now, run the command a 4th time. You'll get the following output (since there were none left to return):

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

In the last section, you will learn how to pull multiple messages from a topic with a flag.

Pub/Sub pulling all messages from subscriptions

Since you pulled all of the messages from your topic in hte last example, populate myTopic with a few more messages.

Run the following commands:

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"

Add a flag to your command so you can output all three messages in one request. You may have not noticed, but you have actually been using a flag this entire time: the --auto-ack part of the pull command is a flag that has been formatting your messages into the neat boxes that you see your pulled messages in.

limit is another flag that sets an upper limit on the number of messages to pull.

Wait a minute to let the topics get created. Run the pull command with the limit flag:

gcloud pubsub subscriptions pull mySubscription --auto-ack --limit=3

Your output should match the following:

cli_PS.png

Now you know how to add flags to a Pub/Sub command to output a larger pool of messages. You are well on your way to becoming a Pub/Sub master.

Congratulations!

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

5edc9b3763bbb596.png

Finish Your Quest

Continue your Quest with Baseline: Infrastructure. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in this Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.

Take Your Next Lab

This lab is part of a series of labs called Qwik Starts. These labs are designed to give you a little taste of the many features available with Google Cloud. Search for "Qwik Starts" in the lab catalog to find the next lab you'd like to take!

Next Steps /Learn More

Google Cloud Training & 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 September 9, 2019
Lab Last Tested September 9, 2019

Copyright 2021 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.