To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud Shell
- Launch Cloud Shell.
Set the default Google Cloud project where you want to apply your Terraform configurations.
You only need to run this command once per project, and you can run it in any directory.
Environment variables are overridden if you set explicit values in the Terraform configuration file.
Prepare the directory
Each Terraform configuration file must have its own directory (also called a root module).
In Cloud Shell, create a directory and a new
file within that directory. The filename must have the
main.tf. In this tutorial, the file is referred to as
mkdir DIRECTORY && cd DIRECTORY && touch main.tf
If you are following a tutorial, you can copy the sample code in each section or step.
Copy the sample code into the newly created
Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.
- Review and modify the sample parameters to apply to your environment.
- Save your changes.
Initialize Terraform. You only need to do this once per directory.
Optionally, to use the latest Google provider version, include the
terraform init -upgrade
Apply the changes
Review the configuration and verify that the resources that Terraform is going to create or
update match your expectations:
Make corrections to the configuration as necessary.
Apply the Terraform configuration by running the following command and entering
yesat the prompt:
Wait until Terraform displays the "Apply complete!" message.
- Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.
To reformat your Terraform configuration in the standard style, enter the following command:
To check whether your configuration is valid, enter the following command:
Remove resources previously applied with your Terraform configuration by running the following
command and entering
yes at the prompt:
Specify the project ID
If you run the
export GOOGLE_CLOUD_PROJECT command, most resources can infer
Some resources, such as
project_iam_*, cannot infer the project ID. As a
workaround, some samples use the
data source. You can replace this data source with the project ID string or a
For a sample that uses this workaround, see sql_instance_iam_condition.