如何使用aws控制台从Windows本地计算机将带库的python代码上传到aws lambda [英] how to upload python code with libraries to aws lambda from windows local machine using aws console

查看:156
本文介绍了如何使用aws控制台从Windows本地计算机将带库的python代码上传到aws lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用通过API网关触发的AWS Lambda.我有python脚本,该脚本从S3bucket加载机器学习模型,并从api调用获取输入并预测结果.我可以成功触发以python内联编写的lambda函数.但是我想使用机器学习包来预测lambda函数.所以我知道我需要上传代码并在虚拟环境中安装了软件包,但是确实做到了,但是lambda触发时会出现错误``无法导入模型lambda_function''.我有lambda_function.py与方法处理程序".请让我知道Iam是否做对了(创建虚拟环境并安装软件包并上传),为什么会出现此错误.另外,请让我知道Windows和AWS控制台的解决方案.我已经看到许多关于Linux命令和使用aws cli的答案.

I need to use AWS Lambda triggered through API gateway. I have python script which loads a machine learning model from S3bucket and gets input from api call and predicts the result. I can successfully trigger the lambda function written inline in python. But I want to use machine learning packages to predict in lambda function. So I came to know that I need to upload the code with the packages installed in virtual environment and I did.But the lambda when triggered gives the error 'Unable to import model lambda_function'. I have lambda_function.py with method 'handler'. Please let me know if Iam doing it right(creating virtual env and installing packages and uploading it) and why is this error. Also, let me know the solutions for Windows and AWS console. I have seen many answers with Linux commands and using aws cli.

zip文件夹

lambda_function

lamnda函数设置

lambda函数设置

更新:

这让我发疯了!.我已经尝试了答案中找到的所有方法,但没有一种适合我.而且它给出了相同的错误:无法导入模块:lambda_function",因此Iam无法理解错误的位置.如果您有任何建议,请帮助我.在您说函数名称之前:我有正确的名称:lambda_function.lambda_handler.我压缩内容而不是目录.请在下面查看我的Lambda代码和Lambda设置 lambda json文件 lambda函数代码: 导入boto3 导入操作系统 导入uuid 导入sklearn 进口泡菜

This is driving me crazy!. I have tried all the methods found in the answers and none works for me. And it gives the same error : 'Unable to import module : lambda_function' So Iam not able to understand where the error is. Please help me if you have any suggestion. Before you say function names: I have correct names: lambda_function.lambda_handler. I zipped the contents and not directory. Please see my lambda code and lambda settings below lambda json file lambda function code: import boto3 import os import uuid import sklearn import pickle

def lambda_handler(event, context):
s3_client = boto3.client('s3')
s_desc=event['params']['querystring']['token']
X_test1=[]
X_test1.append(s_desc)
#load model
bucket = 'harshini-snow-bucket'
key = 'model.pkl'
download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
s3_client.download_file(bucket, key, download_path)

f = open(download_path, 'rb')
model = pickle.load(f)
f.close()
#class_predicted = model.predict(X_test1)

return X_test1

请告诉我是否还有其他方法..我将尝试执行所有操作.

Please tell me if there are any other ways.. I will try anything for this to work.

更新2:

错误

代码

推荐答案

您必须使用 docker EC2 为lambda创建自定义部署程序包.如果您在本地计算机上制作软件包,它将无法正常工作,因为它将无法编译所需的库.

You have to make a custom deployment package for lambda either using docker or EC2. It will not work if you make a package in local machine as it will not compile the libaries needed.

这是完整的示例,您将如何制作自定义包,此示例将python的图像处理库 PILLOW 与lambda代码一起打包,您可以打包模型所需的所有其他库在同一包中以相同的方式与PIL一起

here is the complete example, how you will make a custom package , this example packages PILLOW image processing library of python along with the lambda code , you can pack all other libraries you need for your model in the same way in the same package along with PIL

链接到示例

记住一件事,例如文件名是 CreateThumbnail.py ,您可以为其指定任何名称,但始终以这种方式配置处理程序filename.handler-function,例如 disco.lambda_handler ,其中 disco.py 是文件名,而 lambda_handler 是lambda的代码处理程序模块

Remember one thing , in example filename is CreateThumbnail.py, you can give it any name , but always configure your handler in this way filename.handler-function , e.g disco.lambda_handler where disco.py is filename and lambda_handler is code handler module for lambda

这篇关于如何使用aws控制台从Windows本地计算机将带库的python代码上传到aws lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆