aws-sam-cli Python requirements.txt 在自定义 template.yaml 时从已部署的 Lambda 中丢失

aws-sam-cli Python requirements.txt missing from deployed Lambda when custom template.yaml

同一个项目我有多个AWS SAM模板,其中lambdas使用Python 3.8,部分用于不同的环境,部分用于尝试新事物,我使用-t my-template.yaml来指定使用哪个模板。

我使用常规 sam build -t my-template.yaml && sam deploy -t my-template.yaml 构建。

问题:requirements.txt 文件似乎在构建步骤中受到尊重,但在部署步骤中似乎被忽略了。

.aws-sam/build/MyFunction包含要求:

$ ls
app.py   
certifi-2020.12.5.dist-info  
chardet-4.0.0.dist-info  
idna-2.10.dist-info  
pymysql                  
rds_config.py  
requests-2.25.1.dist-info  
urllib3
certifi  
chardet                      
idna                     
__init__.py          
PyMySQL-1.0.2.dist-info  
requests       
requirements.txt           
urllib3-1.26.3.dist-info

但是当运行在云中时,lambda失败并异常提示找不到pymysql,而当我在AWSUI中检查函数时,没有包含要求(例如,没有 PyMySQL-1.0.2.dist-info).

我注意到,如果我不在 sam deploy 上使用 -t 标志,而只是在那里使用默认的 template.yaml 文件,那么似乎正确地包含了要求,并且lambda 运行成功。

我可能做错了什么?

requirements.txt内容:

$ cat requirements.txt
requests
pymysql

SAM 版本:

$ sam --version
SAM CLI, version 1.18.1

OS:

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
....

解决方案是使用:

sam build -t my-template.yaml

然后做:

sam deploy

无需再次添加 -t my-template.yaml

下面工单的答案:

您正在做的是告诉 sam deploy 使用指向非内置函数的“源模板”。相反,如果您从 sam deploy 命令中删除 -t,SAM CLI 将从 .aws-sam/build/template.yaml 中获取构建的模板,它将“安装”所有依赖项。

https://github.com/aws/serverless-application-model/issues/1927