我正在尝试使用 MacOS 中的 SAM CLI 工具部署 AWS Lambda 函数,而不是使用 Docker 容器。
- SAM CLI 版本 0.4.0
- Lambda 函数的 Python 3.8 运行时
- MacOS 上本地安装的 Python 3.7
- 我有一个
requirements.txt
文件,与我的 Lambda 函数文件位于同一目录中
要求.txt
boto3
botostubs
部署脚本 (PowerShell)
sam build --template-file $InputTemplate
sam package --region $AWSRegion --template-file $InputTemplate --profile $ProfileName --s3-bucket $BucketName --output-template-file $OutputTemplate
sam deploy --region $AWSRegion --profile $ProfileName --template-file $OutputTemplate --stack-name $StackName --capabilities CAPABILITY_NAMED_IAM
实际行为
SAM CLI 忽略我的requirements.txt
文件,并且仅部署我的源代码。当我测试我的函数时,这会导致以下错误。
{
"errorMessage": "Unable to import module 'xxxxxxxxxxxxxx': No module named 'botostubs'",
"errorType": "Runtime.ImportModuleError"
}
预期行为
SAM CLI 将声明的 Python 依赖项打包在requirements.txt
,以及我的源代码。
Question:如何确保 SAM CLI 下载并打包我的 Python 依赖项以及源代码?据我所知,我遵循了文档。
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build.html https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build.html
刚刚弄清楚,阅读有关sam build
命令更深入一点。我没有意识到它正在创建一个名为的子文件夹.aws-sam/build/
并将修改后的模板存储在那里。
我更新了我的命令和路径,现在工作得很好。
$InputTemplate = "$PSScriptRoot/cloudformation.yml"
$BuiltTemplate = "$PSScriptRoot/.aws-sam/build/template.yaml"
$BucketName = 'xxxxxxx'
$AWSRegion = 'xxxxxx'
$StackName = 'xxxxxx'
# Package and deploy the application
sam build --template-file $InputTemplate
sam package --region $AWSRegion --template-file $BuiltTemplate --profile $ProfileName --s3-bucket $BucketName
sam deploy --region $AWSRegion --profile $ProfileName --template-file $BuiltTemplate --stack-name $StackName --capabilities CAPABILITY_NAMED_IAM --s3-bucket $BucketName
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)