'AWS API Gateway path based routing to private integrations

I am using AWS HTTP API Gateway to route requests to my integrations in the VPC.

I've added a custom domain and I want to route my requests to my integrations based on the paths in the following manner

Path based routing

Basically all the requests coming to the API gateway should be routed to different integrations based on the base paths but the integration should receive only the path after the base path. So all the requests coming to my.custom.domain/foo/<path1>/<path2>/<path3> should be routed to the integration Foo but the gateway should strip the base path i.e. /foo and forward the rest to the integration.

The functionality is same as below in nginx where nginx strips the foo from the request path and forwards the rest to the service

location /foo/ {
  proxy_pass http://foo.service
}

I've tried adding custom domain and API mapping in the AWS API gateway but that doesn't work. My service still receives the whole path from the request and hence fails. I am unable to find any documentation or mentions on the internet about this.



Solution 1:[1]

You can use routes and parameter mappings to achieve this.

Create 2 routes with 2 path mappings:

  1. path: "/foo/{proxy+}" with parameter mapping: "/$request.path.proxy"

  2. path: "/bar/{proxy+}" with parameter mapping: "/$request.path.proxy"

"proxy+" is a greedy path variable, so it will contain the path after /foo/ or /bar/.

You can use this variable with a parameter mapping to overwrite the path the backend will receive.

Solution 2:[2]

This is how it looks like in AWS CDK

const privateIntegration = new HttpServiceDiscoveryIntegration('test', service, {
      parameterMapping: new ParameterMapping()
        .overwritePath(MappingValue.custom('/$request.path.proxy'))
    })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Druska
Solution 2 Bogdan Nazarenko