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
Deploy the function
/ 100
Deploy the function
/ 100
A Cloud Run function is a piece of code that runs in response to an event, such as an HTTP request, a message from a messaging service, or a file upload. Cloud events are things that happen in your cloud environment. These might be things like changes to data in a database, files added to a storage system, or the creation of a new virtual machine instance.
Because Cloud Run functions are event-driven, they only run when something happens. This makes them a good choice for tasks that need to run quickly or that don't need to run all the time.
For example, you can use a Cloud Run function to:
You can write your code in any language that supports Node.js, and you can deploy your code to the cloud with a few clicks. After you deploy your Cloud Run function, it automatically starts running in response to events.
This hands-on lab shows you how to create, deploy, and test a Cloud Run function using the Cloud Shell command line.
In this lab, you learn how to perform the following tasks:
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:
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:
Note that the lab timer is located near the top of the page, showing the remaining time.
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.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab setup and access panel.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab setup and access panel.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
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.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Output:
gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
First, you create a simple function named helloPubSub. This function writes a message to the Cloud Run functions logs. Cloud Run function events trigger the function, which accepts a callback function to signal completion.
For this lab, the Cloud Run function event is a Pub/Sub topic event. Pub/Sub is a messaging service that decouples message senders from message receivers. When a sender publishes a message, a receiver needs a subscription to receive an alert and the message. To learn more about Pub/Sub, in the Pub/Sub guides, see Pub/Sub: A Google-Scale Messaging Service.
To learn more about the event parameter and the callback parameter, in the Cloud Run functions documentation, see Background Functions.
To create a Cloud Run function:
In Cloud Shell, run the following command to set the default region:
Run the following command to create a directory for the function code and navigate to it:
Run the following command to create and open index.js for editing:
Copy the following into the index.js file:
To save the file and exit nano, press CTRL+X, then press Y, and then press ENTER.
Run the following command to create and open package.json for editing:
Copy the following into the package.json file:
To save the file and exit nano, press CTRL+X, then press Y, and then press ENTER.
Run the following command to install the package dependencies:
Expected output:
For this lab, you set the --trigger-topic flag to cf-demo.
--trigger-topic, --trigger-bucket, or --trigger-http are common trigger events.
When you deploy an update to an existing function, the function keeps the existing trigger unless you specify otherwise.
Run the following command to deploy the nodejs-pubsub-function function to a Pub/Sub topic named cf-demo:
roles/iam.serviceAccountTokenCreator binding, so a permission prompt might not appear during deployment. Proceed normally if no prompt appears; if a serviceAccountTokenCreator prompt appears, then type n and press ENTER.
Run the following command to verify the status of the function:
An ACTIVE status indicates that your function deployed successfully.
Expected output:
Every message published to the topic triggers function execution and passes the message contents as input data.
Click Check my progress to verify your performed task. If you complete the task successfully, then you receive an assessment score.
After you deploy the function and know that it's active, test that the function writes a message to the cloud log after detecting an event.
Run the following command to publish a message to the cf-demo Pub/Sub topic:
Example output:
View logs to confirm that there are log messages with that execution ID.
Run the following command to check the logs and view your messages in the log history:
The Cloud Run function outputs information similar to the following:
You deployed and tested your application, and viewed the logs.
Answer the following question to reinforce your understanding of the concepts in this lab.
You used Cloud Shell to create, deploy, and test a Cloud Run function using the command line.
This lab is part of a series of labs called Qwik Starts. These labs give you a brief introduction to the many features available with Google Cloud. Search for "Qwik Starts" in the lab catalog to find the next lab you want to take!
...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 on November 23, 2026
Lab Last Tested November 22, 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.
This content is not currently available
We can notify you via email when it becomes available
Great!
We can contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one