0
0
Terraformcloud~10 mins

Conditional expressions (ternary) in Terraform - Interactive Code Practice

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

Complete the code to assign the value based on the condition.

Terraform
variable "environment" {
  default = "production"
}

output "instance_type" {
  value = var.environment == "production" ? [1] : "t2.micro"
}
Drag options to blanks, or click blank then click option'
A"t3.large"
B"t2.large"
C"t3.nano"
D"t2.micro"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the false value in the true condition position.
Forgetting to quote the instance type strings.
2fill in blank
medium

Complete the code to set the bucket versioning based on the environment.

Terraform
resource "aws_s3_bucket" "example" {
  bucket = "my-app-bucket"
  versioning {
    enabled = var.environment == "production" ? [1] : false
  }
}
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
C"true"
D"false"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings "true" or "false" instead of booleans.
Setting the wrong boolean for the production environment.
3fill in blank
hard

Fix the error in the conditional expression for setting the instance count.

Terraform
resource "aws_instance" "web" {
  count = var.environment == "production" ? [1] : 1
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
Drag options to blanks, or click blank then click option'
A3
B"3"
C"1"
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "3" instead of number 3.
Using the wrong number for production count.
4fill in blank
hard

Fill both blanks to set the environment tag based on the environment variable.

Terraform
resource "aws_instance" "app" {
  ami           = "ami-654321"
  instance_type = "t2.medium"

  tags = {
    Environment = var.environment [1] "production" ? [2] : "dev"
  }
}
Drag options to blanks, or click blank then click option'
A==
B!=
C"production"
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of ==.
Using the wrong string value for the true condition.
5fill in blank
hard

Fill all three blanks to create a conditional expression that sets the instance size and count.

Terraform
resource "aws_instance" "db" {
  instance_type = var.environment [1] "production" ? [2] : "t2.small"
  count         = var.environment [3] "production" ? 2 : 1
  ami           = "ami-789012"
}
Drag options to blanks, or click blank then click option'
A==
B"m5.large"
C!=
D"t2.medium"
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == for conditions.
Mixing instance types or counts incorrectly.