How Do You Do Simple String Concatenation in Terraform?

I must be being incredibly stupid but I can't figure out how to do simple string concatenation in Terraform.

I have the following data null_data_source:

data "null_data_source" "api_gw_url" {
    inputs = {
      main_api_gw = "app.api.${var.env_name == "prod" ? "" : var.env_name}mydomain.com"
    }
}

So when env_name="prod" I want the output app.api.mydomain.com and for anything else - let's say env_name="staging" I want app.api.staging.mydomain.com.

But the above will output app.api.stagingmydomain.com <-- notice the missing dot after staging.

I tried concating the "." if the env_name was anything but "prod" but Terraform errors:

data "null_data_source" "api_gw_url" {
    inputs = {
      main_api_gw = "app.api.${var.env_name == "prod" ? "" : var.env_name + "."}mydomain.com"
    }
}

The error is __builtin_StringToInt: strconv.ParseInt: parsing ""

The concat() function in TF appears to be for lists not strings.

So as the title says: How do you do simple string concatenation in Terraform?

I can't believe I'm asking how to concat 2 strings together XD

Update:

For anyone that has a similar issue I did this horrific workaround for the time being:

main_api_gw = "app.api.${var.env_name == "prod" ? "" : var.env_name}${var.env_name == "prod" ? "" : "."}mydomain.com"

2

5 Answers

I know this was already answered, but I wanted to share my favorite:

format("%s/%s",var.string,"string2")

Real world example:

locals {
 documents_path = "${var.documents_path == "" ? format("%s/%s",path.module,"documents") : var.documents_path}" 
}

More info:

2

Try Below data resource :

data "null_data_source" "api_gw_url" {
    inputs = {
      main_api_gw = "app.api${var.env_name == "prod" ? "." : ".${var.env_name}."}mydomain.com"
    }
}
2

so to add a simple answer to a simple question:

  • enclose all strings you want to concatenate into one pair of ""
  • reference variables inside the quotes with ${var.name}

Example: var.foo should be concatenated with bar string and separated by a dash

Solution: "${var.foo}-bar"

0

For Terraform 0.12 and later, you can use join() function:

join(separator, list)

Example:

> join(", ", ["foo", "bar", "baz"])
foo, bar, baz
> join(", ", ["foo"])
foo

If you just want to concatenate without a separator like "foo"+"bar" = "foobar", then:

> join("", ["foo", "bar"])
foobar

Reference:

Use the Interpolation Syntax for versions < 0.12

after lot of research, It finally worked for me. I was trying to follow , but it did not work. Seems string can't be handled inside the expressions.

data "aws_vpc" "vpc" {
  filter {
    name   = "tag:Name"
    values = ["${var.old_cluster_fqdn == "" ? "${var.cluster_fqdn}" : "${var.old_cluster_fqdn}"}-vpc"]
    
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest