简体   繁体   English

如何使用 aws 控制台将带有库的 python 代码从 Windows 本地机器上传到 aws lambda

[英]how to upload python code with libraries to aws lambda from windows local machine using aws console

I need to use AWS Lambda triggered through API gateway.我需要使用通过 API 网关触发的 AWS Lambda。 I have python script which loads a machine learning model from S3bucket and gets input from api call and predicts the result.我有 python 脚本,它从 S3bucket 加载机器学习模型并从 api 调用获取输入并预测结果。 I can successfully trigger the lambda function written inline in python.我可以成功触发在 python 中内联编写的 lambda 函数。 But I want to use machine learning packages to predict in lambda function.但我想使用机器学习包来预测 lambda 函数。 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'.所以我开始知道我需要上传带有安装在虚拟环境中的包的代码,我做到了。但是 lambda 在触发时会给出错误“无法导入模型 lambda_function”。 I have lambda_function.py with method 'handler'.我有带有方法“handler”的 lambda_function.py。 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.另外,让我知道 Windows 和 AWS 控制台的解决方案。 I have seen many answers with Linux commands and using aws cli.我已经看到很多关于 Linux 命令和使用 aws cli 的答案。

zip folder ZIP文件夹

lambda_function lambda_function

lamnda function settings lamnda 功能设置

lambda function settings lambda 函数设置

Update:更新:

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.它给出了相同的错误:'无法导入模块:lambda_function'所以我无法理解错误在哪里。 Please help me if you have any suggestion.如果您有任何建议,请帮助我。 Before you say function names: I have correct names: lambda_function.lambda_handler.在你说函数名称之前:我有正确的名称: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请在下面的lambda json 文件lambda 函数代码中查看我的 lambda 代码和 lambda 设置: 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.请告诉我是否还有其他方法..我会尝试任何方法来解决这个问题。

Update 2:更新 2:

error错误

code代码

First install all the required packages inside a folder in local machine.首先在本地机器的文件夹中安装所有必需的包。 Also include the main lambda_function.py file inside that folder.还包括该文件夹中的主要 lambda_function.py 文件。 Now select all the packages and the python file inside the folder and compress them into a .zip file.现在选择文件夹中的所有包和 python 文件,并将它们压缩为 .zip 文件。 Here you have to make sure you compress contents of the folder not the folder itself.在这里,您必须确保压缩文件夹的内容而不是文件夹本身。 Then you can upload the zip file to lambda either directly or through s3.然后您可以直接或通过 s3 将 zip 文件上传到 lambda。 Another point to note is if the python file is named " lambda_function.py " or not.另一点要注意的是,python 文件是否命名为“ lambda_function.py ”。 By default lambda assumes the main python file to have "lambda_function.py" name.默认情况下,lambda 假定主 python 文件具有“lambda_function.py”名称。 If you have used any other name, you can change the filename from lambda console under Function code section inside Handler ...Hope this helps out.如果您使用了任何其他名称,则可以在Handler内的Function code部分下从 lambda 控制台更改文件名...希望这会有所帮助。

You have to make a custom deployment package for lambda either using docker or EC2 .您必须使用dockerEC2为 lambda 制作自定义部署包。 It will not work if you make a package in local machine as it will not compile the libaries needed.如果您在本地机器上制作一个包,它将不起作用,因为它不会编译所需的库。

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

link to example 链接到示例

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 , eg disco.lambda_handler where disco.py is filename and lambda_handler is code handler module for lambda记住一件事,在示例文件名是CreateThumbnail.py ,您可以给它任何名称,但始终以这种方式配置您的处理程序 filename.handler-function ,例如disco.lambda_handler其中disco.py是文件名, lambda_handler是代码处理程序模块拉姆达

The answers here are technically correct but if you're working on a Windows machine (or anything other than Amazon Linux), you might have a lot of issues putting custom packages in your Python app.此处的答案在技术上是正确的,但如果您在 Windows 机器(或 Amazon Linux 以外的任何机器)上工作,则在将自定义包放入 Python 应用程序时可能会遇到很多问题。 Lambda apps run on Amazon Linux so you need to install the packages using that OS or anything as close as possible. Lambda 应用程序在 Amazon Linux 上运行,因此您需要使用该操作系统或任何尽可能接近的操作系统安装软件包。

Here's one of my answers which describes how I put together packages for my Lambda app:这是我的答案之一,它描述了我如何为我的 Lambda 应用程序组合包:

https://stackoverflow.com/a/50767639/3023353 https://stackoverflow.com/a/50767639/3023353

I know that i am a bit late for answering this question but I was facing the same problem with pandas library for python.我知道我回答这个问题有点晚了,但我在使用 python 的 pandas 库时遇到了同样的问题。

this was the link which help me to solve this issue HERE这是帮助我在这里解决此问题的链接

so the answer in a nutshell is所以简而言之,答案是

1.go to here 1.去这里

2. search the library you want to use 2.搜索你要使用的库

3. then go to download option 3.然后去下载选项

4. download the file with Linux option 4.下载带有Linux选项的文件

5.Unzip the file in your project folder (I use 7-zip for that) 5.解压缩项目文件夹中的文件(我使用7-zip)

6.now make a new zip of your project and upload on lambda 6.现在为您的项目制作一个新的zip并上传到lambda

You need to add a Layer for outside python libraries in AWS Lambda.您需要在 AWS Lambda 中为外部 Python 库添加一个层。

https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

The AWS documentation is very bad with examples and for our team it was not easy. AWS 文档中的示例非常糟糕,对于我们的团队来说,这并不容易。 Had to manually install a number of libraries but this worked.不得不手动安装一些库,但这有效。

This drove me crazy too.这也让我发疯。 I am using Windows to create the python packages.我正在使用 Windows 创建 python 包。 The key to solving this is to store your packages in a folder named python .解决这个问题的关键是将你的包存储在一个名为python的文件夹中。 When I named the folder anything else it wouldnt work.当我将文件夹命名为其他任何内容时,它都不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 AWS Secrets Manager 与 Python 结合使用(Lambda 控制台) - Using AWS Secrets Manager with Python (Lambda Console) 如何在 AWS EC2 上运行 Python 代码并将 csv 文件从服务器写入我的本地计算机? - How to run a Python code on AWS EC2 and write a csv file from the server to my local machine? 如何使用本地机器上的python 2.7执行aws胶水脚本? - How to execute aws glue scripts using python 2.7 from local machine? 如何使用 AWS Lambda 函数将文件从 AWS S3 存储桶复制到 EC2 linux 机器 - How to copy files from AWS S3 bucket to EC2 linux machine using AWS Lambda Functions 如何设置PyCharm以在本地计算机上开发AWS Lambda功能? - How to setup PyCharm to develop AWS Lambda function on local machine? 如何使用 python 中的 AWS lambda function 将 excel 文件上传到 AWS S3 - How to upload excel file to AWS S3 using an AWS lambda function in python 从 S3 存储桶导入 AWS Lambda 函数代码中的库 - Importing libraries in AWS Lambda function code from S3 bucket AWS Lambda Function (Python) - 如何使用外部库? - AWS Lambda Function (Python) - How to use external libraries? 如何将 python 库添加到用于 Alexa 的 AWS lambda function? - How do I add python libraries to an AWS lambda function for Alexa? 如何组织 AWS Lambda 层的 Python 代码 - How to organize Python code for AWS Lambda Layers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM