2. Which of the following is the correct syntax for string interpolation in Terraform?
easy
A. 'Hello, $var.name!'
B. 'Hello, ${var.name}!'
C. "Hello, ${var.name}!"
D. "Hello, $var.name!"
Solution
Step 1: Check string quoting rules
Terraform requires double quotes for strings with interpolation; single quotes treat content literally.
Step 2: Verify interpolation syntax
The correct interpolation uses ${} inside double quotes to evaluate expressions.
Final Answer:
"Hello, ${var.name}!" -> Option C
Quick Check:
Double quotes + ${} = correct syntax [OK]
Hint: Use double quotes with ${} for interpolation [OK]
Common Mistakes:
Using single quotes which disable interpolation
Missing curly braces around variable
Using $ without braces
3. Given the Terraform code:
variable "env" { default = "prod" }
output "message" {
value = "Deploying to ${var.env} environment"
}
What will be the output value?
medium
A. Deploying to prod environment
B. Deploying to ${var.env} environment
C. Deploying to env environment
D. Syntax error
Solution
Step 1: Identify variable value
The variable env has default value "prod".
Step 2: Evaluate interpolation in output
The string uses ${var.env} which inserts "prod" into the string.
Final Answer:
Deploying to prod environment -> Option A
Quick Check:
Interpolation replaces var.env with prod [OK]
Hint: Interpolation replaces variables with their values [OK]
Common Mistakes:
Thinking interpolation shows literal text
Confusing variable name with value
Assuming syntax error due to quotes
4. What is wrong with this Terraform string interpolation?
output "example" {
value = "Path is ${path}"
}
Assuming path is not defined anywhere.
medium
A. Interpolation ignored, outputs empty string
B. Syntax error due to missing variable declaration
C. No error, outputs literal string
D. Runtime error: undefined variable 'path'
Solution
Step 1: Check variable usage
The interpolation uses ${path} but path is not declared as a variable or local.
Step 2: Understand Terraform behavior
Terraform throws a runtime error when a referenced variable is undefined.
Final Answer:
Runtime error: undefined variable 'path' -> Option D
Quick Check:
Undefined variable causes runtime error [OK]
Hint: Undefined variables cause runtime errors in interpolation [OK]
Common Mistakes:
Assuming no error and literal output
Thinking it's a syntax error
Expecting empty string output
5. You want to create a resource name that combines environment and region variables in Terraform. Which interpolation correctly creates a name like app-prod-uswest?