[英]Which port to expose on Docker for AWS Lambda?
I am trying to host a very simple (Hello World) FastAPI on AWS Lambda using Docker image.我正在尝试使用 Docker 图像在 AWS Lambda 上托管一个非常简单的 (Hello World) FastAPI。 The image is working fine locally but when I am running it on Lambda it shows me the port binding error.该图像在本地运行良好,但当我在 Lambda 上运行它时,它显示端口绑定错误。 Below are the error details that I am getting when I am trying to test the Lambda function with this image.以下是我尝试使用此图像测试 Lambda function 时收到的错误详细信息。
START RequestId: ae27e3b1-596d-41f3-a153-51cb9facc7a7 Version: $LATEST
INFO: Started server process [8]
INFO: Waiting for application startup.
INFO: Application startup complete.
ERROR: [Errno 13] error while attempting to bind on address ('0.0.0.0', 80): permission denied
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
END RequestId: ae27e3b1-596d-41f3-a153-51cb9facc7a7
REPORT RequestId: ae27e3b1-596d-41f3-a153-51cb9facc7a7 Duration: 3034.14 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 20 MB
2021-11-01T00:23:59.807Z ae27e3b1-596d-41f3-a153-51cb9facc7a7 Task timed out after 3.03 seconds
This says that I cant bind port 80 on 0.0.0.0, so any idea what port and host should I use in the Dockerfile to make it work on AWS Lambda?这表示我无法在 0.0.0.0 上绑定端口 80,所以知道我应该在 Dockerfile 中使用哪个端口和主机才能使其在 AWS Lambda 上运行吗? Thanks (Below is the Dockerfile which I am using)谢谢(下面是我用的Dockerfile)
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install -r /code/requirements.txt
COPY . /code
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
When running FastAPI in AWS Lambda (assuming used with AWS API Gateway , which you need for Lambda to receive HTTP requests) you can't run it using uvicorn and bind to a port as you would normally.在 AWS Lambda 中运行 FastAPI 时(假设与AWS API 网关一起使用,Lambda 需要它来接收 HTTP 请求),您不能像往常一样使用 uvicorn 运行它并绑定到端口。
Instead you need to use Mangum which will create the Lambda handler and transform any incoming Lambda event and send it to FastAPI as a Request object, and in my experience it all works pretty well.相反,您需要使用Mangum ,它将创建 Lambda 处理程序并转换任何传入的 Lambda 事件并将其作为请求 object 发送到 FastAPI,根据我的经验,这一切都运行良好。
So your code to create the handler might look like this:因此,您创建处理程序的代码可能如下所示:
if __name__ == "__main__":
uvicorn.run("myapp:app")
else:
handler = Mangum(app)
Additionally your Dockerfile would have an entry point something like this:此外,您的Dockerfile将有一个类似这样的入口点:
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "myapp.handler" ]
Where awslambdaric is the python module provided by AWS to run Docker containers in AWS Lambda as describedhere .其中awslambdaric是 AWS 提供的 python 模块,用于在 AWS Lambda 中运行 Docker 容器,如此处所述。
Also note that API Gateway resource needs a method configured using the Lambda Proxy Integration.另请注意,API 网关资源需要使用 Lambda 代理集成配置的方法。
I haven't tested any of the above its just an idea of how to get going.我还没有测试过上面的任何一个,它只是一个如何开始的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.