
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Public Cloud Run service
/ 20
Private Cloud Run service
/ 20
PubSub Topic
/ 20
Service account
/ 20
PubSub Subscription
/ 20
Pub/Sub enables applications to take advantage of efficient message queues. The service is compatible with a range of Google Cloud services, and in this lab, you learn how to integrate it with Cloud Run.
This lab is based on resolving a customer use case by using serverless infrastructure. The lab features three high level sections that resolve a technical problem:
In this lab, you learn to:
These labs are based on intermediate knowledge of Google Cloud. While the steps required are covered in the content, it would be helpful to have familiarity with any of the following products:
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.
To complete this lab, you need:
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.
Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.
On the Choose an account page, click Use Another Account. The Sign in page opens.
Paste the username that you copied from the Connection Details panel. Then copy and paste the password.
After a few moments, the Cloud console opens in this tab.
Google 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.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
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:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
In this lab, you will help the development team at Critter Junction investigate the use of Pub/Sub for their requirements. The team would like to explore how to perform efficient queue processing within their applications.
The team at Critter Junction has a public web application and several microservices built on Google Cloud. Communication between the microservices is critical and needs a resilient form of messaging to be established between each application component.
The development team's previous attempts were unsuccessful due to the microservices needing to know a lot about each other ( High Coupling). In addition, if a service was temporarily unavailable, messages would be lost.
The team needs a solution that includes a level of resilience without introducing additional service dependencies (Low Coupling) into their systems. Now that you know a bit more about Critter Junction and the issues they face, try to prioritize the key criteria for a solution.
To ascertain the key use cases and priorities, initial discussions are held with the Critter Junction stakeholders. The results of the discussions are shown below:
Ref |
User Story |
1 |
As a lead developer, I want to ensure that messaging is resilient, so service operations will be restored without needing manual intervention. |
2 |
As a program manager, I want services to be capable of scaling seamlessly so additional transactional load does not lead to system instability. |
3 |
As an operations lead, I want services to be managed so staff does not need to be reassigned from important maintenance work. |
From a discussion with the team leads, the following high level tasks are defined:
Ref |
Definition of Done |
1 |
Establish an asynchronous component for inter-service communication. |
2 |
Implement the proven scalability of the solution. |
3 |
Services must run unsupervised. |
The team at Critter Junction is keen to define a solution that can be implemented quickly. In consideration of the requirements, the development team narrows their options down to:
See Pub/Sub versus Cloud Tasks to learn more.
Product |
Use case |
Choice |
Pub/Sub |
"Optimal for more general event data ingestion and distribution patterns where some degree of control over execution can be sacrificed." |
|
Cloud Tasks |
"Appropriate for use cases where a task producer needs to defer or control the execution timing of a specific webhook or remote procedure call." |
After considering the requirements, the development team chooses Pub/Sub because they only require a push based distribution pattern. The following high level architecture diagram summarizes the minimal viable product (MVP) that they need to investigate.
In the proposed solution, Pub/Sub will be used to handle asynchronous messages between services.
To ensure access to the necessary API, re-enable the Pub/Sub API.
In the Google Cloud console Navigation menu (), under APIs & Services, click Library.
In the Search box, type Pub/Sub
Click the result for Cloud Pub/Sub API.
Click Manage.
Click Disable API. If asked to confirm, click Disable.
Again, when prompted Do you want to disable Cloud Pub/Sub API and its dependent APIs?
, Click Confirm.
To re-enable the API, click Enable.
When the API has been re-enabled, the page displays information about the API.
Critter Junction has multiple Cloud Run services that they would like integrated with Pub/Sub. To build an MVP, the following tasks are required:
Critter Junction specifies that the externally facing store service should be configured as a public endpoint, indicating these requirements:
Type |
Permission |
Description |
URL Access |
--allow-unauthenticated |
Make the service PUBLIC (Unauthenticated users can see it). |
Invoke Permission |
allUsers |
Allow the service be invoked/triggered by anyone. |
The producer store service accepts public internet based connections for purchase orders. To do this, the service must not require authentication and must be able to be triggered by anyone.
Information collected by this service will be passed to the backend consumer services.
Configure and deploy the store service on Cloud Run. Execute the following commands in Cloud Shell.
Enable the Cloud Run API and configure your Shell environment:
Create a LOCATION environment variable:
Set the compute region:
Deploy the store service:
Click Check my progress to verify the objective.
Once the store service is deployed, the store service is publicly accessible over the internet.
The development team also needs to configure the order service that can be accessed at a private endpoint. Unlike the store service, the order service is not meant to be publicly accessible over the internet, and should only be invoked by an account with the appropriate permissions.
For Cloud Run based services, this can be achieved by using the following settings:
Type |
Permission |
Description |
URL Access |
--no-allow-unauthenticated |
Make the service PRIVATE (Only authenticated users can see it). |
Invoke Role/Permission |
Cloud Run Invoker |
Only allow the service to be invoked by an account with the Cloud Run Invoker role. |
Configure and deploy the order service:
Click Check my progress to verify the objective.
Now only authenticated accounts can access and invoke the service.
Pub/Sub is an asynchronous messaging service that decouples services that produce events from services that consume and process events.
Pub/Sub core concepts:
Pub/Sub requires a couple of options to be completed prior to successful deployment. In the Google Cloud console, Pub/Sub can be accessed under the Big Data menu option.
Field |
Description |
Topic |
A named resource to which messages are sent by publishers. |
Subscription |
A named resource representing the stream of messages from a single, specific topic, to be delivered to the subscribing application. For more details about subscriptions and message delivery semantics, see the Subscriber Guide. |
Message |
The combination of data and (optional) attributes that a publisher sends to a topic and is eventually delivered to subscribers. |
Message attribute |
A key-value pair that a publisher can define for a message. For example, key iana.org/language_tag and value en could be added to messages to mark them as readable by an English-speaking subscriber. |
Pub/Sub can be used in a wide variety of use cases, the most common of which are listed below:
Use Case |
Example |
Balancing workloads in network clusters |
For example, a large queue of tasks can be efficiently distributed among multiple workers, such as Compute Engine instances. |
Implementing asynchronous workflows |
For example, an order processing application can place an order on a topic, from which it can be processed by one or more workers. |
Distributing event notifications |
For example, a service that accepts user signups can send notifications whenever a new user registers, and downstream services can subscribe to receive notifications of the event. |
Refreshing distributed caches |
For example, an application can publish invalidation events to update the IDs of objects that have changed. |
Logging to multiple systems |
For example, a Google Compute Engine instance can write logs to the monitoring system, to a database for later querying, and so on. |
Data streaming from various processes or devices |
For example, a residential sensor can stream data to backend servers hosted in the cloud. |
Reliability improvement |
For example, a single-zone Compute Engine service can operate in additional zones by subscribing to a common topic, to recover from failures in a zone or region. |
Now that the producer (store service
) and consumer (order service
) services have been successfully deployed, you can focus on the main features of Pub/Sub. Using Pub/Sub requires two activities:
When an asynchronous (push) event is created on a topic, applications that subscribe to the topic will be able to process the associated messages. Push event processing with Pub/Sub provides a scalable way to handle messaging on Google Cloud.
The new Pub/Sub Topic will have following values.
Field |
Value |
Name |
ORDER_PLACED |
Encryption |
Google-managed key |
Click Check my progress to verify the objective.
By creating the Pub/Sub Topic, messages can now be independently stored and delivered in a resilient manner.
You'll create a subscription in a subsequent task.
To deliver a Pub/Sub message to a Cloud Run service, you need a Pub/Sub subscription. The subscription must be able to invoke the service using a service account with the appropriate permissions. In this lab, the consumer order service will be invoked by a subscription using the service account.
To achieve this functionality, the following activities are required:
Create a new service account that will provide authenticated access.
Create a new service account called Order Initiator:
Confirm that the service account has been created:
Click Check my progress to verify the objective.
At this point, the Order Initiator service account is available. However, it does not have a role or permissions assigned. To assign it IAM permissions, you need to apply or bind role permissions to the service account.
To bind permissions to an account that is used to invoke a service on Cloud Run, you need the following information:
Category |
Description |
Service Name |
The name of the deployed service to be invoked. |
Member |
The account to bestow the role permissions. |
Region |
The region in which the service is deployed. |
Platform |
The platform type (Cloud Run Managed, Cloud Run for Anthos, or Cloud Run for VMWare) |
Bind the service account with the role Cloud Run Invoker
on the order service:
The new service account has now been given permissions to invoke a Cloud Run service.
Create an environment variable to store the project number:
Enable the project service account to create tokens:
In this task, you create the Pub/Sub subscription and configure it to use the new service account.
Create an environment variable to store the endpoint of the order service:
Create a subscription and bind it to the order service:
Click Check my progress to verify the objective.
To test the application, send a sample JSON payload to the store service.
Create a file called test.json
with the following content. You can use your choice of editor such as nano
, vi
, or the Cloud Shell editor.
Create an environment variable to store the endpoint of the store service:
To test communication between the microservices and generate an order ID, post a message to the store service:
The output of the command indicates that an order had been successfully created, and should be similar to:
The store service (public endpoint) uses Pub/Sub to transmit information to the order service (private endpoint).
In the Google Cloud console Navigation menu (), click Cloud Run.
Click the link to the store-service.
To view the service logs, click Logs. Check the store service logs to view the order ID that was generated.
Add the log filter ORDER ID
to see the ID generated by the store service.
The order service receives a message from the store service passed with Pub/Sub.
Check the order service logs to confirm that the JSON data was successfully transferred.
Add the log filter Order Placed
to see the generated order ID that was passed to the order service.
Critter Junction have now updated their solution to take advantage of Pub/Sub. The following high level architecture diagram summaries the solution deployed.
You have successfully deployed Pub/Sub on Google Cloud to asynchronously communicate between Cloud Run services.
In this lab, you learned how to integrate Cloud Run services with Pub/Sub in your Google Cloud infrastructure. You learned how to:
Follow the Serverless Expeditions video series to learn more about how to use these products within your project.
Manual Last Updated Feb 20, 2024
Lab Last Tested Feb 20, 2024
Copyright 2022 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.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one