Terraform apply is applying in in-correct order (patent / child module structure)

Hi there,
I have a parent/child module relationship where the child calls a private module (Azure repo) and because of that I am having dependency issues.

Here is how it looks:
My child module (Module A) calls this: Terraform Registry

Basically this module will create Postgres instance. I am calling it as

module postgres-server {
          source: Azure/avm-res-dbforpostgresql-flexibleserver/azurerm"
           ...Provide needed variables under here....
            }

Along with it I added couple of resources that will create key-vault and will store the admin creds in the key-vault.

Then I call the Module A from my Module B (For Dev env), Module D (For QA env), Module D (For Stage env)… and so on and this where I do the actual deployment.

While applying, it’s running the key-vault before its installing the Postgres-server itself and its failing.

Because of this multiple dependency, how can I modify or provide a way for the terraform apply to install the resource groups, postgres-server before it can apply other resources?

Cheers for the help here :saluting_face:

I think it’s hard to say from the information given here.

Broadly, I would try to keep modules inside of modules to a bare minimum (though you could look at tools like terragrunt, etc., as well as the new stacks feature to help with managing different environments), and if you want to create a dependency between a module you’re instantiating and resources declared in the same config / state, you’ll need to create an implicit dependency (usually preferred) or an explicit one.

This thread, while a slightly simpler use case, is probably a good one to look at: