question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error resolving path in Api Gateway event handler

See original GitHub issue

Suppose one has two routes to delete resources like the following:

Use Case 1:

Route in handler

@app.delete("/accounts/<account_id>")
def delete_account(account_id: str) -> None:

Request

{
        "path": "/accounts/123",
        "httpMethod": "DELETE",
        "requestContext": {"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"},  # correlation ID
}

Use Case 2:

@app.delete("/accounts/<account_id>/source_networks")
def delete_source_networks(account_id: str) -> Account:

Request

{
        "path": "/accounts/123/source_networks"
        "httpMethod": "DELETE",
        "requestContext": {"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"},  # correlation ID
}

Expected Behavior

Expected behavior is that account_id equals 123 in both use cases

Current Behavior

For use case 1 resolver calls the right function and sets account_id = 123 For use case 2, resolver calls the function of use case 1 and sets accound_id = 123/source_networks

Possible Solution

Not sure if this is actually the expected behavior of proxy integration with its greedy regexp or a bug in Lambda Power Tools

Environment

Power Tools version 1.17.1

delete_source_networks_event = {'body': '{"networks": ["172.217.0.0/16", "8.8.8.8"]}', 'httpMethod': 'DELETE', 'path': **'/accounts/123/source_networks'**, ...}
{"level":"INFO","location":"delete:209","message":"Account deleted","timestamp":"2021-07-11 15:12:42,205-0300","service":"admin_api","cold_start":false,"function_name":"FUNCTION_NAME","function_memory_size":1024,"function_arn":"INVOKED_FUNCTION_ARN","function_request_id":"AWS_REQUEST_ID","correlation_id":"c6af9ac6-7b61-11e6-9a41-93e8deadbeef",**"account_id":"123/source_networks"**,"tax_id":"123","service_id":3}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
heitorlessacommented, Jul 16, 2021

We’re working on some MyPy fixes and as soon as I complete #517 we’ll release it OR the documentation for the new Feature Toggles utility #https://github.com/awslabs/aws-lambda-powertools-python/pull/494

ETA next Friday if all goes well

1reaction
heitorlessacommented, Jul 16, 2021

@michaelbrewer could you look into it as discussed?

@marcioemiranda as a workaround until we fix it, you could change the order of the function definition for the longest/nested being the first one.

For example:

@app.get("/accounts/<account_id>/source_networks")
def account_nested(account_id: str):
    print(f"[ACCOUNT NESTED] Account ID received: --> {account_id}")
    return {"account": f"{account_id}"}

@app.get("/accounts/<account_id>")
def account(account_id: str):
    print(f"[ACCOUNT SINGLE] Account ID received: --> {account_id}")
    return {"account": f"{account_id}"}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix the Most Common API Gateway Request Errors - Dashbird
Solving errors in the cloud isn't always straightforward, and API Gateway isn't an exception ... Handling API Gateway 400 Error: Bad Request.
Read more >
Resolve API Gateway REST API Lambda integration errors
When I invoke my AWS Lambda function using an Amazon API Gateway REST API, I get an "Invalid permissions on Lambda function" error....
Read more >
AWS Lambda - Getting path parameters using Node.js
Short answer: const { id } = event.pathParameters;. I recently released a short training video that demonstrates in detail how to create API...
Read more >
REST API - Lambda Powertools Python - Open Source at AWS
Event handler for Amazon API Gateway REST/HTTP APIs and Application ... where we have two separate functions to resolve two paths: /hello ....
Read more >
Error handling in API Gateway and AWS Lambda - Jayway
The ability to give our clients comprehensive and well defined errors when something goes wrong is just as important as as our “happy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found