[英]AWS SAM Lambda - Unable to import module 'main': No module named 'requests', 'msal' . pipeline using Azure CI/CD
I created a python project using AWS lambda using SAM template.我使用 SAM 模板使用 AWS lambda 创建了一个 python 项目。 Created virtual environment and installed all required modules.
创建虚拟环境并安装所有必需的模块。 for example, requests, msal etc. module names mentioned in requirements.txt file and installed.
例如,requests、msal 等。 requirements.txt 文件中提及并安装的模块名称。 I am able to build, debug and deploy using sam command from my vscode.
我能够使用我的 vscode 中的 sam 命令进行构建、调试和部署。 for example.
例如。 sam build, sam local invoke, sam deploy --guided command.
sam 构建、sam 本地调用、sam 部署 --guided 命令。
As a project repo uses Azure devOps.作为项目回购使用 Azure devOps。 Now, I created project pipeline and deployed to AWS successfully.
现在,我创建了项目管道并成功部署到 AWS。
part of pipeline script -(install dependencies)管道脚本的一部分-(安装依赖项)
When I tested lambda function from AWS console or call url, It show the following error as per cloud watch log.当我从 AWS 控制台测试 lambda function 或调用 url 时,根据云监视日志显示以下错误。 It shows module not found "request" and "msal".
它显示模块未找到“请求”和“msal”。 for example.
例如。 ERROR: Function Logs [ERROR] Runtime.ImportModuleError: Unable to import module 'main': No module named 'requests'
错误:Function 日志 [错误] Runtime.ImportModuleError:无法导入模块“main”:没有名为“requests”的模块
python 3.9 python 3.9
AWS SAM AWS SAM
Azure devOps repo Azure 开发运营回购
VScode VScode
I am unable to figure out the issue.我无法弄清楚这个问题。 I spent time but did not find the cause.
我花了时间但没有找到原因。 I am stuck.
我卡住了。 Please give me hints/ideas/solution.
请给我提示/想法/解决方案。
Lambda dependencies have to be installed at the root folder. Lambda 必须在根文件夹中安装依赖项。 See the "Deployment package with dependencies" Section, Step 5 in this guide .
请参阅本指南中的“使用依赖项部署 package”部分的第 5 步。
Create a deployment package with the installed library at the root.
在根目录下使用已安装的库创建部署 package。
You can do so by going to the root directory, then issue a pip install
with a -t
(target) flag and specifying the current directory with a dot .
为此,您可以转到根目录,然后使用
-t
(目标)标志发出pip install
并使用点指定当前目录.
. .
pip install -r src/requirements.txt -t.
This is the following answer.这是下面的答案。 It might help someone.
它可能会帮助某人。
- task: AWSShellScript@1
displayName: 'Build'
inputs:
awsCredentials: AwsServiceConnection
regionName: 'us-east-1'
scriptType: 'inline'
inlineScript: |
sam build --debug \
--template-file template.yaml
- task: AWSShellScript@1
displayName: 'Package'
inputs:
awsCredentials: AwsServiceConnection
regionName: us-east-1
scriptType: 'inline'
inlineScript: |
sam package --resolve-s3 --output-template-file packaged.yaml
- task: AWSShellScript@1
displayName: 'Deploy Infrastructure'
inputs:
awsCredentials: AwsServiceConnection
regionName: 'us-east-1'
scriptType: "inline"
inlineScript: |
sam deploy \
--template-file packaged.yaml \
--no-confirm-changeset \
--no-fail-on-empty-changeset \
--capabilities CAPABILITY_IAM \
--stack-name sam-test-stack \
--resolve-s3 \
--s3-prefix sam-test-stack
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.