Apply changes
To apply your Terraform configuration in a Google Cloud project, complete the following steps:
- Launch Cloud Shell.
- Set the Google Cloud project where you want to apply the Terraform configuration:
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
- Create a directory and a new file within that directory. The filename must have the
.tf
extension—for examplemain.tf
. In this tutorial, the file is referred to asmain.tf
.mkdir DIRECTORY && cd DIRECTORY && touch main.tf
Copy the sample code into the newly created
main.tf
.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:
terraform init
- Review the configuration and verify that the resources that Terraform is going to create or
update match your expectations:
terraform plan
Make corrections to the configuration as necessary.
- Apply the Terraform configuration by running the following command and entering
yes
at the prompt:terraform apply
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.
Reformat
To reformat your Terraform configuration in the standard style, enter the following command:
terraform fmt
Validate
To check whether your configuration is valid, enter the following command:
terraform validate
Delete changes
Remove resources previously applied with your Terraform configuration by running the following
command and entering yes
at the prompt:
terraform destroy
Specify the project ID
If you run the export GOOGLE_CLOUD_PROJECT
command as specified in the
previous section, most resources can infer the project_id
.
Some resources, such as project_iam_*
, cannot infer the project ID. As a
workaround, some samples use the data "google_project"
data source. You can replace this data source with the project ID string or a
variable.
For a sample that uses this workaround, see sql_instance_iam_condition.