Merge List variable(same name) from two tfvars files

Terraform Version

0.13.7

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 main.tf ??

Expected Output:

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

@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