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

时间:2018-08-09 13:07:28

标签: terraform elastic-load-balancer

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

即这是elb侦听器中所需的状态:

enter image description here

1 个答案:

答案 0 :(得分:8)

此功能已添加到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 {
    field  = "host-header"
    values = ["my-service.*.terraform.io"]
  }
}