Brad Vogel
Brad Vogel

Reputation: 486

Terraform aws_lb_listener_rule redirect rule: can I rewrite the #{path} variable?

I have the following Terraform rule that attempts to redirect URLs in the format https://engineering.example.com/blog/<path> to https://www.example.com/engineering/<path>

resource "aws_lb_listener_rule" "redirect_engineering_blog_urls_to_website" {
  listener_arn = module.web.alb_listener_arn

  action {
    type = "redirect"

    redirect {
      host        = "www.example.com"
      path        = "/engineering/#{path}"
      port        = "443"
      protocol    = "HTTPS"
      status_code = "HTTP_301"
    }
  }

  condition {
    host_header {
      values = ["engineering.example.com"]
    }
  }


  condition {
    path_pattern {
      values = ["/blog/*"]
    }
  }
}

However, it's not working. When I navigate to https://engineering.example.com/blog/mypost it redirects to https://www.example.com/engineering/blog/mypost (notice /engineering/blog/ when it should just be /engineering/).

Is there a way to tell terraform to substitute ${path} for only the * wildcard capture of ["/blog/*"]?

Upvotes: 0

Views: 2974

Answers (1)

ttacon
ttacon

Reputation: 503

As far as I know, to do full path rewriting based on matching (as in your example above), you'll need to use a proper engine or other technology for that, e.g. nginx, haproxy, or, if you're in AWS, a little lambda behind an API Gateway for this - ALBs don't support path extraction based on patterns unfortunately.

Upvotes: 2

Related Questions