0
0
Terraformcloud~5 mins

String interpolation in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
String interpolation in Terraform lets you combine text and variables to create dynamic strings. This helps you build resource names, messages, or configurations that change based on your inputs.
When you want to create a resource name that includes a variable like environment or region.
When you need to build a URL or path that depends on user input or other resource attributes.
When you want to output a message that shows values of variables or resource properties.
When you need to combine multiple variables and text into one string for configuration.
When you want to pass dynamic values to modules or other Terraform resources.
Config File - main.tf
main.tf
variable "environment" {
  type    = string
  default = "dev"
}

variable "region" {
  type    = string
  default = "us-east-1"
}

output "bucket_name" {
  value = "my-app-${var.environment}-${var.region}"
}

This Terraform file defines two variables: environment and region. The output bucket_name uses string interpolation to combine fixed text with these variables, creating a dynamic string like my-app-dev-us-east-1.

Commands
Initializes the Terraform working directory and downloads required providers.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
Applies the Terraform configuration, creating outputs and resources without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: # output.bucket_name will be created Plan: 0 to add, 0 to change, 0 to destroy. Changes to Outputs: + bucket_name = "my-app-dev-us-east-1" Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: bucket_name = "my-app-dev-us-east-1"
-auto-approve - Skip interactive approval before applying changes
Displays the value of the output named bucket_name to verify the string interpolation result.
Terminal
terraform output bucket_name
Expected OutputExpected
my-app-dev-us-east-1
Key Concept

If you remember nothing else from this pattern, remember: use ${var.name} inside strings to insert variable values dynamically.

Common Mistakes
Using quotes incorrectly like "my-app-${var.environment}" without proper syntax
Terraform requires double quotes around the whole string and ${} for variables; missing these causes errors.
Always wrap the entire string in double quotes and use ${var.variable_name} to insert variables.
Trying to concatenate strings with + operator like "my-app-" + var.environment
Terraform does not support + for string concatenation; it uses interpolation inside strings instead.
Use string interpolation with ${} inside double quotes to combine strings and variables.
Summary
Define variables to hold dynamic values like environment and region.
Use string interpolation with ${var.name} inside double quotes to build dynamic strings.
Run terraform apply to see the output with combined strings and variables.