2024-11-15
4 min read

How do you do simple string concatenation in Terraform?

How do you do simple string concatenation in Terraform?

String concatenation is a common task in Terraform, often used to dynamically construct resource names, tags, or other configuration values. Terraform provides several ways to concatenate strings, making it easy to handle various use cases.

Using Interpolation

The simplest way to concatenate strings in Terraform is by using interpolation. Interpolation allows you to embed variables and expressions directly into strings.

Example

variable "environment" {
  default = "dev"
}

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

Explanation

  • ${var.environment}: Embeds the value of the environment variable into the string.
  • The resulting value will be my-app-dev-bucket if the environment variable is set to dev.

Using the join Function

The join function is another way to concatenate strings, especially when working with lists.

Example

variable "tags" {
  default = ["Environment:Dev", "Team:Engineering"]
}

output "tags_string" {
  value = join(", ", var.tags)
}

Explanation

  • join(", ", var.tags): Joins the elements of the tags list into a single string, separated by a comma and a space.
  • The resulting value will be Environment:Dev, Team:Engineering.

Best Practices

  • Use interpolation for simple concatenation tasks.
  • Use the join function for lists to improve readability and maintainability.
  • Avoid hardcoding values; use variables to make your configuration more flexible.

By understanding these techniques, you can efficiently handle string concatenation in Terraform and create dynamic, reusable configurations.

Published: 2024-11-15|Last updated: 2024-11-15T09:00:00Z

Found an issue?