Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform aws_alb_listener_rule with host AND path conditions

AWS ALB supports rules based on matching both host and path conditions in the same rule.

You can also create rules that combine host-based routing and path-based routing.

I've checked the console and the UI does indeed allow for selecting host and path conditions in the same rule.

Terraform aws_alb_listener_rule seems to support host OR path conditions.

Must be one of path-pattern for path based routing or host-header for host based routing.

Emphasis mine

Is there a way to Terraform an ALB rule that only triggers when both the request hostname and path match some criteria?

like image 616
ptomli Avatar asked Sep 19 '17 15:09

ptomli


2 Answers

You can specify two conditions, which results in an AND of the two conditions:

resource "aws_alb_listener_rule" "host_header_rule" {
  condition {
    field  = "host-header"
    values = ["some.host.name"]
  }
  condition {
    field  = "path-pattern"
    values = ["/some-path/*"]
  }
  # etc.
}
like image 147
PaoloC Avatar answered Nov 05 '22 11:11

PaoloC


resource "aws_alb_listener_rule" "listener_path_based_test" {
    listener_arn = "${aws_alb_listener.listener_prod_https_internal_test.arn}"
    action {    
      type             = "forward"    
      target_group_arn = "${aws_alb_target_group.tg_alb_prod_8080_internal_test.arn}"
    }   
    condition {    
     field  = "path-pattern"      
     values = ["/some-path/*"]}} 
like image 44
Mamun Avatar answered Nov 05 '22 11:11

Mamun