Hey guys,
I am trying to build d coker iamge to deploy on aws lambda following the doc and:
https://docs.aws.amazon.com/lambda/latest/dg/images-create.html#images-create-from-base
https://docs.aws.amazon.com/lambda/latest/dg/python-image.html#python-image-base
My Dockerfile:
FROM public.ecr.aws/lambda/python:3.8
# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}
# Install the function's dependencies using file requirements.txt
# from your project folder.
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ]
requirement.txt:
fastapi == 0.71.*
uvicorn[standard]
mangum == 0.12
app.py:
from fastapi import FastAPI
from mangum import Mangum
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
handler = Mangum(app, spec_version=2)
then:
docker build -t hello-world .
docker run -p 9000:8080 hello-world
But when I try to query locally (following the doc):
$ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
{
"errorMessage": "Unable to determine handler from trigger event",
"errorType": "TypeError",
"stackTrace": [
" File \"/var/task/mangum/adapter.py\", line 86, in __call__\n handler = AbstractHandler.from_trigger(\n",
" File \"/var/task/mangum/handlers/abstract_handler.py\", line 113, in from_trigger\n raise TypeError(\"Unable to determine handler from trigger event\")\n"
]
}
I tried using (found in some discussions)
handler = Mangum(app, spec_version=2)
and even from another discussion:
import uvicorn
from fastapi import FastAPI
from mangum import Mangum
# TODO: Add this line
from starlette.middleware.cors import CORSMiddleware
app = FastAPI(title="FastAPI Mangum Example", version='1.0.0')
# TODO: Add these lines
app.add_middleware(
CORSMiddleware,
allow_origins='*',
allow_credentials=False,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["x-apigateway-header", "Content-Type", "X-Amz-Date"],
)
handler = Mangum(app)
@app.get('/', name='Hello World', tags=['Hello'])
def hello_world():
return {"Hello": "Python"}
if __name__ == '__main__':
uvicorn.run(app)
it never works. (same error when deployed to aws lamda and not run locally)
I tried to use previous version but also had issue...
does anyone has a working example for this ?
Hey guys,
I am trying to build d coker iamge to deploy on aws lambda following the doc and:
https://docs.aws.amazon.com/lambda/latest/dg/images-create.html#images-create-from-base
https://docs.aws.amazon.com/lambda/latest/dg/python-image.html#python-image-base
My Dockerfile:
requirement.txt:
app.py:
then:
But when I try to query locally (following the doc):
I tried using (found in some discussions)
and even from another discussion:
it never works. (same error when deployed to aws lamda and not run locally)
I tried to use previous version but also had issue...
does anyone has a working example for this ?