0
0
Terraformcloud~30 mins

String functions (join, split, format) in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Using String Functions in Terraform
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use split and format inside the locals block.