amazon-web-servicesterraformterraform-provider-awsterraform-modules

How to Aggregate EC2 Instance IDs from a Module and Attach to Target Groups in AWS ALB using Terraform


Problem Description:

I’m working on a Terraform setup where I have multiple EC2 instances (for Splunk services) managed using a custom module (splunk_instances), and I’m trying to attach these instances to specific target groups in an AWS Application Load Balancer (ALB).

In my root main.tf, I’m using a module that creates several Splunk instances like the search head (splunk_sh), indexers, and others. These instances are created using a custom EC2 module. I want to collect the instance IDs from this module and pass them to the ALB module to attach each instance to its corresponding target group.

My current setup:

Root main.tf (using the splunk_instances module):

module "splunk_instances" {
  source = "./modules/ec2" # Path to the module

  for_each = {
    "splunk_sh"       = "Search Head"
    "splunk_idx1"     = "Indexer 1"
    "splunk_idx2"     = "Indexer 2"
    "splunk_idx3"     = "Indexer 3"
    "splunk_cm"       = "Cluster Manager"
    "splunk_lm_ds_mc" = "License Manager / Deployment Server / Monitoring Console"
  }

  instance_name        = each.value
  instance_type        = var.instance_types[each.key]
  ami_id               = var.ami_id
  key_name             = var.key_name
  network_interface_id = aws_network_interface.splunk_eni[each.key].id
  # Other variables...
}

# ALB configuration
module "load_balancer" {
  source = "./modules/alb"
  instance_ids = module.splunk_instances.instance_ids # Reference to instance IDs from splunk_instances module
  # Other variables...
}

modules/ec2/outputs.tf:

output "instance_id" {
  value = aws_instance.this.id
}

Expected Output:

In my root module’s outputs, I want to aggregate all instance IDs:

output "instance_ids" {
  value = {
    for instance_key, instance_module in module.splunk_instances :
    instance_key => instance_module.instance_id
  }
}

Issue:

Terraform throws an error:

Error: Unsupported attribute │ │ on main.tf line 55, in module "load_balancer": │ 55: instance_ids = module.splunk_instances.instance_ids │ ├──────────────── │ │ module.splunk_instances is object with 6 attributes │ │ This object does not have an attribute named "instance_ids".

It seems the instance_id output is not being correctly passed from each EC2 instance created by the splunk_instances module. I’m unsure how to properly aggregate and reference these instance IDs in the ALB target group attachment.

What I’ve Tried:

Desired Outcome:

I want to be able to:

Question:

How can I correctly aggregate the instance_id outputs from the splunk_instances module and use them in the ALB module to attach EC2 instances to target groups?


Solution

  • Since the EC2 module, when called, has created instances based on the values from the for_each meta-argument, this means any outputs should use the key-value pairs:

    output "instance_ids" {
      value = values(module.splunk_instances)[*].instance_id
    }
    

    Here the built-in values function is used with all the keys and the instance_id output. You can conclude that you have to use the keys to reference the output based on the error:

    module.splunk_instances is object with 6 attributes

    because six Splunk instances are created when you call the module with for_each.