我想分析我使用 CodeStar 创建的 lambda 项目,因为它在运行时显示:
Runtime.ImportModuleError:无法导入模块
重要的是要知道:
- 它适用于 sam local
- 在 AWS 上的测试适用于 requirements.txt 中的所有依赖项
- 它在最后几天工作没有问题导入一些额外的模块
我在构建日志中看到它安装了所有需要的需求/依赖项并成功运行测试。
但是在运行时它错过了一个依赖!
据我了解,部署的包应该包含所有需要的文件在一个 zip 中。但是,如果我从 S3 下载 zip,则没有依赖项(额外的模块)——只有我的源代码。给定的 template.yml 没有显示任何关于 zip 文件的内容。
我在 Lambda 层、ECS 和 EC2 中进行了搜索,但什么也没有。
这个 CodeStar 创建的管道是如何工作的?运行时所有依赖项在哪里以及如何检查这些依赖项?
这是我的 buildspec.yml(因为 codestar 创建了它。我刚刚添加了 requiremens.txt 部分)
version: 0.2
phases:
install:
runtime-versions:
python: 3.9
commands:
# Upgrade AWS CLI to the latest version
- pip install --upgrade awscli
- pip install -I -r requirements.txt
pre_build:
commands:
# Discover and run unit tests in the 'tests' directory. For more information, see <https://docs.python.org/3/library/unittest.html#test-discovery>
- python -m unittest discover tests
build:
commands:
# Use AWS SAM to package the application by using AWS CloudFormation
- aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.yml
# Do not remove this statement. This command is required for AWS CodeStar projects.
# Update the AWS Partition, AWS Region, account ID and project ID in the project ARN on template-configuration.json file so AWS CloudFormation can tag project resources.
- sed -i.bak 's/\$PARTITION\$/'${PARTITION}'/g;s/\$AWS_REGION\$/'${AWS_REGION}'/g;s/\$ACCOUNT_ID\$/'${ACCOUNT_ID}'/g;s/\$PROJECT_ID\$/'${PROJECT_ID}'/g' template-configuration.json
artifacts:
files:
- template-export.yml
- template-configuration.json
- - - 编辑 - - - - - - - - - -
现在我通过使用进一步得到了它
pip install -r requirements.txt -t .
和通配符
artifacts:
type: zip
files:
- template-export.yml
- template-configuration.json
- '**/*'
这在所有模块的文件夹中造成了一些混乱。它现在确实找到了所需的模块,并且包为 20MB。
但我不明白的是:为什么它适用于所有其他未打包的包?它们是否已经以某种方式安装在系统上?