Module output - Error: Unsupported attribute - This value does not have any attributes

I am refactoring some code to use a module and have become stuck with getting the output from the module to work.

The current error is

Error: Unsupported attribute

  on source/_locals.tf line 123, in output "batman_instances_mod":
 123:   value = [module.batman_tg.batman_instances-module]

This value does not have any attributes.

Before the refactoring the output would give a map of the instance Ids. Code has been simplified to ease of reading here. I’ve kept all the important bits. The data call loops through each group in the locals and then outputs the instance IDs based on the filter.

locals {

batman_target_groups = {
  protocol = "HTTP"
  attachment_port = 80
  groups = {
    first = {
      name = "one-${var.zone}"
      instance_filter = [
        "node-1*",
        "node-2*"]
    },
    two = {
      name = "two-${var.zone}"
      instance_filter = [
        "node-3*"]
    },
    three = {
      name = "three-${var.zone}"
      instance_filter = [
        "node-4*",
        "node-5*"]
    }
  }
}

}


data "aws_instances" "batman" {
    for_each = local.batman_target_groups.groups
    filter {
        name = "tag:Name"
        values = each.value.instance_filter
    }
}

output "batman_instances" {
    value = [for p in keys(data.aws_instances.batman) :  data.aws_instances.batman[p].ids]
}

When I move this into the module I get the error above. The I call the module using a count based on the account it’s being run against

module "batman_tg" {
  count = var.account == "batman" ? 1 : 0
  target_group = local.batman_target_groups.groups
  source = "../modules/target_group"
}

And this is the code within the module

variable target_group {}

data aws_instances "this" {
  for_each = var.target_group
  filter {
    name   = "tag:Name"
    values = each.value.instance_filter
  }
}

output "dna_instances-module" {
  value = [for p in keys(data.aws_instances.this) :  data.aws_instances.this[p].ids]
}

I’m not sure what needs to be amended to get the module output to work. TIA

1 Like

Same Issue is there with my code as well

Hi @nmarchini,

Your module declaration is using count, which means that the module.batman_tg is a list of 0 or more instances of that module.

If you want your output to contain a list of the module.batman_tg output values, you can use a for expression, or more conveniently, a splat expression.

value = module.batman_tg[*].batman_instances-module
3 Likes