I am using below module structure to code a modular approach for creating a cosmos-db account and subsequent database and containers respectively.
module
–>account
—>database
—>container
Each module has its own variables.tf, main.tf and output.tf file. I am not able to use the cosmos-db-account name into modules for database and containers as “name” is not exported by cosmos-db account(azurerm_cosmos_db) resource. Can anyone suggest any mechanism through which i can use same module structure to create the respective resource.
My code snippet around is below.
account module–> main.tf
resource “azurerm_cosmosdb_account” “cosmos_db” {
name = var.cosmos_acc_name
location = var.location
resource_group_name=var.resource_group_name
offer_type = "Standard"
kind= "GlobalDocumentDB"
tags = var.tags
consistency_policy {
consistency_level = "Session"
}
geo_location {
location = var.location
failover_priority = 0
}
}
account–>output.tf
output “cosmos_acc_name” {
value = azurerm_cosmosdb_account.cosmos_db.name
}
database–> main.tf
resource “azurerm_cosmosdb_sql_database” “sql_name” {
name = var.db_name
resource_group_name = var.resource_group_name
account_name = var.cosmos_acc_name
}
The above code in database–>main.tf is throwing errors
What is the error you are seeing? What does the code that is calling these modules look like?
I have attached both the modules and main configuration file. Below is the error i am receiving.accounts_main.txt (404 Bytes) accounts_output.txt (84 Bytes) accounts_variables.txt (531 Bytes) database_main.txt (171 Bytes) database_output.txt (175 Bytes) database_variables.txt (371 Bytes) main.txt (1.2 KB)
For the azurerm_cosmosdb_sql_database resource the required fields are name, resource_group_name and account_name. Your module is setting those to the variables db_name, resource_group_name and cosmos_acc_name respectively.
In the module’s variables.tf you are then defining all three variables as optional (by giving them default values), with the defaults for both resource_group_name and cosmos_acc_name being “” (empty string). This isn’t recommended as the empty string isn’t a valid option for either value. Instead the default should be removed for both.
Looking at the main.tf in the root module you can see that only cosmos_acc_name is being passed into the database module. As a result the values for db_name and resource_group_name are set to their defaults (DeviceManagement and empty string).
As the empty string isn’t valid for resource_group_name you are getting the error you pasted.
It is important that defaults are only set when the variable should be optional and there is a sensible valid default available. A default for db_name seems reasonable, but neither resource_group_name or cosmos_acc_name have a value that could be used as a default, so as mentioned the default line should be removed from the module’s variables.tf.
If that is done you would then receive an error that there is a missing required variable for the database module. It would then prompt you to add that variable, with the value from the rg_name azurerm_resource_group resource.
Thank you very much. Passing the resource-group-name in the database module resolved the issue. Also got more clear around putting in the default values for the variables defined in each modules. Cheers 