Convert 'map' variable to list

I have been going round in circles with this problem and hope someone can help me solve it…

I have a map variable that is passed in via TFVARs…
(The number of records can and does change)

appgw_dns_records = {
  "record0"   = {
    "name" = "record1"
    "zone_name" = "xxxxxxxxxxxxxxx.co.uk"
  },
  "record1"   = {
    "name" = "record2"
    "zone_name" = "xxxxxxxxxxxxxxx.co.uk"
  }
}

I want to use information from the map later on in my code as a list. The format I need is this…
[“record1.xxxxxxxxxxxxxxx.co.uk”, “record2.xxxxxxxxxxxxxxx.co.uk”]

Can anyone advise me if what I am trying to do is possible!? And how I go about it!? I have tried various combinations of count, format concat but cant figure out how to do it!

Thanks

I tried some stuff out locally with your code, and here’s a way to reference things that seems to work:

locals {
    appgw_dns_records = {
        "record0" = {
            "name" = "record1"
            "zone_name" = "xxxxxxxxxxxxxxx.co.uk"
        },
        "record1" = {
            "name" = "record2"
            "zone_name" = "xxxxxxxxxxxxxxx.co.uk"
        }
    }

    list = ["${local.appgw_dns_records.record0.name}.${local.appgw_dns_records.record0.zone_name}", "${local.appgw_dns_records.record1.name}.${local.appgw_dns_records.record1.zone_name}"]
}

output "list" {
    value = local.list
}

Hope that helps out a little!

Thanks @salaxander!

We can generalize that solution to work for any number of appgw_dns_records elements by using a for expression:

[for r in local.appgw_dns_records : "${r.name}.${r.zone_name}"]

You can use an expression like the above in the place where you need the list of hostnames, and its result value should be the same as what @JamesStanley showed in the initial example.

4 Likes

Thanks for the replies @salaxander and @apparentlymart !!

@apparentlymart - Will give this way a try! That looks like it will do exactly what I want with a dynamic number of records!

Cheers guys

Wow, this really helped me, thank you!