Dynamic nasted object structure creation

Output

mainstructure =
{ resource1 = {
env1 = {
key1 = var.test1
key2 = var.test2
key3 = var.test3
}
env2 = {
key1 = var.test4
key2 = var.test5
key3 = var.test6
}
}
resource2 = {
env1 = {
key1 = var.test7
key2 = var.test8
key3 = var.test9
}
env2 = {
key1 = var.test10
key2 = var.test11
key3 = var.test12
}
}
.
.
.
}

need this dynamic structure where resource1, resource2 will be incremented.

I tried null_resource but it didn’t help.

can anyone please help me create this dynamic nested object structure?

I don’t think so, no - you appear to be trying to do something very far away from what Terraform is intended for.

But, you haven’t really shared what you’re actually trying to accomplish, so it’s difficult to recommend what you should do instead…

Firstly, I need to create this nested map in runtime so that in case of any more resources got added my automation flow will take care of, and based on this nested map I will iterate through and create resources for key1, key2, key3…

in short, I have to create the same set of multiple resources, having 2 environments and 3 keys for each environment

ex:
Azurerepo_1
||—env
|||—secretkey1
|||—secretkey2
|||—secretkey3
||—env2
|||—secretkey4
|||—secretkey5
|||—secretkey6
Azurerepo_2
||—env
|||—secretkey7
|||—secretkey8
|||—secretkey9
||—env2
|||—secretkey10
|||—secretkey11
|||—secretkey12

You’ve not shared what your difficulty with creating your desired data structure is, or what you want to use as input data. Based on the limited information in your previous posts, “Why don’t you just type out the nested map containing all the data?” appears to be a valid response - but presumably you don’t want to do that, because otherwise you wouldn’t be asking the question.

However, I’m still doubtful that’s what you actually should be aiming for, as you indicate you want to “iterate through and create resources”, except that’s not a mode of operation Terraform supports. Terraform is inherently declarative - all resources need to be declared using resource blocks - and even if you use the for_each feature - The for_each Meta-Argument - Configuration Language | Terraform by HashiCorp - assembling a complex nested description of your environment as a data structure just means you then need to break it down again into the bits applying to each resource type.