Bird
Raised Fist0
GCPcloud~10 mins

Terraform GCP provider setup - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the Terraform provider for Google Cloud Platform.

GCP
provider "google" {
  project = "my-project-id"
  region  = "us-central1"
  [1] = "4.0"
}
Drag options to blanks, or click blank then click option'
Aapi_version
Bprovider_version
Cgcp_version
Dversion
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'provider_version' instead of 'version'.
Using 'gcp_version' or 'api_version' which are invalid.
2fill in blank
medium

Complete the code to set the credentials file path for the GCP provider.

GCP
provider "google" {
  credentials = file("[1]")
  project     = "my-project-id"
  region      = "us-central1"
}
Drag options to blanks, or click blank then click option'
A/path/to/keyfile.txt
B/path/to/credentials.json
C/path/to/config.yaml
D/path/to/secret.key
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-JSON file for credentials.
Using a path with wrong file extension.
3fill in blank
hard

Fix the error in the provider block by completing the missing attribute for specifying the region.

GCP
provider "google" {
  project     = "my-project-id"
  [1] = "us-west1"
  credentials = file("/path/to/credentials.json")
}
Drag options to blanks, or click blank then click option'
Alocation
Bzone
Cregion
Darea
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'zone' instead of 'region' in the provider block.
Using 'location' or 'area' which are invalid attributes.
4fill in blank
hard

Fill both blanks to configure the provider with project and region variables.

GCP
provider "google" {
  project = var.[1]
  region  = var.[2]
  credentials = file("/path/to/credentials.json")
}
Drag options to blanks, or click blank then click option'
Aproject_id
Bregion_name
Cregion
Dproject
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'project_id' instead of 'project' variable.
Using 'region_name' instead of 'region' variable.
5fill in blank
hard

Fill all three blanks to define variables for project, region, and credentials file path.

GCP
variable "[1]" {
  type        = string
  description = "The GCP project ID"
}

variable "[2]" {
  type        = string
  description = "The GCP region"
}

variable "[3]" {
  type        = string
  description = "Path to the GCP credentials JSON file"
}
Drag options to blanks, or click blank then click option'
Aproject
Bregion
Ccredentials_path
Dcredentials_file
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'credentials_file' instead of 'credentials_path'.
Using inconsistent variable names that don't match usage.

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

  1. 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.
  2. Step 2: Identify the specific function for GCP

    For Google Cloud, the provider block sets up the connection using project ID, region, and credentials.
  3. Final Answer:

    To connect Terraform to Google Cloud services -> Option D
  4. 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

  1. Step 1: Recall the correct attribute name for project ID

    In Terraform's GCP provider, the attribute to set the project is project.
  2. Step 2: Match the correct syntax

    The correct syntax is project = "my-gcp-project", which matches project = "my-gcp-project".
  3. Final Answer:

    project = "my-gcp-project" -> Option A
  4. Quick Check:

    Project ID attribute = project [OK]
Hint: Use 'project' keyword for project ID in provider [OK]
Common Mistakes:
  • Using project_id instead of project
  • Mixing attribute names like gcp_project
  • Misspelling the attribute name
3. Given this Terraform provider block snippet:
provider "google" {
  project = var.project_id
  region  = "us-central1"
  credentials = file("account.json")
}

What will happen if the file account.json is missing or unreadable?
medium
A. Terraform will throw an error and stop
B. Terraform will apply changes without authentication
C. Terraform will use default credentials automatically
D. Terraform will ignore the credentials and proceed

Solution

  1. Step 1: Understand the role of credentials in provider block

    The credentials file provides authentication details needed to access GCP.
  2. Step 2: Identify behavior when credentials file is missing

    If the file is missing or unreadable, Terraform cannot authenticate and will throw an error, stopping the process.
  3. Final Answer:

    Terraform will throw an error and stop -> Option A
  4. Quick Check:

    Missing credentials file = error stop [OK]
Hint: Missing credentials file causes error [OK]
Common Mistakes:
  • Assuming Terraform skips authentication
  • Thinking default credentials always apply
  • Believing Terraform proceeds without credentials
4. You wrote this provider block:
provider "google" {
  project = var.project
  region = var.region
  credentials = file("credentials.json")
}

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

  1. Step 1: Analyze the error message

    The error says var.project is not defined, meaning Terraform cannot find the variable declaration.
  2. Step 2: Understand variable usage in Terraform

    Variables must be declared in a variables.tf file or equivalent before use.
  3. Final Answer:

    Variables project and region are not declared; declare them in variables.tf -> Option C
  4. 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

  1. Step 1: Understand flexibility needs

    To switch projects and regions easily, avoid hardcoding values in code.
  2. Step 2: Use variables for dynamic configuration

    Using variables allows passing different values at runtime without changing code, which is best practice.
  3. Step 3: Avoid insecure or rigid methods

    Hardcoding or multiple fixed provider blocks reduce flexibility; storing credentials in code risks security.
  4. Final Answer:

    Use variables for project and region and pass values at runtime -> Option B
  5. Quick Check:

    Variables for flexibility = best practice [OK]
Hint: Use variables to change project and region easily [OK]
Common Mistakes:
  • Hardcoding values reduces flexibility
  • Duplicating provider blocks causes maintenance issues
  • Embedding credentials in code risks security
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard