【问题标题】:Terraform set a variable with conditional another variable presentTerraform 设置一个有条件存在另一个变量的变量
【发布时间】:2021-09-18 03:43:19
【问题描述】:

我只想在值存在时设置一个变量。

我的变量是:

variable "http_tcp_listeners" {
  description = "aws_lb_listener"
  type        = map(any)
  default = {
    http = {
      # load_balancer_arn = aws_lb.nlb_test.arn
      port        = "80"
      protocol    = "TCP"
      action_type = "forward"
      certificate_arn = ""
      alpn_policy     = ""
    },
    https = {
      # load_balancer_arn = aws_lb.nlb_test.arn
      port        = "443"
      protocol    = "TLS"
      action_type = "forward"
      certificate_arn = "arn:aws:acm:us-east-1:b447fa7953be"
      alpn_policy     = "HTTP2Preferred"
    }
  }
}

如果http 监听器字符串alpn_policy = each.value.alpn_policy 应该不存在。如果字符串为空alpn_policy = "",我们将收到错误Error: expected alpn_policy to be one of [HTTP1Only HTTP2Only HTTP2Optional HTTP2Preferred None], got

如果我们设置任何值,我们将收到 ALPN policy cannot be set for non secure listeners 的错误消息

我想要这样的东西。伪代码。

...
If val.alpn_policy != empty then
  certificate_arn = try(each.value.certificate_arn, false)
  alpn_policy = each.value.alpn_policy
  default_action {
else
  certificate_arn = try(each.value.certificate_arn, false)
  default_action {
...
resource "aws_lb_listener" "frontend_http_tcp" {
  for_each          = var.http_tcp_listeners
  load_balancer_arn = aws_lb.main.arn
  port              = each.value.port
  protocol          = each.value.protocol
  certificate_arn = try(each.value.certificate_arn, false)
  alpn_policy = each.value.alpn_policy
  default_action {
    type = each.value.action_type
    target_group_arn = aws_lb_target_group.main[each.key].arn
  }

  depends_on = [
    aws_lb.main,
    aws_lb_target_group.main,
  ]
}

【问题讨论】:

  • 鉴于您的侦听器的名称(“frontend_http_tcp”),我假设您已经检查了 terraform-aws-alb 模块。他们声明了两个变量(https_listeners 和 http_tcp_listeners),然后在模块代码(main.tf)中保持分隔。这对你有用吗?或者你有没有 alpn_policy 的 https 监听器?
  • 我还注意到他们使用了alpn_policy = lookup(var.https_listeners[count.index], "alpn_policy", null)这个表达式……你试过这个吗?抱歉没时间测试自己
  • 那样的话,为什么你在http中还有certificate_arn和alpn_policy呢?为什么不完全删除它们,而不是将它们设置为空字符串?

标签: amazon-ec2 terraform aws-load-balancer


【解决方案1】:

感谢@RafaP 和@Marcin 的想法。最后,代码如下所示。 我删除了VAR 中不需要的变量并使用了try Function alpn_policy= try(each.value.alpn_policy, null)

而不是 alpn_policy = lookup(var.https_listeners[count.index], "alpn_policy", null)

variable "http_tcp_listeners" {
  description = "aws_lb_listener"
  type        = map(any)
  default = {
    http = {
      port        = "80"
      protocol    = "TCP"
      action_type = "forward"
    },
    https = {
      port        = "443"
      protocol    = "TLS"
      action_type = "forward"
      certificate_arn = "arn:aws:acm:us-east-1:714154805721:certificate/c3be"
      alpn_policy     = "HTTP2Preferred"
    }
  }
}
resource "aws_lb_listener" "frontend_http_tcp" {
  for_each          = var.http_tcp_listeners
  load_balancer_arn = aws_lb.main.arn
  port              = each.value.port
  protocol          = each.value.protocol

  certificate_arn = try(each.value.certificate_arn, null)
  alpn_policy     = try(each.value.alpn_policy, null)

  default_action {
    type = each.value.action_type
    target_group_arn = aws_lb_target_group.main[each.key].arn
  }

  depends_on = [
    aws_lb.main,
    aws_lb_target_group.main,
  ]
}

【讨论】:

    猜你喜欢
    • 2014-10-06
    • 2023-01-20
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多