Search code examples
listterraformconcatenation

Is it possible to concatenate a string value and a list of objects in terraform


Below is my terraform code where I have a list of objects which has 5 values, is it possible to concat each value in the list with the string values

locals{
mylist = ["aaa","bbb","ccc","ddd","eee"]
str1 = "hello"
str2 = "Data"

mergedstring =  "${local.str1},local.mylist,${local.str2}"
}

I need the output in the following format

hello,aaa,Data
hello,bbb,Data
hello,ccc,Data
hello,ddd,Data
hello,eee,Data

How can I achieve this?


Solution

  • You can do this as follows:

    locals{
      mylist = ["aaa","bbb","ccc","ddd","eee"]
      str1 = "hello"
      str2 = "Data"
    
      mergedstring = join("\n",[for v in local.mylist: "${local.str1},${v},${local.str2}"])
    }