Search code examples
terraformterraform-provider-azureterraform0.12+

How to access and assign object type variable value in terraform root


Objective: How to access and assign object type variable value from terraform root config

Code I am trying:

I am using modules in terraform. The following is one module I am using to create a cosmos DB account

child module:

resource "azurerm_cosmosdb_account" "cosmosaccount" {
  name                = var.account_name
  location            = var.location
  resource_group_name = var.rg_name  
  offer_type          = var.cosmos_db_config.cosmosdb_offer_type
  kind                = var.cosmos_db_config.cosmosdb_kind
  is_virtual_network_filter_enabled = "true"
  ip_range_filter     = var.cosmos_db_config.ip_range_filter
  enable_automatic_failover = false
  consistency_policy {
    consistency_level       = var.cosmos_db_config.cosmosdb_consistancy_level
    max_interval_in_seconds = 5
    max_staleness_prefix    = 100
  }
  geo_location {
    location          = var.location
    failover_priority = 0
  }
  tags = var.tags
}

child module variable.tf:

variable "cosmos_db_config" {
  default = {
    cosmosdb_offer_type = ""
    cosmosdb_kind   = ""
    cosmosdb_consistancy_level = ""
    ip_range_filter = ""
  }
  type = object(
    {
    cosmosdb_offer_type = string
    cosmosdb_kind   = string
    cosmosdb_consistancy_level = string
    ip_range_filter = string
    }
  )
}

main root variable.tf:

variable "cosmos_db_config" {
  default = {
    cosmosdb_offer_type = ""
    cosmosdb_kind   = ""
    cosmosdb_consistancy_level = ""
    ip_range_filter = ""
  }
  type = object(
    {
    cosmosdb_offer_type = string
    cosmosdb_kind   = string
    cosmosdb_consistancy_level = string
    ip_range_filter = string
    }
  )
}

Module call:

module "cosmosdb" {  
  source = "../modules/cosmosdb"
  account_name = "test"
  rg_name  = module.resource_group.name
  location = module.resource_group.location
  cosmosdb_offer_type = var.cosmos_db_config.cosmosdb_offer_type
  cosmosdb_kind = var.cosmos_db_config.cosmosdb_kind
  ip_range_filter = var.cosmos_db_config.ip_range_filter
  #subnet_id = var.subnet_id
  cosmosdb_consistancy_level = var.cosmos_db_config.cosmosdb_consistancy_level
  tags = local.common_tags
}

terraform.tfvars.json:

"cosmos_db_config": {
        "cosmosdb_offer_type": "Standard",
        "cosmosdb_kind": "GlobalDocumentDB",
        "cosmosdb_consistancy_level": "Session",
        "ip_range_filter": ""
    },

Error I get:

Error: Unsupported argument
│ 
│   on cosmosdb.tf line 6, in module "cosmosdb":
│    6:   cosmosdb_offer_type = var.cosmos_db_config.cosmosdb_offer_type
│ 
│ An argument named "cosmosdb_offer_type" is not expected here

Can you please suggest what mistake I am doing here ?


Solution

  • You need only to pass the variable that the module is expecting and that is cosmos_db_config and not the variable attributes:

    module "cosmosdb" {  
      source = "../modules/cosmosdb"
      account_name       = "test"
      rg_name            = module.resource_group.name
      location           = module.resource_group.location
      cosmosdb_db_config = var.cosmos_db_config
    
      tags = local.common_tags
    }