0
0
Terraformcloud~20 mins

Default values in Terraform - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Values Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Configuration
intermediate
1:30remaining
What is the default value of the variable 'instance_type'?

Given this Terraform variable declaration, what is the default value of instance_type?

variable "instance_type" {
  type    = string
  default = "t2.micro"
}
A""
B"t2.micro"
Cnull
D"t3.medium"
Attempts:
2 left
💡 Hint

Look at the default attribute inside the variable block.

service_behavior
intermediate
1:30remaining
What happens if a variable with no default is not provided?

Consider this Terraform variable declaration without a default value:

variable "region" {
  type = string
}

What happens if you run terraform apply without providing a value for region?

ATerraform prompts the user to input a value for 'region'.
BTerraform throws a syntax error and stops.
CTerraform automatically assigns the provider's default region.
DTerraform uses an empty string as the default value.
Attempts:
2 left
💡 Hint

Think about how Terraform handles required variables without defaults.

Architecture
advanced
2:00remaining
How does default value affect module reuse?

You have a Terraform module with a variable environment that has a default value "dev". You want to reuse this module for production without changing the module code.

Which approach correctly overrides the default value when calling the module?

Terraform
<pre>module "app" {
  source = "./app_module"
  # How to override environment?
}</pre>
ARename the variable in the module to "prod".
BChange the default value inside the module variable to "prod".
CSet an environment variable named TF_VAR_environment to "prod" before running Terraform.
DAdd <code>environment = "prod"</code> inside the module block.
Attempts:
2 left
💡 Hint

Think about how to pass values to modules to override defaults.

security
advanced
2:00remaining
What risk arises from default sensitive variable values?

A Terraform variable is declared as sensitive with a default value containing a password:

variable "db_password" {
  type      = string
  sensitive = true
  default   = "defaultPass123"
}

What is the main security risk of this setup?

AThe password will be encrypted automatically, so no risk exists.
BTerraform will ignore the sensitive flag and print the password always.
CThe default password may be exposed if the code is shared publicly.
DTerraform will fail to apply because sensitive variables cannot have defaults.
Attempts:
2 left
💡 Hint

Consider what happens if code with default secrets is stored in public repositories.

🧠 Conceptual
expert
2:00remaining
What is the output of this Terraform expression with default values?

Given the following Terraform locals and variables:

variable "count" {
  type    = number
  default = 3
}

locals {
  total = var.count + 2
}

What is the value of local.total if no value is provided for count?

A5
B3
C2
Dnull
Attempts:
2 left
💡 Hint

Remember how default values are used when no input is given.