Skip to content

fix(event_handler): fill request context from ASGI scope in HttpResolverLocal - #8841

Open
dropps07 wants to merge 1 commit into
aws-powertools:developfrom
dropps07:fix/8802-asgi-request-context
Open

dropps07 wants to merge 1 commit into
aws-powertools:developfrom
dropps07:fix/8802-asgi-request-context

Conversation

@dropps07

Copy link
Copy Markdown

Issue number: closes #8802

Summary

Changes

Fill request_context from the ASGI scope so handlers running under uvicorn read the same fields they read in Lambda. Fixes #8802.

from_asgi fills the source IP from the scope client address and the user agent from the user-agent header, and exposes it through a request_context property on HttpProxyEvent. Lambda-specific fields stay as they are today.

The v2.0 local event shape option from the issue is not included. Leandro's scope for this issue is the request_context fill only. Happy to follow up on the event shape option separately if he wants it.

User experience

Before this change, handlers running under uvicorn that read the source IP or user agent from the request context raised AttributeError, because the request context was not filled from the ASGI scope. After this change, those handlers read the same fields they read in Lambda, with the source IP taken from the ASGI scope client address and the user agent taken from the user-agent header.

Verification

Added 2 new tests through HttpResolverLocal with a realistic ASGI scope. All 69 HTTP resolver tests pass. Negative control: both new tests fail on unfixed code with AttributeError, the exact bug from the issue. Ruff format check, ruff check and mypy are clean on the changed file. Not verified: no live uvicorn run and no full make pr run.

Acknowledgement: This work was AI-assisted. I verified it myself by running the repo's pytest suite for the HTTP resolver, plus ruff and mypy on the changed file. I did not run a live uvicorn server or the full make pr pipeline.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

…verLocal

HttpProxyEvent.from_asgi only copied method, path, headers, body and query string, so the request context fell back to a stub with only stage, requestId and http method/path, and HttpProxyEvent had no request_context property. Handlers reading source_ip or user_agent from the request context raised AttributeError under uvicorn.

Fill the request context from the ASGI scope: the source IP from scope client and the user agent from the user-agent header, and expose it through a request_context property on the local event.

Refs aws-powertools#8802

Signed-off-by: Ajey Awasthi <115537579+dropps07@users.noreply.github.com>
@dropps07
dropps07 requested a review from a team as a code owner November 16, 2026 08:00
@dropps07
dropps07 requested a review from hjgraca November 16, 2026 08:00
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 16, 2026
@sonarqubecloud

Copy link
Copy Markdown

@dropps07

Copy link
Copy Markdown
Author

Hi @leandrodamascena, the PR is ready for review whenever you have a moment. Please let me know if it needs anything added, removed or changed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event_handlers size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: fill the request context from the ASGI scope in HttpResolverLocal

1 participant