Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 748
Force Hero Avatar asked Mar 23 '19 10:03

Force Hero


People also ask

How do you concatenate two variables in terraform?

For Terraform 0.12 and later, you can use the join() function, to allow you to join or concatenate strings in your Terraform plans. The terraform join function has two inputs, the separator character and a list of strings we wish to join together.

How do you concatenate strings together?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

What is string concatenation give example?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalisations of concatenation theory, also called string theory, string concatenation is a primitive notion.


2 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:
https://www.terraform.io/docs/configuration/functions/format.html

like image 90
Juan Manuel Garcia del Moral Avatar answered Sep 26 '22 07:09

Juan Manuel Garcia del Moral


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"     } } 
like image 35
sarvesh shetty Avatar answered Sep 25 '22 07:09

sarvesh shetty