HttpResolverLocal is intended for local development and testing only.
The API may change in future releases. Do not use in production environments.
When developing locally, you can use HttpResolverLocal to run your API with any ASGI server like uvicorn. It implements the ASGI specification, is lightweight with no external dependencies, and the same code works on any compute platform, including Lambda.
If your Lambda is behind Lambda Web Adapter or any other HTTP proxy that speaks the HTTP protocol, it works seamlessly.
All existing resolver features work out of the box: routing, middleware, validation, OpenAPI/Swagger, CORS, exception handling, and more.
Install uvicorn:
1
pipinstalluvicorn
1 2 3 4 5 6 7 8 9101112
fromaws_lambda_powertools.event_handlerimportHttpResolverLocalapp=HttpResolverLocal()@app.get("/hello/<name>")defhello(name:str):return{"message":f"Hello, {name}!"}# Lambda handler - same code works in Lambdahandler=app
fromaws_lambda_powertools.event_handlerimportHttpResolverLocal,Responseapp=HttpResolverLocal()classNotFoundError(Exception):def__init__(self,resource:str):self.resource=resource@app.exception_handler(NotFoundError)defhandle_not_found_error(exc:NotFoundError):returnResponse(status_code=404,content_type="application/json",body={"error":"Not Found","resource":exc.resource},)@app.not_founddefhandle_not_found(exc:Exception):returnResponse(status_code=404,content_type="application/json",body={"error":"Route not found","path":app.current_event.path},)@app.get("/users/<user_id>")defget_user(user_id:str):ifuser_id=="0":raiseNotFoundError(f"User {user_id}")return{"user_id":user_id}handler=app