Home > database >  Filter azure terraform backend_address_pool outputs, where name of backend pool and get the id.?
Filter azure terraform backend_address_pool outputs, where name of backend pool and get the id.?

Time:01-18

I have azure terraform application gateway outputting backend pools, I want to get backendpool id by filtering the name. This id will be passed to other module to attach the VMSS resource creation.

 ```   
 output "backend_address_pool" {
 value = azurerm_application_gateway.ag[*].backend_address_pool

  } ```

Above azurem terraform will output following

   backend_address_pool = [
   toset([
    {
      "fqdns" = toset([])
      "id" = "/subscriptions/10b5e1f01f7/resourceGroups/test-blue/providers/Microsoft.Network/applicationGateways/ag-internal/backendAddressPools/activityvnetb-beap-app12"
      "ip_addresses" = toset([])
      "name" = "activity--vnetb-beap-app12"
    },
    {
      "fqdns" = toset([])
      "id" = "/subscriptions/10b5e1f01f7/resourceGroups/test-blue/providers/Microsoft.Network/applicationGateways/ag-internal/backendAddressPools/externalvnetb-beap-app6"
      "ip_addresses" = toset([])
      "name" = "external--vnetb-beap-app6"
    },
    {
      "fqdns" = toset([])
      "id" = "/subscriptions/10b5e1f01f7/resourceGroups/test-blue/providers/Microsoft.Network/applicationGateways/ag-internal/backendAddressPools/hivnetb-beap-app8"
      "ip_addresses" = toset([])
      "name" = "hif-vnetb-beap-app8"
    }])] 

So basically how do i get backend address pool filtering the name= "hif-vnetb-beap-app8" or "activity--vnetb-beap-app12" or external--vnetb-beap-app6"

I have tried to to filter but this gives all pool ids only,

value = azurerm_application_gateway.ag[*].backend_address_pool[*].id 

I want to pass the output from gateway module and get the id according to the name filter. currently i am getting pool id by using index but this is not consistent, if we add more backend pool the index keeps changing and not stays fixed.

agw_pool_id  = module.agw-internal-gateway.agw_internal_lb_pool_id[2]

I am thinking if there is way to grab the id like below ?

agw_pool_id  = module.agw-internal-gateway.agw_internal_lb_pool_id[name=agw_pool_id]

CodePudding user response:

Your backend_address_pool is a list of lists. You have to iterate to filter by specific name that you want. For example:

agw_pool_id = flatten([for v1 in azurerm_application_gateway.ag[*].backend_address_pool: 
              [  for v2 in v1: v2.id if v2.name == "hif-vnetb-beap-app8"
              ] ])[0]
  • Related