Challenge - 5 Problems
Null Values Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate2: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" }
Attempts:
2 left
💡 Hint
Check how the ternary operator handles null values in Terraform.
✗ Incorrect
The local variable
resource_name uses a conditional expression to check if var.name is not null. If it is null, it assigns "default-name". Since var.name is null, the result is "default-name".❓ Configuration
intermediate2: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) }
Attempts:
2 left
💡 Hint
The lookup function returns the default value if the key is missing.
✗ Incorrect
The
lookup function returns the value for the key if it exists. If not, it returns the default value provided, which is null here. Since the map is empty, the output is null.❓ Architecture
advanced2: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" }
Attempts:
2 left
💡 Hint
Look at how the count argument handles null values with a conditional expression.
✗ Incorrect
The count argument uses a conditional expression to assign 1 if
var.instance_count is null. Since it is null, count becomes 1, so one instance is created.❓ security
advanced2: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 }
Attempts:
2 left
💡 Hint
Consider how Terraform treats null values for required resource attributes.
✗ Incorrect
Terraform does not automatically convert null to empty string or default values for required attributes. Passing null to a required string attribute causes an error.
✅ Best Practice
expert3: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 = ???
}Attempts:
2 left
💡 Hint
Terraform has a built-in function to handle null and fallback values.
✗ Incorrect
The
coalesce function returns the first non-null argument. It is the recommended way to handle optional null variables with defaults in Terraform.