emmdee
emmdee

Reputation: 1631

Terraform, cannot create ALB Rule with multi value path-pattern rule

As the screenshot shows, AWS ALB Rules allow for multiple match conditions on the path rule:

enter image description here

How can I accomplish this same exact thing in Terraform?

When I try to create this condition in Terraform:

resource "aws_lb_listener_rule" "test" {
  listener_arn = "<<arn_scrubbed>>"
  priority     = 25
  action {
    type             = "forward"
    target_group_arn = "${aws_lb_target_group.mytarget.arn}"
  }

  condition {
    field  = "path-pattern"
    values = ["/account.php", "/client*"]
  }

  condition {
    field  = "host-header"
    values = ["dev01site.example.com"]
  }
}

Terraform will throw the error: attribute supports 1 item maximum, config has 2 declared

If attempted as such:

resource "aws_lb_listener_rule" "test" {
  listener_arn = "<<arn_scrubbed>>"
  priority     = 25
  action {
    type             = "forward"
    target_group_arn = "${aws_lb_target_group.mytarget.arn}"
  }

  condition {
    field  = "path-pattern"
    values = ["/account.php"]
  }
  condition {
    field  = "path-pattern"
    values = ["/client*"]
  }
  condition {
    field  = "host-header"
    values = ["dev01site.example.com"]
  }
}

In this case it throws A rule can only have one 'path-pattern' condition

Is it impossible to perform (with Terraform) these actions that the AWS Console allows, or do I just have my syntax wrong?

NOTE: In case you're wondering - I want to combine rules due to the 100 rule limit per ALB. Trying to get my money's worth using 1 ALB for multiple dev environments using a host/path combination rule. Each require several path rules for service routing. That's why I am consolidating multiple paths per rule. This will save my company a couple hundred bucks a month if I don't have to make 1 rule per path route.

Upvotes: 2

Views: 5716

Answers (1)

Akshata Kenchanagudd
Akshata Kenchanagudd

Reputation: 21

I also faced the similar issue with multiple values for host-header values.

Below link helped me resolved that.

https://github.com/terraform-providers/terraform-provider-aws/issues/12034

Broken config:

condition {
  field  = "host-header"
  values = var.host_names
}

What should have been done:

condition {
  host_header {
    values = var.host_names
  }
}

and declare the host_names variable in variable file with the type as list(string)

variable "host_names" {
  type        = list(string)
  default     = ["abc.com", "xyz.com"]
}

Upvotes: 2

Related Questions