Skip to content

Feature request: give ApiGatewayRestResolver middleware access to resolved route and route args #7992

@RynoM

Description

@RynoM

Use case

We are using a few "lambdaliths" behind API Gateway (up to 10s of routes each, behind seperate base resources). We came from single lambdas for every api gateway endpoint and then moved to this approach. However we had not gotten rid of all the individual api gateway methods and integrations. We are looking to replace all of them with /<base_resource>/{proxy+} with ANY method. This all seemed to work fine, untill we ran into an issue with the authorizer middleware we created.

We created some ACLs on a resource/endpoint level, for which the simplest example looks like:

/applications/{application_id}:
  PUT: [admin]

Where /applications is the base resource and one lambda handles all the routes behind this. To check which route is being called, our middleware accessed:

app.current_event.resource

For which the value would be /applications/{application_id} if that is the route being called.
However when moving to /applications/{proxy+}, we no longer have access to this information. It looks like there is no simple/standard way to get the information we'd need for our authorizaion from the app.current_event . Besides the actual route, we also need the path parameters, since in this case we use the application_id for authorization. In the app.current_event these are now:

{'proxy': '4da715ee-79d4-4e52-81cb-1ecc464708fb'}

Instead of what we used to have:

{'application_id': '4da715ee-79d4-4e52-81cb-1ecc464708fb'}

However we know the information is available (powertools needs to route the request to the correct handler after the middleware after all), and can see this from the app.context:

{'_route': <aws_lambda_powertools.event_handler.api_gateway.Route object at 0xffffb3cef410>, '_path': '/applications/4da715ee-79d4-4e52-81cb-1ecc464708fb', '_route_args': {'application_id': '4da715ee-79d4-4e52-81cb-1ecc464708fb'}}

And the Route object in the _route key also contains exactly the endpoint we'd need:

{'method': 'GET', 'path': '/applications/<application_id>', 'openapi_path': '/applications/{application_id}', ... }

Solution/User Experience

We are unsure how stable the app.context usage would be, since they are "private". So I think ideally we would just like those to be public. Either stable in the app.context or in the app.current_event.

Alternative solutions

Another alternative would be use (parts of) the ApiGatewayResolver._resolve method, such that we can match the app.current_event.path to a route ourselves, but this seems much more involved and roundabout.

Related issue I found: https://github.com/aws-powertools/powertools-lambda-python/issues/4609

Acknowledgment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestfeature requesttriagePending triage from maintainers

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions