【问题标题】:Terraform: If-else not working with alb_lb_listener resourceTerraform:If-else 不适用于 alb_lb_listener 资源
【发布时间】:2019-02-01 14:54:47
【问题描述】:

我正在尝试为 if-else 条件创建资源 aws_lb_listener 并使用 terraform 插值。但它告诉我基础设施没有变化。但是,它尚未在基础架构上创建 https 侦听器。下面的代码有什么遗漏吗?

alb.tf

resource "aws_lb_listener" "https" {
  count = "${var.https_listener_enable == true ? 1 : 0}"
  load_balancer_arn = "${aws_lb.main.arn}"
  port              = "443"
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-2016-08"
  certificate_arn   = "arn:aws:iam::187416307283:server-certificate/test_cert_rab3wuqwgja25ct3n4jdj2tzu4"

  default_action {
    type = "fixed-response"

  fixed_response {
      content_type = "text/plain"
      message_body = "Nothing is here. Go Away."
      status_code  = "200"
    }
}
}

变量.tf

variable "https_listener_enable" {}

main.tf

module "public_alb" {
  source             = "../modules/alb"
  load_balancer_name = "example-production"
  https_listener_enable = true
  security_groups            = ["${module.security_group.sg_http}"]
  load_balancer_is_internal  = false
  idle_timeout               = 60
  enable_deletion_protection = false
  enable_http2               = true
  tags                       = "${map("Environment", "production",
                "Name", "example-production",)}"
  subnets = "${module.vpc.public_subnets}"

  vpc_id = "${module.vpc.vpc_id}"
}

【问题讨论】:

  • 您的三元组要求将https_listener_enable 设置为true,因此您可能不会在任何地方这样做。至少您没有在任何显示的代码中执行此操作。
  • 在 main.tf 中,我将该变量作为 true 传递。

标签: terraform


【解决方案1】:

将 count = "${var.https_listener_enable == true ? 1 : 0}" 替换为 count = "${var.https_listener_enable == "true" ? 1 : 0}" 。如果您已经在 .tfvars 文件中定义了变量“https_listener_enable”值或从命令行传递,这应该可以工作。

【讨论】:

  • 我还没有测试过这个解决方案。但这似乎应该可行。
【解决方案2】:

将计数值更改为此 "${var.https_listener_enable ? 1 : 0}" 对我有用。

【讨论】:

    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2011-11-19
    • 2019-08-22
    • 2017-07-09
    • 1970-01-01
    • 2019-11-05
    相关资源
    最近更新 更多