AWS Lambda |如何使用层导入的依赖关系?
因此,我通常是AWS的新手,我正在尝试使用Lambda执行一些代码。 由于我的代码具有需要像请求模块
一样导入的依赖项,因此我尝试了许多添加模块的方法。 经过一些研究,我发现有两种可能的方法可以做到这一点。您可以将带有依赖项的ZIP文件包含在代码源中,也可以将其导入一层,然后将该图层连接到我的函数。
我选择了第二个原因。现在的问题是,我可以在lambda_function.py文件中使用模块请求,而不是其他文件。
我知道我缺少一些东西,但我不知道我缺少什么。
我的文件夹结构:
MyFirstFunction/
├── lambda_function.py
├── scripts/
│ ├── file0.py
│ ├── file1.py
│ ├── file2.py
│ └── file3.py
└── remote.py
│
└── server.py
│
└── executescript.py
函数logs
START RequestId: fa72c61d-2e34-4025-aa81-6205e8be288d Version: $LATEST
Traceback (most recent call last):
File "executescript.py", line 8, in <module>
from server import Server
File "/var/task/server.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
END RequestId: fa72c61d-2e34-4025-aa81-6205e8be288d
REPORT RequestId: fa72c61d-2e34-4025-aa81-6205e8be288d Duration: 753.01 ms Billed Duration: 754 ms Memory Size: 128 MB Max Memory Used: 50 MB Init Duration: 228.97 ms
上面错误的第8行是以下行:
from server import Server
server.py文件的第一行是:
import requests
我如何测试该模块确实导入了:
我刚刚写了一行:
import requests
在lambda_functions.py文件中,包括 没有错误的层,当我删除层时,它给出了以下错误:
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'requests'
这向我显示了模块被导入。
我的问题
我需要更改什么才能让Server.py文件使用请求模块(和其他模块)?
So I am very new to AWS in general and I am trying to execute some code using Lambda.
Since my code has dependencies that need to be imported like the request module
I have tried many ways to add the module.
After some research, I found out that there are 2 possible ways to do this. You can either include the zip file with the dependencies into the code source or import it into a Layer and then connect that layer to my function.
I chose the second for a few reasons. The problem now is that I can use the module requests inside the lambda_function.py file, but not the other files.
I know that I am missing something, but I do not know what I am missing.
My folder structure:
MyFirstFunction/
├── lambda_function.py
├── scripts/
│ ├── file0.py
│ ├── file1.py
│ ├── file2.py
│ └── file3.py
└── remote.py
│
└── server.py
│
└── executescript.py
The Function Logs
START RequestId: fa72c61d-2e34-4025-aa81-6205e8be288d Version: $LATEST
Traceback (most recent call last):
File "executescript.py", line 8, in <module>
from server import Server
File "/var/task/server.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
END RequestId: fa72c61d-2e34-4025-aa81-6205e8be288d
REPORT RequestId: fa72c61d-2e34-4025-aa81-6205e8be288d Duration: 753.01 ms Billed Duration: 754 ms Memory Size: 128 MB Max Memory Used: 50 MB Init Duration: 228.97 ms
This line 8 from the error above is the following line:
from server import Server
And the first line of the server.py file is:
import requests
How I tested that the module does get imported:
I just wrote the line:
import requests
Inside the lambda_functions.py file and including the layer it gave no error and when I deleted the layer it gave the following error:
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'requests'
Which shows me that the module is imported.
My Question
What do I need to change in order to let the server.py file use the requests module (and other modules)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论