Inject/Merge Key/Value in a List of Objects

I have a module where the input is a list of objects, I’m trying to add a random_string as a new key/value within this variable, how can I achieve this? is it possible?

vms = [
    region = "dfw"
    plan   = "vhf-2c-2gb"
    label  = "tf-dev-dns-dfw1-01"
    tag    = "dns-dev"
    id = "387"

Hi @amjanoni

You can use a “for expression” to iterate over the value and create new objects with the desired structure:

  input = [ for v in local.vms : {
      region = v.region
      plan = v.plan
      label = v.label
      tag = v.tag
      id =
      random_string = "new random string"

And in this case since the desired changes are all simple key-values at the top level of the object, you can simplify that by merging the new keys into the existing object value

  input = [ for v in local.vms : merge({random_string = "new random string"}, v) ]