FastAPI responds with an HTTP 307 Temporary Redirect due to a missing or additional trailing slash (depending on the route). When this happens, it uses the Host header.
When using CloudFront instead of API Gateway's custom domains, the Host header is changed to [apiId].execute-api.[region].amazonaws.com and the original host is stored in an additional header x-original-host.
Combined, this causes requests to be redirected to an execute-api url instead of the intended host.
Related issues on FastAPI:
Workaround
As a temporary workaround, consider adding a wrapper around Mangum's handler
mangumHandler = Mangum(app, lifespan="off")
def handler(event: LambdaEvent, context: LambdaContext):
if event["multiValueHeaders"]["x-original-host"]:
event["multiValueHeaders"]["Host"] = event["multiValueHeaders"]["x-original-host"]
response = mangumHandler(event, context)
return response
FastAPI responds with an HTTP 307 Temporary Redirect due to a missing or additional trailing slash (depending on the route). When this happens, it uses the
Hostheader.When using CloudFront instead of API Gateway's custom domains, the
Hostheader is changed to[apiId].execute-api.[region].amazonaws.comand the original host is stored in an additional headerx-original-host.Combined, this causes requests to be redirected to an
execute-apiurl instead of the intended host.Related issues on FastAPI:
Workaround
As a temporary workaround, consider adding a wrapper around Mangum's handler