Terraform (A)lb 重定向 http -> https

Terraform (A)lb redirect http -> https

如果我做对了,lb_listener 只接受转发作为有效的操作类型。 https://www.terraform.io/docs/providers/aws/r/lb_listener.html 如何配置侦听器以将 HTTP 重定向到 HTTPS?

即这是 elb 侦听器中的理想状态:

此功能已添加到 AWS 提供商和 released with 1.33.0

以下是使用 aws_lb_listener resource:

在负载均衡器侦听器上设置默认操作的方法
resource "aws_lb" "front_end" {
  # ...
}

resource "aws_lb_listener" "front_end" {
  load_balancer_arn = "${aws_lb.front_end.arn}"
  port              = "80"
  protocol          = "HTTP"

  default_action {
    type = "redirect"

    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }
}

您还可以在 aws_lb_listener_rule resource:

中使用单独的负载均衡器侦听器规则添加重定向和固定类型响应
resource "aws_lb_listener_rule" "redirect_http_to_https" {
  listener_arn = "${aws_lb_listener.front_end.arn}"

  action {
    type = "redirect"

    redirect {
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }

  condition {
    host_header {
      values = ["my-service.*.terraform.io"]
    }
  }
}