2

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?

Devin
  • 851
  • 12
  • 32

2 Answers2

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

Azize
  • 4,006
  • 2
  • 22
  • 38
  • 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. – Devin Nov 29 '21 at 15:27
  • @Devin 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. – Dennis Ameling Mar 02 '22 at 17:08
  • 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. – Devin Mar 03 '22 at 18:16
  • Hi, any update on this ? I have the same issue on my side. It seems to be impossible using HTTP API and private ALB :( – Spawnrider Apr 06 '22 at 13:07
  • Do we have a way to perform same with Lambda function {proxy+}? – Hardik Uchdadiya May 27 '22 at 15:19
3

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

enter image description here

Nicolas
  • 1,193
  • 1
  • 10
  • 25