Get substring from a list of strings in terraform

3.4k views Asked by At

There is a list of strings which are the output of vnet peering details. I need to extract all the source vnet names in one list and destination vnet names in another list. My vnet peering names are as below

Peer =["vnet1tovnet2",
       "vnet1tovnet3",
       "vnet4tovnet5"]

I need two lists in below format

source=["vnet1","vnet1",vnet4"]
dest=["vnet2","vnet3","vnet5"]

How can this be achieved in terraform

1

There are 1 answers

4
Marcin On BEST ANSWER

You can do that as follows:

locals{

  Peer =["vnet1tovnet2",
         "vnet1tovnet3",
         "vnet4tovnet5"]
    
  source = [for v in local.Peer: split("to", v)[0]]
  dest = [for v in local.Peer: split("to", v)[1]]

}