Stay organized with collections Save and categorize content based on your preferences.
This tutorial demonstrates how to deploy a Pub/Sub function by uploading a function source code zip file to a Cloud Storage bucket, using Terraform to provision the resources. Terraform is an open source tool that lets you provision Google Cloud resources with declarative configuration files
This tutorial uses a Node.js function as an example, but it also works with Python, Go, and Java functions. The instructions are the same regardless of which of these runtimes you are using. See Hashicorp's reference pages for details on using Terraform with the Cloud Functions v2 API.
ObjectivesIn this document, you use the following billable components of Google Cloud:
For details, see Cloud Run functions pricing.
To generate a cost estimate based on your projected usage, use the pricing calculator.
New Google Cloud users might be eligible for a
free trial.
Before you beginIn the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Build, Artifact Registry, and Cloud Storage APIs.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloud init
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Build, Artifact Registry, and Cloud Storage APIs.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloud init
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
roles/run.invoker
and the roles/cloudbuild.builds.builder
to the default compute service account.In this tutorial, you run commands in Cloud Shell. Cloud Shell is a shell environment with the Google Cloud CLI already installed, including the Google Cloud CLI, and with values already set for your current project. Cloud Shell can take several minutes to initialize:
Preparing the applicationIn Cloud Shell, perform the following steps:
Clone the sample app repository to your Cloud Shell instance:
git clone https://github.com/terraform-google-modules/terraform-docs-samples.git
Change to the directory that contains the Cloud Run functions sample code:
cd terraform-docs-samples/functions/pubsub
The Node.js sample used in this tutorial is a basic "Hello World" Pub/Sub function. Here is the main.tf
file:
In the terraform-docs-samples/functions/pubsub
directory containing the main.tf
file, run this command to add the necessary plugins and build the .terraform
directory:
terraform init
Validate the Terraform configuration
Preview the Terraform configuration. This step is optional, but it lets you verify that the syntax of main.tf
is correct. This command shows a preview of the resources that will be created:
terraform plan
Apply the Terraform configuration
Deploy the function by applying the configuration. When prompted, enter yes
:
terraform apply
Triggering the function
To test the Pub/Sub function:
Publish a message to the topic (in this example, the topic name is functions2-topic
):
gcloud pubsub topics publish TOPIC_NAME --message="Friend"
Read the function logs to see the result, where FUNCTION_NAME
is the name of your function (in this example, the function name is function
):
gcloud functions logs read FUNCTION_NAME
You should see logging output that includes your new "Friend" message.
Note: Logs might take a few moments to appear. If you don't see them immediately, check again in a minute or two.After completing the tutorial, you can delete everything that you created so that you don't incur any further costs.
Terraform lets you remove all the resources defined in the configuration file by running the terraform destroy
command:
terraform destroy
Enter yes
to allow Terraform to delete your resources.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["This tutorial guides you through deploying a Pub/Sub function using Terraform, which involves uploading a function's source code zip file to a Cloud Storage bucket."],["The process works for Node.js, Python, Go, and Java functions, with the instructions remaining consistent across these different runtimes."],["You will need to set up your environment using Cloud Shell, prepare your function application by cloning the sample and creating a zip file of its source code, create a main.tf file to configure terraform, and apply the configuration."],["The deployed Pub/Sub function can be tested by publishing a message to the specified topic, and then reading the function logs to verify the function's response to the message."],["You can remove all the resources created during the tutorial using the `terraform destroy` command to avoid incurring further costs."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4