Bird
Raised Fist0
Terraformcloud~20 mins

Null values handling in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Null Values Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Handling null values in Terraform variables
Given the following Terraform variable declaration and usage, what will be the value of resource_name after applying the configuration if var.name is null?
Terraform
variable "name" {
  type    = string
  default = null
}

locals {
  resource_name = var.name != null ? var.name : "default-name"
}
Anull
B"default-name"
C""
DError: variable "name" must not be null
Attempts:
2 left
💡 Hint
Check how the ternary operator handles null values in Terraform.
Configuration
intermediate
2:00remaining
Null value in Terraform map lookup
What will be the output of the following Terraform expression if var.settings is {} (an empty map)?
Terraform
variable "settings" {
  type = map(string)
  default = {}
}

output "region" {
  value = lookup(var.settings, "region", null)
}
Anull
B"region"
CError: key "region" not found
D""
Attempts:
2 left
💡 Hint
The lookup function returns the default value if the key is missing.
Architecture
advanced
2:00remaining
Impact of null values on resource count in Terraform
Consider the following Terraform snippet. What will be the number of aws_instance resources created if var.instance_count is null?
Terraform
variable "instance_count" {
  type    = number
  default = null
}

resource "aws_instance" "example" {
  count = var.instance_count != null ? var.instance_count : 1
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
A1
B0
Cnull
DError: count must be a non-negative integer
Attempts:
2 left
💡 Hint
Look at how the count argument handles null values with a conditional expression.
security
advanced
2:00remaining
Null values in sensitive Terraform variables
If a sensitive Terraform variable is set to null, what is the behavior when it is used in a resource attribute expecting a string?
Terraform
variable "db_password" {
  type      = string
  sensitive = true
  default   = null
}

resource "aws_db_instance" "default" {
  password = var.db_password
  # other required attributes
}
ATerraform ignores the password attribute
BTerraform applies with an empty string as password
CTerraform replaces null with a default password automatically
DTerraform throws an error due to null value in a required attribute
Attempts:
2 left
💡 Hint
Consider how Terraform treats null values for required resource attributes.
Best Practice
expert
3:00remaining
Best practice for handling optional null values in Terraform modules
You are designing a Terraform module with an optional variable that can be null. Which approach below correctly handles the null value to provide a default inside the module without causing errors?
Terraform
variable "environment" {
  type    = string
  default = null
}

locals {
  env = ???
}
Aenv = var.environment != null ? var.environment : "production"
Benv = var.environment || "production"
Cenv = coalesce(var.environment, "production")
Denv = var.environment ?? "production"
Attempts:
2 left
💡 Hint
Terraform has a built-in function to handle null and fallback values.

Practice

(1/5)
1. What does a null value represent in Terraform configuration?
easy
A. A zero number
B. An empty string
C. An absence of a value or an intentional skip
D. A syntax error

Solution

  1. Step 1: Understand the meaning of null in Terraform

    In Terraform, null means no value is set or the value is intentionally skipped.
  2. Step 2: Differentiate null from other values

    Zero and empty string are actual values, while null means absence of any value.
  3. Final Answer:

    An absence of a value or an intentional skip -> Option C
  4. Quick Check:

    Null means no value set = B [OK]
Hint: Null means no value, not zero or empty string [OK]
Common Mistakes:
  • Confusing null with zero
  • Thinking null is an empty string
  • Assuming null causes syntax error
2. Which of the following is the correct way to assign a null value to a variable in Terraform?
easy
A. variable "example" { default = null }
B. variable "example" { default = "null" }
C. variable "example" { default = '' }
D. variable "example" { default = 0 }

Solution

  1. Step 1: Review Terraform syntax for null assignment

    In Terraform, null is a keyword without quotes to represent no value.
  2. Step 2: Identify incorrect options

    variable "example" { default = "null" } uses quotes making it a string, C is empty string, D is zero number.
  3. Final Answer:

    variable "example" { default = null } -> Option A
  4. Quick Check:

    Null keyword without quotes = A [OK]
Hint: Use null without quotes to assign null value [OK]
Common Mistakes:
  • Using "null" as a string instead of null keyword
  • Confusing empty string with null
  • Assigning zero instead of null
3. Given this Terraform expression:
var.input != null ? var.input : "default_value"

What will be the result if var.input is null?
medium
A. null
B. "default_value"
C. var.input
D. Error: invalid expression

Solution

  1. Step 1: Understand the conditional expression

    The expression checks if var.input is not null; if true, returns var.input, else returns "default_value".
  2. Step 2: Apply the condition when var.input is null

    Since var.input is null, the condition is false, so the expression returns "default_value".
  3. Final Answer:

    "default_value" -> Option B
  4. Quick Check:

    Null input returns default = A [OK]
Hint: If input is null, conditional returns default value [OK]
Common Mistakes:
  • Assuming null is returned instead of default
  • Thinking expression causes error
  • Confusing var.input with string "var.input"
4. Identify the error in this Terraform snippet handling null values:
output "example" {
  value = var.optional_value != null ? var.optional_value : null
}
medium
A. The output block must not use variables
B. The conditional operator syntax is incorrect
C. Using null as fallback causes output to be invalid
D. No error; this is valid Terraform code

Solution

  1. Step 1: Analyze the conditional expression

    The expression returns var.optional_value if not null, else returns null.
  2. Step 2: Understand output block behavior with null

    Terraform outputs can have null values; it is valid and will display as null when using terraform output.
  3. Final Answer:

    No error; this is valid Terraform code -> Option D
  4. Quick Check:

    Outputs can accept null = D [OK]
Hint: Outputs can safely return null values [OK]
Common Mistakes:
  • Believing outputs cannot accept null values
  • Misreading the conditional syntax as incorrect
  • Thinking variables cannot be used in output blocks
5. You want to create a resource only if a variable enable_feature is not null and true. Which Terraform expression correctly handles null values to achieve this?
hard
A. count = var.enable_feature != null && var.enable_feature == true ? 1 : 0
B. count = var.enable_feature != false ? 1 : 0
C. count = var.enable_feature ? 1 : 0
D. count = var.enable_feature != null ? 1 : 0

Solution

  1. Step 1: Understand the requirement

    The resource should be created only if enable_feature is not null and true.
  2. Step 2: Evaluate each option

    count = var.enable_feature != null ? 1 : 0: count = var.enable_feature != null ? 1 : 0 creates the resource if not null, regardless of true or false.
    count = var.enable_feature != false ? 1 : 0: count = var.enable_feature != false ? 1 : 0 creates for true and null (null != false).
    count = var.enable_feature ? 1 : 0: count = var.enable_feature ? 1 : 0 errors if null (invalid boolean condition).
    count = var.enable_feature != null && var.enable_feature == true ? 1 : 0: count = var.enable_feature != null && var.enable_feature == true ? 1 : 0 checks both conditions explicitly.
  3. Final Answer:

    count = var.enable_feature != null && var.enable_feature == true ? 1 : 0 -> Option A
  4. Quick Check:

    Check null and true explicitly = D [OK]
Hint: Check both not null and true explicitly for safe resource creation [OK]
Common Mistakes:
  • Ignoring null check causing errors
  • Assuming null is false automatically
  • Using only one condition without null check