Bird
Raised Fist0
Terraformcloud~20 mins

String interpolation 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
🎖️
Terraform String Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Terraform String Interpolation Syntax
What will be the output of the following Terraform expression?

"Hello, ${var.name}! Welcome to ${var.region}."

Assume var.name = "Alice" and var.region = "us-west-2".
A"Hello, Alice! Welcome to us-west-2."
B"Hello, ${var.name}! Welcome to ${var.region}."
C"Hello, Alice! Welcome to ${var.region}."
D"Hello, var.name! Welcome to var.region."
Attempts:
2 left
💡 Hint
Terraform replaces variables inside ${} with their values.
❓ Configuration
intermediate
2:00remaining
Correct Use of String Interpolation in Resource Names
Which option correctly uses string interpolation to create an AWS S3 bucket name with a prefix and environment variable?

Assume var.env = "prod" and var.prefix = "myapp".
Abucket = "${var.prefix-var.env}-bucket"
Bbucket = "${var.prefix}-${var.env}-bucket"
Cbucket = "var.prefix-var.env-bucket"
D"tekcub-}vne.rav{$-}xiferp.rav{$" = tekcub
Attempts:
2 left
💡 Hint
Use ${} around each variable and separate with hyphens outside the braces.
❓ Architecture
advanced
2:00remaining
Combining String Interpolation with Conditional Expressions
Given the Terraform variable var.is_production which is a boolean, which option correctly sets the resource tag Environment to "prod" if true, or "dev" if false, using string interpolation?
Atags = { Environment = "${var.is_production ? "prod" : dev}" }
Btags = { Environment = "${if var.is_production then "prod" else "dev"}" }
Ctags = { Environment = "${var.is_production ? prod : dev}" }
Dtags = { Environment = "${var.is_production ? "prod" : "dev"}" }
Attempts:
2 left
💡 Hint
Terraform uses the ternary operator inside ${} with quotes around string results.
❓ security
advanced
2:00remaining
Avoiding Sensitive Data Exposure in String Interpolation
Which option correctly avoids exposing a sensitive variable var.db_password in Terraform output using string interpolation?
Aoutput "db_password" { value = var.db_password }
Boutput "db_password" { value = var.db_password, sensitive = true }
Coutput "db_password" { value = "${var.db_password}" sensitive = true }
Doutput "db_password" { value = "${var.db_password}" }
Attempts:
2 left
💡 Hint
Use the sensitive attribute to hide output values.
❓ service_behavior
expert
2:00remaining
Effect of Incorrect String Interpolation on AWS Resource Creation
What will happen if you define an AWS EC2 instance resource with the following name attribute?

name = "${var.instance_name"

Assuming var.instance_name = "webserver".
ATerraform plan will fail with a syntax error due to missing closing brace.
BTerraform will create the instance with name literally '${var.instance_name'.
CTerraform will create the instance with name 'webserver'.
DTerraform plan will succeed but instance name will be empty.
Attempts:
2 left
💡 Hint
Check for matching braces in interpolation expressions.

Practice

(1/5)
1. What does string interpolation in Terraform do?
${} is used to:
easy
A. Create new variables automatically
B. Insert variable or expression values inside strings
C. Comment out lines in configuration
D. Encrypt sensitive data in strings

Solution

  1. Step 1: Understand string interpolation syntax

    Terraform uses ${} inside double quotes to insert values dynamically into strings.
  2. Step 2: Identify the purpose of interpolation

    This allows combining text with variable or expression results to build flexible strings.
  3. Final Answer:

    Insert variable or expression values inside strings -> Option B
  4. Quick Check:

    String interpolation = Insert values [OK]
Hint: Remember ${} inserts values inside strings [OK]
Common Mistakes:
  • Thinking it creates variables
  • Confusing with comments
  • Assuming it encrypts data
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

  1. Step 1: Check string quoting rules

    Terraform requires double quotes for strings with interpolation; single quotes treat content literally.
  2. Step 2: Verify interpolation syntax

    The correct interpolation uses ${} inside double quotes to evaluate expressions.
  3. Final Answer:

    "Hello, ${var.name}!" -> Option C
  4. 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

  1. Step 1: Identify variable value

    The variable env has default value "prod".
  2. Step 2: Evaluate interpolation in output

    The string uses ${var.env} which inserts "prod" into the string.
  3. Final Answer:

    Deploying to prod environment -> Option A
  4. 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

  1. Step 1: Check variable usage

    The interpolation uses ${path} but path is not declared as a variable or local.
  2. Step 2: Understand Terraform behavior

    Terraform throws a runtime error when a referenced variable is undefined.
  3. Final Answer:

    Runtime error: undefined variable 'path' -> Option D
  4. 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?
variable "env" { default = "prod" }
variable "region" { default = "uswest" }
hard
A. "app-${var.env}-${var.region}"
B. "app-${env}-${region}"
C. "app-${var.env + var.region}"
D. "app-${var.env}_${var.region}"

Solution

  1. Step 1: Use correct variable references

    Variables must be referenced as var.env and var.region inside interpolation.
  2. Step 2: Combine with dashes correctly

    Use string interpolation with dashes between variables: "app-${var.env}-${var.region}".
  3. Final Answer:

    "app-${var.env}-${var.region}" -> Option A
  4. Quick Check:

    Correct variable syntax + dashes = "app-${var.env}-${var.region}" [OK]
Hint: Use var. prefix and separate with dashes inside ${} [OK]
Common Mistakes:
  • Omitting var. prefix
  • Concatenating without separator
  • Using underscore instead of dash