python-3.x - 在 AWS Lambda 中安装 Python 包?
问题描述
我正在尝试在 AWS Lambda 函数中安装一个包。
Amazon 推荐的方法是创建一个包含依赖项和 python 函数的压缩部署包(如Python 中的 AWS Lambda 部署包中所述)。但是,这会导致无法使用 AWS Lambda GUI 中的内联代码编辑来编辑 Lambda 函数。
所以相反,我想在 AWS Lambda 函数本身内安装包。在 AWS Lambda 中,除了 /tmp/ 目录之外,文件系统是只读的,因此我尝试将 pip install 安装到 /tmp/ 目录。该函数每天只调用一次,因此我不介意每次运行该函数时重新安装软件包所需的额外几秒钟。
我的尝试
def lambda_handler(event, context):
# pip install dependencies
print('begin lambda handler')
import subprocess
import sys
subprocess.call('pip install cryptography -t /tmp/ --no-cache-dir'.split())
from cryptography.fernet import Fernet
pwd_encrypted = b'gAAAAABeTcT0OXH96ib7TD5-sTII6jMfUXPhMpwWRCF0315rWp4C0yav1XAPIn7prfkkA4tltYiWFAJ22bwuaj0z1CKaGl8vTgNd695SDl25HnLwu1xTzaQ='
key = b'fP-7YR1hUeVW4KmFmly4JdgotD6qjR52g11RQms6Llo='
cipher_suite = Fernet(key)
result = cipher_suite.decrypt(pwd_encrypted).decode('utf-8')
print(result)
print('end lambda handler')
但是,这会导致错误
[错误] ModuleNotFoundError:没有名为“密码学”的模块
我也尝试用以下内容替换子进程调用,如this stackoverflow answer中所建议的那样
cmd = sys.executable+' -m pip install cryptography -t dependencies --no-cache-dir'
subprocess.check_call(cmd.split())
但是,这会导致错误
OSError:[Errno 30] 只读文件系统:'/var/task/dependencies'
解决方案
我通过对原始尝试的单行调整解决了这个问题。您只需将 /tmp/ 添加到 sys.path 以便 Python 知道在 /tmp/ 中搜索该模块。您需要做的就是添加该行sys.path.insert(1, '/tmp/')
。
解决方案
import os
import sys
import subprocess
# pip install custom package to /tmp/ and add to path
subprocess.call('pip install cryptography -t /tmp/ --no-cache-dir'.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
sys.path.insert(1, '/tmp/')
from cryptography.fernet import Fernet
def lambda_handler(event, context):
# pip install dependencies
pwd_encrypted = b'gAAAAABeTcT0OXH96ib7TD5-sTII6jMfUXPhMpwWRCF0315rWp4C0yav1XAPIn7prfkkA4tltYiWFAJ22bwuaj0z1CKaGl8vTgNd695SDl25HnLwu1xTzaQ='
key = b'fP-7YR1hUeVW4KmFmly4JdgotD6qjR52g11RQms6Llo='
cipher_suite = Fernet(key)
result = cipher_suite.decrypt(pwd_encrypted).decode('utf-8')
print(result)
输出
你好堆栈溢出!
注意 - 正如评论中提到的@JohnRotenstein,添加 Python 包的首选方法是在AWS Lambda 层中打包依赖项。我的解决方案只是表明可以直接在 AWS Lambda 函数中安装包。
推荐阅读
- python - 如何在 Pandas 中绘制和标记多个自相关函数?
- python - 在 Pandas DataFrame 中重新分配最大值
- c++ - 我是初学者,刚开始学习 c++,我在 windows 10 64 位 pc 上使用 eclipse [版本:Helios Service Release 2 Build id:20110218-0911]
- printing - 如何限制 Fortran 中每行打印的字符数?
- ios - 如果要使用主故事板文件,应用程序委托必须实现 window 属性。(迅速)
- java - 在使用嵌套 for 循环时,if 语句是否会等到循环结束后再继续执行 if 语句代码?
- javascript - 将数组中的值放入文本框中
- javascript - 在 Angular 中使用 BehaviourSubject 在两个组件之间进行通信
- domain-driven-design - 工厂模式实现类在 DDD 的干净架构中位于何处
- reactjs - Handling error in React with Formik: Objects are not valid as a React child