Challenge - 5 Problems
Terraform Conditional Expressions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate2:00remaining
Terraform Conditional Expression Output
What will be the value of
instance_type after applying this Terraform configuration snippet?Terraform
variable "environment" { default = "production" } locals { instance_type = var.environment == "production" ? "t3.large" : "t3.micro" }
Attempts:
2 left
💡 Hint
Check the condition comparing the environment variable.
✗ Incorrect
The condition checks if environment equals "production". Since it does, the instance_type is set to "t3.large".
❓ Configuration
intermediate2:00remaining
Terraform Conditional Expression in Resource Count
Given the following Terraform resource snippet, how many instances of
aws_instance will be created if var.deploy is false?Terraform
variable "deploy" { default = false } resource "aws_instance" "example" { count = var.deploy ? 3 : 0 ami = "ami-123456" instance_type = "t2.micro" }
Attempts:
2 left
💡 Hint
Look at the count expression and the value of var.deploy.
✗ Incorrect
Since var.deploy is false, the count expression evaluates to 0, so no instances are created.
❓ Architecture
advanced2:00remaining
Choosing Subnet Based on Environment Using Conditional Expression
In a Terraform module, you want to select a subnet ID based on the environment variable
var.env. Which option correctly assigns subnet_id to "subnet-prod123" if var.env is "prod", and to "subnet-dev123" otherwise?Attempts:
2 left
💡 Hint
Check the equality operator and the order of values in the ternary expression.
✗ Incorrect
Option C correctly uses the equality operator == and assigns the prod subnet when var.env is "prod".
❓ security
advanced2:00remaining
Conditional Expression for Enabling Encryption
You want to enable encryption on an AWS S3 bucket only if the variable
var.enable_encryption is true. Which Terraform snippet correctly sets the server_side_encryption_configuration block conditionally?Attempts:
2 left
💡 Hint
Encryption block should be present only when enabled.
✗ Incorrect
Option B correctly assigns the encryption configuration when var.enable_encryption is true, otherwise an empty list disables it.
✅ Best Practice
expert2:00remaining
Complex Conditional Expression for Instance Count
Consider this Terraform variable and resource snippet. What is the value of
count for aws_instance.example if var.env is "staging" and var.scale_up is true?Terraform
variable "env" { default = "staging" } variable "scale_up" { default = true } resource "aws_instance" "example" { count = var.env == "production" ? 5 : (var.scale_up ? 3 : 1) ami = "ami-abc123" instance_type = "t3.medium" }
Attempts:
2 left
💡 Hint
Evaluate the nested conditional expression step by step.
✗ Incorrect
Since var.env is not "production", the count depends on var.scale_up. Because var.scale_up is true, count is 3.