Remove mapped path from AWS API Gateway custom domain mapping

Viewed 764

I have a custom domain set up in AWS API Gateway. My intention is to use "API mappings" to send traffic for different API versions to their respective API Gateways, e.g.:

  • GET https://example.com/v1/foo is sent to an API gateway "APIv1" ($default stage) via an API mapping on the custom domain with path="v1".
  • GET https://example.com/v2/foo is sent to an API gateway "APIv2" ($default stage) via an API mapping on the custom domain with path="v2" (not shown)

enter image description here

The HTTP APIs themselves are configured with a single route /{proxy+} and an integration that sends requests to a private ALB:

enter image description here

enter image description here

enter image description here

This setup works fine as far as routing traffic goes, but the problem is that when the request makes it to the actual application, the routes the application receives are like /v1/foo instead of just /foo, which is what the app is expecting.

I've played around with different route matching and parameter mapping (of which I can find almost no examples for my use case) to no avail.

I could change my app code to match the routes that AWS is sending, but the entire point of this was to handle versioning using my AWS stack and not app code. Do I have another option?

1 Answers

If you create a resource called /foo and the proxy resource inside it, when you set integration you can define which path to pass and the {proxy} will have just the part after /foo, ignoring the v1 entirely.

See an example below.
In this case it is ignoring everything before v1 and it is also rewriting the integration to /api/{proxy}.

It will receive a request as GET https://example.com/abc/xyz/v1/foo and will forward to backend as GET https://example.com/api/foo.

API Gateway demo

Update

It can't be done via VPC Link, but we can use public ALB almost like private, like the explanation below. It explain about CloudFront, but the same is valid for API Gateway.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/restrict-access-to-load-balancer.html

Related