代码之家  ›  专栏  ›  技术社区  ›  LiorH

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

  •  2
  • LiorH  · 技术社区  · 6 年前

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

    i、 e.这是elb侦听器中所需的状态:

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  8
  •   ydaetskcoR    5 年前

    此功能已添加到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"]
      }
    }