我使用部署為容器映像的 AWS SAM CLI 創建了一個 Lambda 函式。問題是每次我對代碼(app.py)進行小改動并運行sam build時都會下載要求。原因可以從下面的Dockerfile中理解。
Dockerfile
FROM public.ecr.aws/lambda/python:3.8
COPY app.py requirements.txt ./
COPY models /opt/ml/models
RUN python3.8 -m pip install -r requirements.txt -t .
CMD ["app.lambda_handler"]
每次我運行sam build時它都會下載要求。
我還在 github 上遇到了一個使用--cached選項的執行緒,但如果我們使用容器映像,它就不起作用。 https://github.com/aws/aws-sam-cli/issues/805
模板.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Timeout: 50
MemorySize: 5000
Resources:
InferenceFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
PackageType: Image
Architectures:
- x86_64
Metadata:
Dockerfile: Dockerfile
DockerContext: ./app
DockerTag: python3.8-v1
依賴項是超過 200MB 的 tensorflow 2.8.0,我無法更改為任何其他選項,例如 tensorflow-lite。
uj5u.com熱心網友回復:
使用 docker 快取的作業方式,您的COPY陳述句之后的所有內容都在快取中無效(假設正在更改)。依賴項通常保留在快取中的方式是僅添加安裝依賴項所需的內容,安裝它們,然后僅在安裝依賴項后添加服務代碼。在下面的示例中,pip install如果 requirements.txt 更改,則只會運行多次。
FROM public.ecr.aws/lambda/python:3.8
COPY requirements.txt ./
RUN python3.8 -m pip install -r requirements.txt -t .
COPY app.py ./
COPY models /opt/ml/models
CMD ["app.lambda_handler"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450563.html
標籤:亚马逊网络服务 码头工人 aws-lambda dockerfile aws-山姆
