Search code examples
pythonwindowsamazon-web-servicesaws-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. 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 folder

lambda_function

lamnda function settings

lambda function settings

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. 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.

Update 2:

error

code


Solution

  • 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.

    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

    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 , e.g disco.lambda_handler where disco.py is filename and lambda_handler is code handler module for lambda