📖 Scenario: You are setting up a Terraform configuration to manage cloud resources. You need to work with strings to create resource names and parse input values.
🎯 Goal: Build a Terraform configuration that uses join, split, and format string functions to manipulate resource names and tags.
📋 What You'll Learn
Create a list of strings representing environment names
Create a variable to hold a delimiter string
Use join to combine environment names into a single string
Use split to break the combined string back into a list
Use format to create a formatted resource name string
💡 Why This Matters
🌍 Real World
Cloud engineers often need to manipulate strings to create resource names, tags, and configuration values dynamically in Terraform.
💼 Career
Understanding string functions in Terraform helps you write flexible and reusable infrastructure code, a key skill for cloud infrastructure roles.
Progress0 / 4 steps
1
Create a list of environment names
Create a Terraform variable called environments with the exact list of strings: ["dev", "test", "prod"].
Terraform
Hint
Use the variable block and set default to the list.
2
Create a delimiter variable
Create a Terraform variable called delimiter and set its default value to the string "-".
Terraform
Hint
Use a variable block with default = "-".
3
Join environment names into a single string
Create a Terraform local variable called env_string that uses the join function with var.delimiter and var.environments to combine the list into a single string.
Terraform
Hint
Use locals block and join function.
4
Split the string and format a resource name
Add to the locals block a variable called env_list that uses split with var.delimiter and local.env_string. Then add a variable called resource_name that uses format to create the string "resource-%s-%s" with the first two elements of local.env_list.
Terraform
Hint
Use split and format inside the locals block.
Practice
(1/5)
1. What does the Terraform function join(",", ["a", "b", "c"]) return?
easy
A. "a,b,c"
B. ["a", "b", "c"]
C. "a b c"
D. "abc"
Solution
Step 1: Understand the join function
The join function combines list elements into a single string using the given separator.
Step 2: Apply join with separator ','
Joining ["a", "b", "c"] with "," results in the string "a,b,c".
Final Answer:
"a,b,c" -> Option A
Quick Check:
join with comma = "a,b,c" [OK]
Hint: Join combines list items with separator into one string [OK]
Common Mistakes:
Confusing join output with original list
Using space instead of comma as separator
Expecting a list instead of a string
2. Which of the following is the correct syntax to split the string "one,two,three" into a list using Terraform?
easy
A. format(",", "one,two,three")
B. split("one,two,three", ",")
C. split(",", "one,two,three")
D. join(",", "one,two,three")
Solution
Step 1: Recall split function syntax
The split function takes the separator first, then the string to split.
Step 2: Match correct argument order
Only split(",", "one,two,three") matches the correct syntax.
Hint: split(separator, string) order matters in Terraform [OK]
Common Mistakes:
Swapping arguments order in split
Using join instead of split
Using format instead of split
3. What is the output of this Terraform expression?
format("Hello %s, you have %d messages", "Alice", 5)
medium
A. "Hello Alice, you have 5 messages"
B. "Hello Alice, you have %d messages"
C. "Hello %s, you have %d messages"
D. "Hello %s, you have 5 messages"
Solution
Step 1: Understand format placeholders
%s is replaced by string, %d by integer in order.
Step 2: Replace placeholders with arguments
"Alice" replaces %s, 5 replaces %d, producing "Hello Alice, you have 5 messages".
Final Answer:
"Hello Alice, you have 5 messages" -> Option A
Quick Check:
format with %s and %d = formatted string [OK]
Hint: format replaces %s and %d placeholders in order [OK]
Common Mistakes:
Not replacing placeholders
Mixing up %s and %d types
Expecting original string without substitution
4. Identify the error in this Terraform code snippet:
locals {
parts = split(",", "apple,banana,orange")
result = join(";", parts)
message = format("Fruits: %s", result)
}
medium
A. join separator should be a list, not a string
B. split function arguments are reversed
C. format placeholder %s cannot accept a string
D. No error, code is correct
Solution
Step 1: Check split function usage
split separator and string are correctly ordered.
Step 2: Check join and format usage
join uses string separator ";" correctly; format uses %s for string correctly.
Final Answer:
No error, code is correct -> Option D
Quick Check:
split, join, format used correctly = No error, code is correct [OK]
Hint: Check argument order and placeholder types carefully [OK]
Common Mistakes:
Swapping split arguments
Using wrong placeholder in format
Passing wrong types to join
5. You have a list of IP addresses: ["10.0.0.1", "10.0.0.2", "10.0.0.3"]. You want to create a single string with these IPs separated by commas, but enclosed in square brackets like this: [10.0.0.1,10.0.0.2,10.0.0.3] Which Terraform expression achieves this?
hard
A. join(",", format("[%s]", ["10.0.0.1", "10.0.0.2", "10.0.0.3"]))
B. format("[%s]", join(",", ["10.0.0.1", "10.0.0.2", "10.0.0.3"]))
C. split(",", format("[%s]", join(",", ["10.0.0.1", "10.0.0.2", "10.0.0.3"])))
D. format(join(",", ["10.0.0.1", "10.0.0.2", "10.0.0.3"]), "[%s]")
Solution
Step 1: Join IP list with commas
Using join(",", [...]) creates "10.0.0.1,10.0.0.2,10.0.0.3".
Step 2: Use format to add square brackets
Wrapping the joined string with format("[%s]", ...) adds brackets around it.
Final Answer:
format("[%s]", join(",", ["10.0.0.1", "10.0.0.2", "10.0.0.3"])) -> Option B
Quick Check:
join then format = format("[%s]", join(",", ["10.0.0.1", "10.0.0.2", "10.0.0.3"])) [OK]
Hint: Join first, then format to add brackets [OK]
Common Mistakes:
Trying to format before join
Using split instead of join
Incorrect argument order in format
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.