Skip to content

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

Merged
leandrodamascena merged 2 commits into
aws-powertools:developfrom
dropps07:fix/8802-asgi-request-context
Nov 25, 2026
Merged

leandrodamascena merged 2 commits into
aws-powertools:developfrom
dropps07:fix/8802-asgi-request-context

Conversation

@dropps07

Copy link
Copy Markdown
Contributor

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
@dropps07

Copy link
Copy Markdown
Contributor 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.

@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 20, 2026
@dropps07

dropps07 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @leandrodamascena @hjgraca , this PR is still waiting for its first review. It fills the ASGI request context from the scope in HttpResolverLocal and is mergeable with no conflicts. Could you please take a look when you get a chance. Thanks.

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dropps07! Filling only what the ASGI scope can actually tell us, and leaving the Lambda-specific fields alone, is the scope I had in mind. I ran it under uvicorn and a handler reading source_ip and user_agent goes from a 500 on develop to the right values here. Goes out in 3.42.0, no need to rebase, you'll be in the release notes. @dacevedo12 this should cover what your handlers read; shout if something is missing.

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.14%. Comparing base (dbe1662) to head (fa76833).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
...s_lambda_powertools/event_handler/http_resolver.py 93.75% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8841      +/-   ##
===========================================
- Coverage    97.15%   97.14%   -0.01%     
===========================================
  Files          327      327              
  Lines        16813    16828      +15     
  Branches      1539     1539              
===========================================
+ Hits         16334    16348      +14     
- Misses         336      337       +1     
  Partials       143      143              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit 5cd5491 into aws-powertools:develop Nov 25, 2026
13 of 14 checks passed
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

2 participants