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
Set up Firestore
/ 20
Develop an event-driven function for new Firestore documents
/ 20
Develop an event-driven function for Firestore to update a document
/ 30
Use Secrets with Cloud Run functions
/ 30
Cloud Run functions can extend your applications and services by integrating with Google Cloud databases namely Firestore, Cloud Spanner, Cloud SQL, Cloud Bigtable and with Memorystore, Google Cloud's in-memory datastore cache service.
In this lab, you create Cloud Run functions that integrate with Firestore, Google Cloud's serverless NoSQL document database. You'll use the Cloud Run functions Framework and Firestore client library for Node.js to create functions, and set up triggers to execute them when events occur in the database.
A Firestore function's lifecycle typically involves these steps:
In this lab, you will:
This Google Skills 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:
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 task, you set up environment variables and enable relevant service APIs that are needed to perform this lab.
Before you create Cloud Run functions, you set some environment variables.
Sign in to the Google Cloud console with your lab credentials, and open the Cloud Shell terminal window.
Run the following command in Cloud Shell to set your Project ID and REGION environment variables.
Set an environment variable for the Project Number:
Set the default region for Cloud Run functions:
To enable service APIs that are needed for this lab, run the following command:
To perform the tasks in this lab, you need to set up a Firestore database. Firestore stores data in the form of documents and collections. To use Cloud Run functions with Firestore, you must first set up Firestore before deploying the functions.
In the Google Cloud Console, click the Search bar in the top navigation and type Firestore. Select Firestore from the search results.
Click Create a Firestore database.
Select Standard Edition.
Under Configuration options, select Firestore in Native mode.
For Security rules, choose Open.
In Location type, click Region, and then select the lab region
Leave the other settings as their defaults, and click Create Database.
Click Check my progress to verify the objective.
After your Firestore database is created, you can develop your function code. In this task, you write your function's source code that responds to the creation of new documents in the database. The function logs information about the data received in the function invocation.
Firestore functions are invoked with a cloudevents data structure that can be decoded using Protocol Buffers with the protobuf.js NPM module. For more information see the links that are provided at the end of the lab.
To copy the required .proto and dependency files from Cloud Storage and extract the contents from the archive, run the following command:
Change to the firestore_functions directory:
The firestore_functions directory also contains empty node.js and package.json files which you will update in the next subtask.
In the Cloud Shell toolbar, click Open Editor.
In the editor, add the following code to the firestore_functions/index.js file:
In the editor, add the following to the firestore_functions/package.json file:
Grant the Cloud Run functions service agent certain permissions before deploying the function. Run the following commands in Cloud Shell.
Click Open Terminal.
Set an environment variable for the Cloud Run functions service agent's service account:
To view and get artifacts from Artifact Registry, grant the artifactregistry.reader role to the Cloud Run functions service account:
Disable the Cloud Functions API:
Re-enable the Cloud Functions API:
Wait a few seconds, and then rerun the command to grant the artifactregistry.reader role to the Cloud Run functions service account:
To deploy the function, run the following command from Cloud Shell:
After the command executes successfully, the command generates the URL for the function endpoint, as shown in this sample partial command output:
Navigate to Firestore Studio in the Cloud console.
To create a new document collection, click Start collection.
For Collection ID, type customers
To generate an ID for a document in this collection, click into Document ID.
For this document, add a field with the following values:
| Field name | Field type | Field value |
|---|---|---|
| firstname | string | Lucas |
Click Save.
To verify that your Cloud Run function was invoked, on the Navigation menu (), click Cloud Run.
Click the function name newCustomer.
Click Logs.
Verify that the log entries generated from the function code are present and display the data from the database document that you created.
You might need to click Refresh to view the latest log entries.
Click Check my progress to verify the objective.
In this task, you develop a function that is triggered when a document is updated in the Firestore database. Your function adds a new field to the document with a value that is derived from the values of some of the other document's fields.
In the editor, add the following code below in the firestore_functions/index.js file:
With this approach, every function may share the same set of dependencies even if some of those functions do not need those dependencies.
To minimize the number of dependencies needed for a particular function and reduce it's memory requirements, it is recommended to keep each function's source code in it's own top-level directory with it's own project configuration files.
To deploy the new function, run the following command from Cloud Shell:
Verify the command output indicating that the function has been deployed and the state is Active.
In the Cloud Console, in Firestore Studio, select the existing documents in the customers collection with a firstname field value of Lucas.
For this document, click Add Field.
Add a field with the following values:
| Field name | Field type | Field value |
|---|---|---|
| lastname | string | Sherman |
Click Save.
Wait for a few seconds, and then verify that you see a new field fullname is added to the document.
This indicates that your function updateCustomer was invoked when the document was updated.
To verify that your Cloud Run function was invoked, on the Navigation menu (), click Cloud Run.
Click the function name updateCustomer.
Click Logs.
Verify that the log entries generated from the function code are present that indicate that the fullname field was added to the document.
You might need to click Refresh to view the latest log entries.
Click Check my progress to verify the objective.
Secret Manager is a Google Cloud service that securely stores data like API keys, passwords, certificates, credentials, and other sensitive information. You can then access these secrets from Cloud Run functions or other services for use in your function logic or service implementation.
In this task, you create and store a credential as a secret in Secret Manager. You develop a function to access the key in your function logic.
To create and use secrets, run the following command in Cloud Shell and enable the Secret Manager API:
Create and store a secret named api-cred with value secret_api_key in Secret Manager:
To access a secret, your function's runtime service account must be granted access to the secret.
By default, Cloud Run functions uses the Compute Engine default service account as a function's runtime service account.
To authenticate with Secret Manager, grant the Secret Manager Secret Accessor role to the Compute Engine default service account:
In this subtask, you modify the previously developed newCustomer function to access the secret.
In the editor, add the following code to the newCustomer function in the index.js file. Add the code at the end of the function after the last console.log statement in the function body:
In Cloud Shell, redeploy the newCustomer function with the secret:
By referencing a secret as a volume, your function accesses the latest secret value from Secret Manager each time the file is read from disk.
After the function is deployed, verify that it has access to the secret:
The output from the describe command includes information about the secret. Here's a partial output from the command:
To test the function, repeat the test from the previous task to add a new customer document from Firestore Studio in Cloud console.
To view the function's logs in the Cloud console, on the Navigation menu (), click Cloud Run.
Click the newCustomer function name.
To view the function's logs, click Logs.
Verify that the entry to log the value of the secret key is present:
Click Check my progress to verify the objective.
In this lab, you set up a Firestore database, and developed an event-driven cloud function that is triggered when a new document is created in the database. You also developed a function to add a new field to a document when that document is updated. You also created and accessed a secret from a Cloud Run function and used logs to verify the secret value.
To learn more about Cloud Run functions for Firestore and other topics, view the documentation:
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 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