简体   繁体   中英

Terraform outputs to another module when using a for_each

I'm having trouble referencing the outputs of a module as an input to another module. I have a module that creates the target group using a for_each. I have another module that is also using a for_each for creating a listener rule.

I would like to use the output from the target group as an input for the target group under the listener rule module - but be able to specify which target group would attach to the listener rule.

I have tried many different suggestions with numerous variations for outputs but have not been successful.

In my main, I have these two modules created for the target groups and the listener rules:

Target Groups:

module "aws_lb_target_group_ui" {
  source   = "../../terraform/modules/aws_lb_target_group"
  for_each = var.target_group_values

  target_group_name        = each.value["name"]
  target_group_target_type = each.value["target_type"]
  target_group_port        = each.value["port"]
  target_group_protocol    = each.value["protocol"]
  target_group_slow_start  = each.value["slow_start"]
  /* Health Check */
  target_group_health_check_path              = each.value["healthcheckpath"]
  target_group_health_check_port              = each.value["healthcheckport"]
  target_group_health_check_enabled           = each.value["healthcheckenabled"]
  target_group_health_check_healthy_threshold = each.value["healthy_threshold"]
  target_group_health_check_interval          = each.value["interval"]
  target_group_health_check_timeout           = each.value["timeout"]
  target_group_tags = {
    Environment = each.value["Environment"]
    Component   = each.value["Component"]
    Application = each.value["Application"]
    TechContact = each.value["TechContact"]
  }
}

Listener Rule:

module "aws_alb_listener_rule_ui" {
  depends_on = [
    module.aws_lb_target_group_ui,
    module.aws_alb_listener_ui_https
  ]

  source        = "../../terraform/modules/aws_lb_listener_rule_https"
  listener_rule = module.aws_alb_listener_ui_https.ui_lb_listener
  for_each      = var.ui_https_listener_rules

  listener_rule_host_header  = each.value["values"]
  listener_rule_target_group = each.value["target_group"]
  listener_rule_action_type  = var.ui_https_listener_rule_action_type
}

The resources themselves are very generic with variables but I can post those if it's helpful.

In my tfvars I have this:

Target Groups:

I have removed all the variables but just wanted to get an idea of it would replicate out in my config through the tfvars.

target_group_values = {
  Application1 = {
    "name"        = "TargetGroup1",
  },
  Application2 = {
    "name"        = "TargetGroup2",
  },
}

Listener Rules:

ui_https_listener_rule_action_type = "forward"
ui_https_listener_rules = {
  Application1 = {
    values = ["Host_Header_1"]
    target_group = "Manul_ARN_Entry"
  },
  Application2 = {
    values = ["Host_Header_2"]
    target_group = "Manul_ARN_Entry"
  },
}

If I run it in this format with the manually arn being entered from an existing target group it runs without issue. I would like to utilize the target groups I am creating, though and get the output from those groups into the code instead of the manual entry.

What I was trying to accomplish was to get the output and then update the "listener_rule_target_group" value with the output but be able to identify one of the specific target groups as it would be a 1:1 for target groups and rules.

I have not posted the output as I have not been successful in a method yet but was more looking for some help on what I should be doing with this setup.

Update:

I was able to resolve this.

I moved "values" and "target_group_values" variables into the "target_group_values" variable and deleted the "ui_https_listener_rules" variable.

This allowed me to use one variable across both modules for my for_each. From there I was able to use the below to resolve the issue.

listener_rule_target_group = module.aws_lb_target_group_kinsale_ui[each.key].arn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM