Merge List variable(same name) from two tfvars files

Terraform Version


Terraform Configuration Files

Consider first tfvars file (one.tfvars) and a variable defined in it.

api_stages=[ { api_id= “xxxx” stage=“play” } ]

and second tfvars file (second.tfvars) also have same name variable

api_stages=[ { api_id= “yyy” stage=“play” } ]

How can we merge both variable into one and use in ??

Expected Output:

api_id= “yyy”
api_id= “xxxx”

@jbardin Please see if you can help here…

You can see the variable precedence definition in the docs here, note specifically the sentence:

If the same variable is assigned multiple values, Terraform uses the last value it finds, overriding any previous values.

There is no merging behavior here, you will need to combine the values manually as needed, making sure to use different names for the input from each file. In this case you could presumably use a list(map(string)) for each, and combine them into a local value using the concat function