Insertion of a new object in a nested map of objects

Given this map of objects

old_dns = {
  public = {
    default = {
      name = "public.default.com"
      records = {
        service1 = {
          name    = "service1"
          type    = "A"
          ttl     = 300
          records = ["1.2.3.4"]
        }
        service2 = {
          name    = "service2"
          type    = "A"
          ttl     = 300
          records = ["2.3.4.5"]
        }
      }
      tags = {
        foo = "bar"
      }
    }
  }
  private = {
    default = {
      name = "private.default.com"
      records = {
        service1 = {
          name    = "service1"
          type    = "A"
          ttl     = 300
          records = ["1.2.3.4"]
        }
        service2 = {
          name    = "service2"
          type    = "A"
          ttl     = 300
          records = ["2.3.4.5"]
        }
      }
      tags = {
        foo = "bar"
      }
    }
  }
}

I want to insert this object into the records map in private:

new_public_record = {
  service3 = {
    name    = "service3"
    type    = "A"
    ttl     = 300
    records = ["3.4.5.6"]
  }
}

I can’t quite get my head round the syntax needed in the locals block. Feels like something that might be done pretty easily with the .update() method in python - is reshaping the data with locals even a good idea? pretty frustrated with this :cry:

But why do you want to do this?

Terraform is intended for declaring desired state.

Although the expression language allows for some manipulation of the format of values so they can be defined in various ways, it’s just about mapping one point in time state to a different format.

The fact you have “old” and “new” in your variable names suggests that you are trying to model an imperative change, in a system that is solely declarative - which is why it’s so frustrating.

well actually I just used that to try and make the two obviously different - I was hoping to merge the two so I ended up with a single map which declared the existing state

Right I think I know what you mean. I’ll restructure the map and I may post back the approach. I’d be interested in comments if poss. :grinning: