Search code examples
amazon-web-servicesapi-gatewayaws-api-gateway

How to configure $default path in AWS API gateway?


We are trying to leverage the $default path in AWS API gateway as per https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-routes.html

configured api gateway like this leveraging the $default as one of the routes

/
 /-default
   ANY
 /api
  /{proxy=}

when we are trying to invoke the api gateway on the $default path and GET call

https://apigateway.amazonaws.com/prod/test

we assumed it will invoke the default path but it didn't

message: "Missing Authentication Token"

but when we do

https://apigateway.amazonaws.com/prod/api/test 

the api integration is invoked

Note : we already tried configuring greedy path{proxy+} instead of $default that does not work as the greedy path always takes precedence and /api routes also get routed to greedy path

Any help from the community in pointing us in the right direction would be of great help


Solution

  • It seems like you have not set up your API Gateway HTTP API routes correctly causing the routing to not work as expected. Would also like to mention that HTTP APIs and REST APIs are different types of API Gateway APIs, so do confirm that you have configured your API correctly.

    Coming to how the routing would work, as a sample, here's how the routes for the API look:

    HTTP API routes

    1. Request to GET https://xxxx.execute-api.xxxx.amazonaws.com/prod/test : Routed to $default path

    2. Request to GET https://xxxx.execute-api.xxxx.amazonaws.com/prod/api/test : Routed to /api/{proxy+} path


    Further, if you have a greedy path at ANY /{proxy+}, then as you mentioned, this greedy path will take priority over the $default route. However, this would not take precedence over the ANY /api route if the request matches to the route, for example: GET https://xxxx.execute-api.xxxx.amazonaws.com/prod/api : would be routed to /api path and not /{proxy+}

    The routing priority is also explained here

    After selecting a stage, API Gateway selects a route. API Gateway selects the route with the most-specific match, using the following priorities:

    1. Full match for a route and method.
    2. Match for a route and method with a greedy path variable ({proxy+}).
    3. The $default route.

    If no routes match a request, API Gateway returns {"message":"Not Found"} to the client.

    EDIT:

    To create the $default route, just specify the path as $default when creating the route

    Create $default route