This tutorial demonstrates how to deploy an HTTP 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 HTTP function as an example, but it also works with Python, Go, and Java HTTP functions. The instructions are the same regardless of which of these runtimes you are using.
Objectives
- Learn how to use Terraform to deploy an HTTP function.
Costs
In 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.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Run, 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Run, 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
-
Prepare your development environment.
If you already have the gcloud CLI installed, update it by running the following command:
gcloud components update
Setting up your environment
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 application
In 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 examples:
cd terraform-docs-samples/functions/basic
The Node.JS sample used in this tutorial is a basic "Hello World" HTTP function. Here is the
main.tf
file:
Initialize Terraform
In the terraform-docs-samples/functions/basic
directory containing the main.tf
file, run this command to add the necessary plugins and build the .terraform
directory:
terraform init
Apply the Terraform configuration
In the same terraform-docs-samples/functions/basic
directory containing the main.tf
file, deploy the function by applying the configuration. When prompted, enter
yes
:
terraform apply
Test the function
When the function finishes deploying, take note of the URI property or find it using the following command:
gcloud functions describe function-v2 --gen2 --region=us-central1 --format="value(serviceConfig.uri)"
Make a request to this URL to see your function's "Hello World" message. Note that the function is deployed requiring authentication. Therefore you must provide credentials in your request:
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" YOUR_FUNCTION_URL
Clean up
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 in the terraform-docs-samples/functions/basic
directory containing your main.tf
file:
terraform destroy
Enter yes
to allow Terraform to delete your resources.