Terraform aws_alb_listener_rule with host AND path conditions

Viewed 7106

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?

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.
}
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/*"]}} 
Related