For_each module with jsondecode

Hi All,

I am using terraform 0.13.4 and I wrote a module to work with the Azure Service Bus. One of the unique challenges I have is our development teams want use the asyncapi 2.0 as their event framework/documentation. With that said, I am rendered a .json file that contains “topics”. I’m trying to determine the best proper way to parse this json and use it to create both topics with my module. Im trying to use make use of jsondecode but having no luck when I get to using the for_each in the module. Any recommendations would be greatly helpful. Upon using terraform console I receive The given “for_each” argument value is unsuitable: the “for_each” argument must be a map, or set of strings, and you have provided a value of type tuple. Is there an easier way to get this data to map/string or can I just actively feed this json by changing how its called in the module?

topicsfile.json

{
    "appName": "Application1",
    "publishes": [{
            "topicName": "topica-sbt-v1"
        },
        {
            "topicName": "topicb-sbt-v1"
        }
    ],
    "subscriptions": []
}

topics.tf

provider "azurerm" {
  version         = "=2.20.0"
  subscription_id = "X"
  features {}
}
locals {
  json_data = jsondecode(file("../Topics/Topicfile.json"))
}

module "Demo" {
  for_each      = local.json_data.publishes
  source        = "../modules/azurerm_servicebus_topic"
  asb-name      = "asb-dev-namespace1"
  asb-topic     = each.value
  resourcegroup = "dev-rg-servicebus-eastus2"
}

I wanted to provide an update. I changed this around and I don’t really like it, however for_each does not appear to like tuples. My question now, is there a better way to decodejson into a map instead of dealing with the tuple?

Here is the change to resolve the tuple / for_each issue

topics.tf

provider "azurerm" {
  version  = "=2.20.0"
  subscription_id   = "X"
  features {}
}
locals {
  json_data = jsondecode(file("../Topics/Topicfile.json"))
}

module "Demo" {
  for_each = { for t in local.json_data.publishes : t.topicName => t }
  source    = "../modules/azurerm_servicebus_topic"
  asb-name  = "asb-dev-namespace1"
  asb-topic = each.value.topicName
  resourcegroup = "dev-rg-servicebus-eastus2"
}