[英]install library in python lambda
This is how my folder structure looks like.这就是我的文件夹结构的样子。 I want to use the requests library in my Python lambda function. I have installed the library within the package folder.我想在我的 Python lambda function 中使用请求库。我已将库安装在 package 文件夹中。
I am creating a zip like this.我正在创建这样的 zip。 But how can I include the library from /packages into the lambda too?但是我怎样才能将 /packages 中的库也包含到 lambda 中呢? Lambda.tf: Lambda.tf:
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "${path.module}/src/trigger_bitbucket_pipeline_from_s3.py"
output_file_mode = "0666"
output_path = "${path.module}/bin/trigger_bitbucket_pipeline_from_s3.zip"
}
resource "aws_lambda_function" "processing_lambda" {
filename = data.archive_file.lambda_zip.output_path
function_name = "triggering_pipleline_lambda"
handler = "trigger_bitbucket_pipeline_from_s3.lambda_handler"
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
role = aws_iam_role.processing_lambda_role.arn
runtime = "python3.9"
}
My lambda function in src/trigger_bitbucket_pipeline_from_s3.py is pretty straightforward for now:我在 src/trigger_bitbucket_pipeline_from_s3.py 中的 lambda function 现在非常简单:
import logging
import requests
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
logger.info(f'## EVENT: {event}')
return {
'statusCode': 200,
}
Currently, my Lambda function gives this error:目前,我的 Lambda function 给出了这个错误:
[ERROR] Runtime.ImportModuleError: Unable to import module 'trigger_bitbucket_pipeline_from_s3': No module named 'requests'
I also tried installing the library in the root src/ folder instead of packages.我还尝试将库安装在根 src/ 文件夹中而不是包中。 However, I still got the same error so I guess I am zipping incorrectly perhaps?但是,我仍然遇到同样的错误,所以我想我可能压缩不正确?
You can try following commands which worked for me.您可以尝试执行对我有用的命令。
pip install --target ./package <module name>
pip install --target ./package requests
cd package
Place your trigger_bitbucket_pipeline_from_s3.py file to root folder in package/将您的trigger_bitbucket_pipeline_from_s3.py文件放在 package/ 中的根文件夹中
zip -r ../my-deployment-package.zip .
Hope it helps Thank you希望对你有帮助谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.