使用 for_each 和 ARN 列表输出调用 Terraform 模块 [英] Terraform Module Call with for_each and ARN List Output

查看:36
本文介绍了使用 for_each 和 ARN 列表输出调用 Terraform 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过子模块调用根模块如下:

I am calling a root module via child module as follows:

子模块 - main.tf

  module "create_network_lb" {
  source             = "../../modules/lb_test"
  for_each           = var.target_groups
  
  name                   = each.key
  tg_name                = each.value.tg_name
  tg_backend_port        = each.value.tg_backend_port
  tg_backend_protocol    = each.value.tg_backend_protocol
  tg_hc_port             = each.value.tg_hc_port
  tg_hc_protocol         = each.value.tg_hc_protocol
  tg_htl_port            = each.value.tg_htl_port
  tg_htl_protocol        = each.value.tg_htl_protocol
  tg_htl_action          = lookup(each.value, "tg_htl_action", "forward")
  subnets                = tolist(data.aws_subnet_ids.private_compute[0].ids)
  vpc_id                 = sort(data.aws_vpcs.platform_private_vpc.ids)[0]
}

子模块 - vars.tf

variable "target_groups" {
  description = "A list of maps containing key/value pairs that define the target groups to be created. Order of these maps is important and the index of these are to be referenced in listener definitions. Required key/values: name, backend_protocol, backend_port"
  type        = map
  default     = {
    lb-1 = {
      tg_name = "test1"
      tg_backend_protocol = "TCP"
      tg_backend_port = "80"
      tg_target_type = "instance"
      tg_deregistration_delay = "180"
      tg_hc_healthy_threshold = 3
      tg_hc_interval = 30
      tg_hc_port = 80
      tg_hc_protocol = "TCP"
      tg_hc_unhealthy_threshold = 3
      tg_htl_port = "80"
      tg_htl_protocol = "TCP"
    }
    lb-2 = {
      tg_name = "test2"
      tg_backend_protocol = "TCP"
      tg_backend_port = "8080"
      tg_target_type = "instance"
      tg_deregistration_delay = "180"
      tg_hc_healthy_threshold = 3
      tg_hc_interval = 30
      tg_hc_port = 8080
      tg_hc_protocol = "TCP"
      tg_hc_unhealthy_threshold = 3
      tg_htl_port = "80"
      tg_htl_protocol = "TCP"
    }
  }
}

根模块 - main.tf

resource "aws_lb_target_group" "main" {
  name                               = var.tg_name
  vpc_id                             = var.vpc_id
  port                               = var.tg_backend_port
  protocol                           = var.tg_backend_protocol
  depends_on = [aws_lb.default]

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_lb_listener" "frontend_http_tcp" {
  load_balancer_arn = aws_lb.default.arn
  port              = var.tg_htl_port
  protocol          = var.tg_htl_protocol
  
  default_action {
    type = var.tg_htl_action
    target_group_arn = aws_lb_target_group.main.arn
  }

}

根模块 - output.tf

output "target_group_arns" {
  description = "ARNs of the target groups. Useful for passing to your Auto Scaling group."
  value       = aws_lb_target_group.main.arn
}

在子模块内部,调用附加模块来创建 Auto Scaling Group

Inside the child module, an additional module is called to create an Auto Scaling Group

module "create_autoscaling_group" {
  source                    = "../../modules/cloudformation_stack_autoscaling"
  template_name             = "launch-template"
  cf_stack_name             = "stack"
  autoscaling_group_name    = "asg"
  instance_type             = var.vm_type
  block_device_mappings     = var.block_device_mappings
  image_id                  = data.aws_ami.centos_ami.image_id
  vpc_security_group_ids    = concat(data.aws_security_groups.security_group_ids.ids, [module.create_security_group.id])
  user_data_base64          = data.template_cloudinit_config.config.rendered
  iam_instance_profile_name = var.iam_instance_profile_name
  volume_tags               = merge(local.vmss_tags,{"fds:cloudformation:stack-name"=local.stack_tag})
  ec2_tags                  = var.vmss_tags
  subnet_ids                = tolist(data.aws_subnet_ids.private_compute[0].ids)
  max_size                  = var.vm_count != 0 ? var.vm_count : var.max_size
  min_size                  = var.vm_count != 0 ? var.vm_count : var.min_size
  desired_capacity          = var.vm_count != 0 ? var.vm_count : var.desired_capacity
  health_check_type         = var.health_check_type
  health_check_grace_period = var.health_check_grace_period
  target_group_arns         = module.create_network_lb[*].target_group_arns
 

注意变量target_group_arns".在这里,我需要通过调用create_network_lb"模块创建的所有目标组的 arn,以便所有 LB 及其关联的目标组都由 1 个 Auto Scaling 组管理.

Notice the variable, 'target_group_arns'. Here I need the arn of all target groups created by the call to the 'create_network_lb' module, so that all LBs and its associated targets groups are managed by the 1 Auto Scaling Group.

从我读到的内容来看,当使用 for_each 时,就像我对 'create_network_lb' 模块调用所做的那样,我不能使用 splat 表达式.我尝试使用列表理解,但一定是做错了.任何帮助将不胜感激.

From what I read, when using a for_each, as I did with the 'create_network_lb' module call, I cannot use splat expressions. I tried using list comprehension but must be doing it wrong. Any help here would be appreciated.

推荐答案

你的模块 create_network_lb 将是一个地图,而不是列表.因此,您应该能够将所有 target_group_arns 引用为:

Your module create_network_lb will be a map, not list. Thus you should be able to reference all target_group_arns as:

target_group_arns = values(module.create_network_lb)[*].target_group_arns

这篇关于使用 for_each 和 ARN 列表输出调用 Terraform 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆