What is the purpose of the Terraform GCP provider?
The Terraform GCP provider allows Terraform to manage and interact with Google Cloud Platform resources by translating Terraform configurations into API calls to GCP.
Click to reveal answer
beginner
How do you specify the GCP project and region in the Terraform provider configuration?
You specify the project and region inside the provider block using the 'project' and 'region' attributes, for example:
provider "google" {
project = "my-gcp-project"
region = "us-central1"
}
Click to reveal answer
intermediate
What authentication methods can Terraform use to connect to GCP?
Terraform can authenticate to GCP using: - Application Default Credentials (ADC) - Service Account JSON key file - Environment variables pointing to credentials This allows Terraform to securely access GCP resources.
Click to reveal answer
beginner
What is the minimum Terraform block needed to initialize the GCP provider?
A minimal provider block looks like:
provider "google" {
project = "your-project-id"
region = "your-region"
}
This tells Terraform which GCP project and region to use.
Click to reveal answer
beginner
Why is it important to run 'terraform init' after adding the GCP provider?
'terraform init' downloads the GCP provider plugin and sets up the working directory so Terraform can communicate with GCP. Without it, Terraform cannot apply or plan changes.
Click to reveal answer
Which block in Terraform config specifies the GCP provider?
Aprovider "google" {}
Bresource "google" {}
Cmodule "google" {}
Dvariable "google" {}
✗ Incorrect
The provider block with name "google" configures the GCP provider.
How does Terraform authenticate to GCP using a service account?
ABy using the 'region' attribute
BBy specifying the project ID only
CBy running 'terraform apply' without config
DBy setting the 'credentials' attribute with the path to the JSON key file
✗ Incorrect
Terraform uses the 'credentials' attribute to point to the service account JSON key file for authentication.
What command must you run to download the GCP provider plugin after adding it to your config?
Aterraform plan
Bterraform init
Cterraform apply
Dterraform validate
✗ Incorrect
'terraform init' downloads provider plugins and initializes the working directory.
Which attribute is NOT part of the GCP provider configuration?
Ainstance_type
Bregion
Ccredentials
Dproject
✗ Incorrect
'instance_type' is a resource attribute, not a provider configuration attribute.
What environment variable can Terraform use to find GCP credentials automatically?
AGCP_PROJECT_ID
BTERRAFORM_CREDENTIALS
CGOOGLE_APPLICATION_CREDENTIALS
DGOOGLE_REGION
✗ Incorrect
The 'GOOGLE_APPLICATION_CREDENTIALS' environment variable points to the JSON key file for authentication.
Explain how to set up the Terraform GCP provider from scratch.
Think about what Terraform needs to connect and manage GCP resources.
You got /3 concepts.
Describe the role of the 'terraform init' command in the GCP provider setup.
Consider what happens before you can plan or apply changes.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the provider block in a Terraform configuration for GCP?
easy
A. To store user credentials permanently
B. To define virtual machines in GCP
C. To create network firewalls automatically
D. To connect Terraform to Google Cloud services
Solution
Step 1: Understand the role of the provider block
The provider block tells Terraform which cloud service to use and how to connect to it.
Step 2: Identify the specific function for GCP
For Google Cloud, the provider block sets up the connection using project ID, region, and credentials.
Final Answer:
To connect Terraform to Google Cloud services -> Option D
Quick Check:
Provider block = Connect Terraform to cloud [OK]
Hint: Provider block always sets up cloud connection [OK]
Common Mistakes:
Confusing provider block with resource definitions
Thinking provider stores credentials permanently
Assuming provider creates resources automatically
2. Which of the following is the correct syntax to specify the GCP project ID in the Terraform provider block?
easy
A. project = "my-gcp-project"
B. project_id = "my-gcp-project"
C. gcp_project = "my-gcp-project"
D. id_project = "my-gcp-project"
Solution
Step 1: Recall the correct attribute name for project ID
In Terraform's GCP provider, the attribute to set the project is project.
Step 2: Match the correct syntax
The correct syntax is project = "my-gcp-project", which matches project = "my-gcp-project".
Final Answer:
project = "my-gcp-project" -> Option A
Quick Check:
Project ID attribute = project [OK]
Hint: Use 'project' keyword for project ID in provider [OK]
Terraform gives an error: <code>"var.project" is not defined. What is the likely cause and fix?
medium
A. The provider block must not use variables; replace with fixed strings
B. The file credentials.json is missing; add the file
C. Variables project and region are not declared; declare them in variables.tf
D. Terraform does not support variable use in provider blocks; remove variables
Solution
Step 1: Analyze the error message
The error says var.project is not defined, meaning Terraform cannot find the variable declaration.
Step 2: Understand variable usage in Terraform
Variables must be declared in a variables.tf file or equivalent before use.
Final Answer:
Variables project and region are not declared; declare them in variables.tf -> Option C
Quick Check:
Undefined variable error = declare variables [OK]
Hint: Declare variables before using them in provider [OK]
Common Mistakes:
Assuming missing credentials file causes this error
Thinking variables can't be used in provider block
Ignoring the need to declare variables explicitly
5. You want to set up the Terraform GCP provider to use different projects and regions without changing code. Which is the best practice to achieve this?
hard
A. Hardcode project and region in the provider block
B. Use variables for project and region and pass values at runtime
C. Create multiple provider blocks with fixed values for each environment
D. Store credentials inside the Terraform code directly
Solution
Step 1: Understand flexibility needs
To switch projects and regions easily, avoid hardcoding values in code.
Step 2: Use variables for dynamic configuration
Using variables allows passing different values at runtime without changing code, which is best practice.
Step 3: Avoid insecure or rigid methods
Hardcoding or multiple fixed provider blocks reduce flexibility; storing credentials in code risks security.
Final Answer:
Use variables for project and region and pass values at runtime -> Option B
Quick Check:
Variables for flexibility = best practice [OK]
Hint: Use variables to change project and region easily [OK]