Remove mapped path from AWS API Gateway custom domain mapping
Asked Answered
G

2

5

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?

Guilford answered 19/11, 2021 at 22:13 Comment(0)
M
5

It's totally possible. You just need to use parameters mapping for this. Using the AWS UI it would be:

enter image description here

Maggs answered 14/10, 2022 at 19:49 Comment(0)
H
3

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

Hintze answered 23/11, 2021 at 19:49 Comment(5)
Thanks, but I'd actually be choosing the "VPC Link" option from that dialog, not HTTP, since the traffic is going to a private ALB via the VPC Link. This doesn't allow me to customize the endpoint URL.Guilford
@Guilford did you find a solution in the end? I'm running into the exact same issue where we're routing to a private ALB via the VPC Link.Vegetative
No, I concluded that it couldn't be accomplished within the API Gateway infrastructure and had to update my API code to match the routes that were requested.Guilford
Hi, any update on this ? I have the same issue on my side. It seems to be impossible using HTTP API and private ALB :(Drub
Do we have a way to perform same with Lambda function {proxy+}?Multifoliate

© 2022 - 2024 — McMap. All rights reserved.