简体   繁体   English

为负载均衡器侦听器规则编写动态 Terraform 块

[英]Write a dynamic Terraform block for a load balancer listener rule

I'm new to dynamic blocks and am having some trouble writing rules to listeners on a load balancer that was created using for_each .我是动态块的新手,在为使用for_each创建的负载平衡器上的侦听器编写规则时遇到了一些麻烦。

Below are the resources I created:以下是我创建的资源:

resource "aws_lb_listener" "app_listener_forward" {
  for_each          = toset(var.app_listener_ports)
  load_balancer_arn = aws_lb.app_alb.arn
  port              = each.value
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-TLS-1-2-Ext-2018-06"
  certificate_arn   = var.ssl_cert

  default_action {
    type = "forward"
    forward {
      dynamic "target_group" {
        for_each = aws_lb_target_group.app_tg
        content {
          arn = target_group.value["arn"]
        }
      }
      stickiness {
        enabled  = true
        duration = 86400
      }
    }
  }
}

resource "aws_lb_listener_rule" "app_https_listener_rule" {
  for_each     = toset(var.app_listener_ports)
  listener_arn = aws_lb_listener.app_listener_forward[each.value].arn

  action {
    type = "forward"
    forward {
      dynamic "target_group" {
        for_each = aws_lb_target_group.app_tg
        content {
          arn = target_group.value["arn"]
        }
      }
    }
  }

  dynamic "condition" {
    for_each = var.images
    path_pattern {
      content {
        values = condition.value["paths"]
      }
    }
  }
}

resource "aws_lb_target_group" "app_tg" {
  for_each    = var.images
  name        = each.key
  port        = each.value.port
  protocol    = "HTTP"
  target_type = "ip"
  vpc_id      = aws_vpc.app_vpc.id

  health_check {
    interval            = 130
    timeout             = 120
    healthy_threshold   = 10
    unhealthy_threshold = 10
  }

  stickiness {
    type            = "lb_cookie"
    cookie_duration = 86400
  }
}

Below are how the variables are defined:以下是变量的定义方式:

variable "images" {
  type = map(object({
    app_port = number
    paths = set(string)
  }))
  {
    "app-one" = {
      app_port = 3000
      paths = [
        "/appOne",
        "/appOne/*"
      ]
    }
    "app-two" = {
      app_port = 4000
      paths = [
        "/appTwo",
        "/appTwo/*"
      ]
    }
  }

variable "app_listener_ports" {
  type = list(string)
  default = [
    80, 443, 22, 7999, 8999
  ]
}

Upon executing, I am getting an error dealing with the path_pattern being unexpected:执行后,我收到一个处理path_pattern意外的错误:

Error: Unsupported block type
│
│   on alb.tf line 78, in resource "aws_lb_listener_rule" "app_https_listener_rule":
│   78:     path_pattern {
│
│ Blocks of type "path_pattern" are not expected here.

I've tried a few ways to get this dynamic block but am having some difficulty.我已经尝试了几种方法来获得这个动态块,但遇到了一些困难。 Any advice would be appreciated.任何意见,将不胜感激。

Thank you!谢谢!

Try it like this:像这样尝试:

  dynamic "condition" {
    for_each = var.images
    content {
      path_pattern {
        values = condition.value.paths
      }
    }
  }

And change the type of paths from set(string) to list(string) .并将路径类型从set(string)更改为list(string)

This is also completely acceptable:这也是完全可以接受的:

  dynamic "condition" {
    for_each = var.images
    content {
      path_pattern {
        values = condition.value["paths"]
      }
    }
  }

However, in my opinion here it's better to not use a dynamic block for the condition to maintain readability and maintenance.但是,在我看来,最好不要对条件使用动态块以保持可读性和维护性。

  condition {
    path_pattern {
      values = [
        "/appOne",
        "/appOne/*" ## can also use variables if you prefer !!
      ]
    }
  }

I have already answered your original post related to the problem which you had after fixing the dynamic syntax.我已经回答了您在修复动态语法后遇到的与问题相关的原始帖子。

Post URL: Error when creating dynamic terraform rule for alb listener rule发布 URL: 为 alb 侦听器规则创建动态 terraform 规则时出错

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为 alb 侦听器规则创建动态 Terraform 规则时出错 - Error when creating dynamic terraform rule for alb listener rule terraform output Google Kubernetes 集群入口负载均衡器 Z957B527BCFBAD3E380F58ZD2068 - terraform output Google Kubernetes cluster inggress load balancer ip Terraform - 启用访问负载平衡器日志 InvalidConfigurationRequest:存储桶的访问被拒绝 - Terraform - Enabling Access Load balancer logs InvalidConfigurationRequest: Access Denied for bucket terraform aws_lb_listener_rule 条件争论未在 terraform 0.12.20 中得到认可 - terraform aws_lb_listener_rule condition arguement not getting recognized in terraform 0.12.20 Terraform 动态选项块中的条件选项设置 - Terraform conditional option_settings in a dynamic option block 无法将目标 ID(启动配置)附加到 Terraform 中的网络负载均衡器的目标组 - Not able to attach Target id(launch configurtion) to the target group of network load balancer in Terraform 在 Terraform for Cloud Run 中创建动态秘密变量块 - Creating a dynamic secret variable block within Terraform for Cloud Run 我们如何在 terraform 中声明 gcp cloud armor 高级模式规则选项块? - How can we declare a gcp cloud armor advance mode rule options block in terraform? kube.netes 集群的负载均衡器 - Load balancer for kubernetes clusters terraform 在尝试创建负载均衡器时为 GCP 返回“invalid_grant”,我无法以所有者身份查看或编辑 SA 权限 - terraform returns 'invalid_grant' for GCP when attempting to create load balancer and I cannot view or edit SA permissions as owner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM