Search code examples
amazon-web-servicesterraformmicroservicesaws-application-load-balancer

Terraform - AWS Application Load Balancer Listener without Default Action


I am creating a micro services architecture in AWS, using an application load balancer to route traffic based on it's listener rules.

However, I am struggling to deal with the default action argument. I want to provide rules that specify path based routing to the target groups of my micro services, thus defining a rule for each micro service based on the request path.

The terraform application load balancer listener requires a default action rule, however, if I am providing rules for all of the valid request paths, then I do not need nor do I want there to be a valid default routing action. Is there a way to return some sort of 'request invalid' response?

Am I approaching this correctly? Or should I change the way I am building my infrastructure.


Solution

  • The terraform application load balancer listener requires a default action rule, however, if I am providing rules for all of the valid request paths, then I do not need nor do I want there to be a default routing action.

    That's not Terraform's fault, that's just how the AWS Application Load Balancer works. The load balancer itself has to know what to do when it gets a request that doesn't match any of those other rules. For example if some bot is scanning your website for vulnerabilities, and trying to hit common URLs, you have to tell AWS how you want it to handle those requests.

    If you consider any requests that don't match your path rules to be invalid, then the appropriate configuration would tell AWS to return an error response for other requests. For example, this configuration would return a 404 error for any request that doesn't match one of your paths:

      default_action {
        type = "fixed-response"
    
        fixed_response {
          content_type = "text/plain"
          message_body = "NOT FOUND"
          status_code  = "404"
        }
      }