Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine list and object in terraform

How do I combine something like

[for x in var.variable : { "key" : x , "value" : true}] and { "key" : "*", "value" : true}

Say var.variable is an array/list with values [1,2]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and then I need to perform a jsonencode on the above result in terraform.

I have tried merge, join, concat but somehow nothing seems to work or maybe I dont know how to use them correctly.

I need the final output something like –

[
   {
     "key" : 1,
     "value" : true

   },
{
     "key" : 2,
     "value" : true

   },
{
     "key" : "*",
     "value" : true

   },
]

>Solution :

You can do that as follows:

variable "variable" {
  default = [1,2]
}

output "test" {
  value = jsonencode(concat(
            [for x in var.variable : { "key" : x , "value" : true}], 
            [{ "key" : "*", "value" : true}])) 
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading