Unable to pass a module's output as input to another module

Hello everybody, this is my first post in this community and also fairly new to Terrafrom. Please correct or disregard deviation from community guidelines.

I currently have a directory structure as below and I am trying to output db_endpoint from cosmosdb module and use it in environments/dev/keyvault.tf to pass it as a secret
├── environments
│ └── dev
│ ├── cosmosdb.tf
│ ├── keyvault.tf
│ ├── variables.tf
├── modules
│ ├── cosmosdb
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ ├── keyvault
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
Here is my output from cosmosdb module. modules/cosmosdb/output.tf
output “db_endpoint” {
value = azurerm_cosmosdb_account.example.endpoint
description = “Database endpint url”
}
Below is what I have in environments/dev/keyvault.tf (tried using data source, but it still comes back with same error)

/data “azurerm_cosmosdb_account” “dbconnectionurl” {
name = module.cosmosdb.name
resource_group_name = var.resourcegroupname
endpoint = module.cosmosdb.db_endpoint
}
/
module “keyvault-dev” {
source = “…/…/modules/keyvault”
secrets = {
“dbConnectionUrl” = module.cosmosdb.db_endpoint
}
}
When I run plan, it errors out with a message “Reference to undeclared module”
Error: Reference to undeclared module

on keyvault.tf line 21, in module “keyvault-dev”:
21: “dbConnectionUrl” = module.cosmosdb.db_endpoint

No module call named “cosmosdb” is declared in the root module.

Can someone please help on how i can achieve this? Please let me know if you need more information.

Hi @sai-ns!

Do you have a module "cosmosdb" block in your configuration? It should be in the same module as the module "keyvault-dev" block.

You are awesome. your question itself have solved my issue.
I have a

module cosmosdb-dev

block in environments/dev/cosmosdb.tf (same location as keyvault.tf). So, based on your answer I am guessing I should be able to access dbendpoint url by module.cosmosdb-dev.db_endpoint?

I changed module.cosmosdb to module.cosmosdb-dev and it worked like a charm.
Is that how we always input values from module outputs?

Thanks a lot for your quick response.

I often dump the module output during coding so I can see which values are defined and how I can get hold of them.

output "cosmosdb-module" {
  value = module.cosmosdb
}

Okay, will give this a shot as well. Thanks!