Search code examples
listterraformconcatenation

Concat two lists in terraform


I have the following strings and lists, i need to merge both the lists.

str1 = "Cost"
str2 = "peervalue"
str3 = "date"
str4 = "value2"
samplelist1 = ["network_spoke1","network_spoke2","network_spoke3","network_spoke4"]
samplelist2 = ["valueA","valueB","valueC","valueD"]

I need the output in the below format. How can that be achieved

FinalList = [
"Cost,network_spoke1,peervalue,date,valueA,value2"
"Cost,network_spoke2,peervalue,date,valueB,value2"
"Cost,network_spoke3,peervalue,date,valueC,value2"
"Cost,network_spoke4,peervalue,date,valueD,value2"
]

Solution

  • You can do this as follows:

      FinalList = [for v1, v2 in zipmap(local.samplelist1, local.samplelist2): "${local.str1},${v1},${local.str2},${local.str3},${v2},${local.str4}"]