41
loading...
This website collects cookies to deliver better user experience
from typing import List, Optional
from fastapi import FastAPI
from fastapi.param_functions import Query
from fastapi.responses import PlainTextResponse
from mangum import Mangum
app = FastAPI()
@app.get("/math/add", response_class=PlainTextResponse)
async def addition(i: Optional[List[int]] = Query([])):
return str(sum(i))
@app.get("/")
async def health():
return {"status": "OK"}
handler = Mangum(app)
FROM public.ecr.aws/lambda/python:3.8
RUN pip install fastapi "uvicorn[standard]" mangum
ADD main.py ${LAMBDA_TASK_ROOT}
CMD ["main.handler"]
[“uvicorn”]
. This sets the container to run the Uvicorn Python web server on startup. The CMD is overridden with ["main:app", "--host", "0.0.0.0", "--port", "80"]
, which defines the options for the web server to load the application and which ports to use. math_task = ecs.FargateTaskDefinition(
self,
"MathTask",
memory_limit_mib=1024,
cpu=512,
)
math_task.add_container(
"MathServiceContainer",
image=ecs.ContainerImage.from_ecr_repository(
image.repository, image.asset_hash
),
memory_limit_mib=1024,
cpu=512,
port_mappings=[{"containerPort": 80}],
entry_point=["uvicorn"],
command=["main:app", "--host", "0.0.0.0", "--port", "80"],
)
% http "http://xxxxx.ap-southeast-2.elb.amazonaws.com/math/add?i=5&i=10"
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 2
Content-Type: text/plain; charset=utf-8
Date: Fri, 22 Oct 2021 04:24:35 GMT
server: uvicorn
15
% http "https://xxxxx.execute-api.ap-southeast-2.amazonaws.com/math/add?i=4&i=6"
HTTP/1.1 200 OK
Apigw-Requestid: Hl6YIiA0ywMEPTA=
Connection: keep-alive
Content-Length: 2
Content-Type: text/plain; charset=utf-8
Date: Fri, 22 Oct 2021 04:28:01 GMT
10