Eduardo Gimenez
Eduardo Gimenez

Reputation: 85

Concatenate variables in terraform V0.12

I'm trying to update my terraform files from V0.11 to V0.12 and I have some questions.

What's the best way to concatenate variables with strings?

In V0.11 I use this: ${var.name}-STRING-${var.name2}, in V0.12 can I use this: var.name"-STRING-"var.name2 or must I use another ways to concatenate variables and strings?

Upvotes: 4

Views: 7143

Answers (4)

Marcin Piechota
Marcin Piechota

Reputation: 71

List of available functions is available at https://www.terraform.io/docs/configuration/functions/join.html

For Python programmers I would suggest using join as another readable alternative:

join("-", [var.name, "STRING", var.name2])

Upvotes: 2

mjahr
mjahr

Reputation: 159

Former C Programmers might like this function better:

format("%s-STRING-%s", var.name, var.name2)

For me, it's less clunky than the old $-Syntax

Upvotes: 3

pabloxio
pabloxio

Reputation: 1493

In v0.12 for interpolations like this:

"${var.example}"

You should use now:

var.example

In your example, In v0.12 you should keep using the previous syntax from v0.11:

"${var.name}-STRING-${var.name2}"

There is a great section in Terraform documentation about migrating to v0.12

Upvotes: 4

Ashish
Ashish

Reputation: 69

To concatenate, check few example below:

If you want to add '@' to string:

value = "${var.username}@${aws_instance.my-instance.public_dns}"

Output: [email protected]

To create link:

value = "http://${aws_instance.my-instance.public_dns}:90"

Output: http://ec2-184-72-11-141.us-west-1.compute.amazonaws.com:90

Upvotes: 3

Related Questions