I am trying to use a variable list of objects to define the value's type and defaults, and use that in a dynamic block. I know there is an experimental feature, but just wondering how I would do this without the experimental feature?
variables.rf
variable "identity" {
type = list(object({
type = string
identity_ids = list(string)
}))
default = [
{
type = null
identity_ids = null
}
]
}
main.tf
resource "azurerm_cognitive_account" "azure" {
# Required
name = var.name
location = var.location
resource_group_name = var.resource_group_name
kind = var.kind
sku_name = var.sku_name
dynamic "identity" {
for_each = var.identity
content {
type = identity.value.type
identity_ids = identity.value.identity_ids
}
}
}
Use as a module
module "cognitive_account" {
source = "../modules/cognitive-account"
name = "name"
location = "Australia East"
resource_group_name = module.rg.name
kind = "TextAnalytics"
sku_name = "S"
custom_subdomain_name = "unique-name"
identity = [{
type = "SystemAssigned"
}]
}
Using that code gives me an error:
│ Error: Invalid value for module argument
│
│ on main.tf line 66, in module "cognitive_account":
│ 66: identity = [{
│ 67: type = "SystemAssigned"
│ 68: }]
│
│ The given value is not suitable for child module variable "identity" defined at .terraform\modules\cognitive_account\variables.tf:123,1-20: element 0:
│ attribute "identity_ids" is required.
I am not sure how to deal with omitting identity_ids from the object block, I thought the default being null would take care of it.