4

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

user2359997
  • 561
  • 1
  • 16
  • 40
  • I'm not sure, I think order matters. Shouldn't `default` be last? – Marcin May 30 '20 at 22:18
  • @Marcin i don't think order matters even though i created default after /api ..it gets added before – user2359997 May 31 '20 at 01:40
  • 2
    @user2359997 It doesn't seem like you have created the `$default` path correctly. When creating the route for the HTTP API you have to specify `$default` as the `path` (the method can't be changed for this case). I tested this out and it works exactly as documented [here](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-routes.html#http-api-develop-routes.evaluation). Could you confirm? – Paradigm Jun 02 '20 at 08:47

1 Answers1

14

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

Paradigm
  • 1,876
  • 1
  • 12
  • 16
  • thanks for the answer could you please let us know how we could configure the default path ? if possible with a screen shot .... – user2359997 Jun 02 '20 at 19:02
  • @user2359997 updated my answer to include that, pretty easy, nothing much to it really. – Paradigm Jun 03 '20 at 03:26
  • Important distinction to make is tha tthe `$default` path seems to only work for HTTP APIs, not REST APIs! – bombillazo Apr 28 '22 at 12:08