For loop over existing resources in tf file


I’m using the Cloudflare Terraform provider with a bunch of custom hostname definitions Terraform Registry

What I am currently trying to do is iterate over all existing resources of this type, from inside my .tf file and possibly read some information out of the associated state.
I’ve been playing with multiple functions to get some of the data I need since there is no data type of resources that would help me out here.

regex("[a-zA-Z0-9_]+",(split(" ",join(" ",regexall("cloudflare_custom_hostname.*",file("")))))[1])
  • get what is needed (extract the exact resource name)
range(1,length(split(" ",join(" ",regexall("cloudflare_custom_hostname.*",file(""))))),3) 
  • iterate every 3 items
length(split(" ",join(" ",regexall("cloudflare_custom_hostname.*",file("")))))
  • get total length of items

What I’m thinking would technically do the trick is something like this:

output "someoutput" {
    value = {
        for i in range(1, length(split(" ",join(" ",regexall("cloudflare_custom_hostname.*",file(""))))), 3): {
            regex("[a-zA-Z0-9_]+",(split(" ",join(" ",regexall("cloudflare_custom_hostname.*",file("")))))[i])

but I’m unsure of how to actually structure things as I’m being greeted with the following:

│ Error: Missing attribute value
│   on line 37, in output "someoutput":
│   36:         for i in range(1, length(split(" ",join(" ",regexall("cloudflare_custom_hostname.*",file(""))))), 3): {
│   37:             regex("[a-zA-Z0-9_]+",(split(" ",join(" ",regexall("cloudflare_custom_hostname.*",file("")))))[i])
│   38:         }
│ Expected an attribute value, introduced by an equals sign ("=").

Any advice on how to obtain some example attributes out of the state file is highly appreciated.
I’m trying to do a dynamic, one-off run to iterate over everything.


Your output type is a map so you have to build a map with name=value patterns.