I have an issue with Terraform module which should pass the name of resource to another module to retrieve the ID of the resource, but the name of resource is contained in list of objects.
The module should create Azure application gateway.
Terraform Registry reference:
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/application_gateway
The block in question from Registry is:
- gateway_ip_configuration - (Required) One or more gateway_ip_configuration blocks as defined below.
A gateway_ip_configuration block supports the following:
- name - (Required) The Name of this Gateway IP Configuration.
- subnet_id - (Required) The ID of the Subnet which the Application Gateway should be connected to.
So I need the ID of the subnet and I can have multiple gateway_ip_configuration blocks. To be able to evaluate multiple blocks, I need to use a list of objects.
Code:
Child module contains following code (I’ll include only the relevant part of the code):
resource "azurerm_application_gateway" "appgw" {
...
dynamic "gateway_ip_configuration" {
for_each = try(var.settings.gateway_ip_configuration, null) = null? [] : [1]
content {
name = var.settings.gateway_ip_configuration.name
subnet_id = var.subnet.id
}
}
So subnet_id should be retrieved as a var.subnet.id from parent module.
Parent module contains following code (I’ll include only the relevant part of the code):
module "application_gateway" {
for_each = var.application_gateways
name = each.key
settings = each.value
subnet_id = module.subnet[each.value.gateway_ip_configuration.subnet_name].id //This line is the issue
}
So subnet_id should be retrieved from module.subnet which references the value from list of objects. I’m sure that each.value.gateway_ip_configuration.subnet_name line cannot retrieve the subnet_name value from list of objects - the syntax is wrong.
Tfvars file contains following code (I’ll include only the relevant part of the code):
application_gateways = {
appgw_name = {
gateway_ip_configuration = [
{
name = "gateway_config_name"
subnet_name = "subnet_name"
}
]
}
}
So “subnet_name” should be retrieved from gateway_ip_configuration list of objects and used in parent module to evaluate the subnet_name.id in child module module.subnet.
The error I get during plan stage is:
Error: Unsupported attribute
on terraform.tf line xx, in module “application_gateway”:
subnet_id = module.subnet[each.value.gateway_ip_configuration.subnet_name].id
each.value.gateway_ip_configuration is tuple with 1 element
This value does not have any attributes.
So the issue is that I don’t know how to fetch the subnet_name from list of objects and pass it to subnet module to get the ID back.
This is the only scenario where I’m unable to retrieve such an information. If I rewrite the code to not use the parent/child module scenario, just azurerm resource scenario, I can fetch the information, because I’m not trying to pass it from one module to another. But I’d really like to get to the bottom of this scenario.
Any suggestions?