How can i import and properly run pip imported libraries on lambda?

后端 未结 1 1301
無奈伤痛
無奈伤痛 2020-12-12 07:04

I imported boto3 for my lambda function in python. When i test lambda it gives this error : No module named boto3 which is rather expected. Then i

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 07:44

    You need to make a folder in your local system, install the required libraries into that folder, zip the contents of the folder and upload the zipped file to the AWS Lambda.

    • Make a folder in local system

    I don't think you need help in this. Lets suppose you made a folder in D drive named yellow-bot

    • Install the required libraries into the folder

    You can install the required packages in the folder using below command

        pip install {package-name} -t "{path-to-project-dir}"
    

    In your case it would be:

        pip install apiai -t "D:\yellow-bot"
    
    • Zip the contents of the folder

    Now after installing the required libraries there will be multiple files and folders in your yellow-bot folder. You need to select all and zip the content. Please note that do not zip the folder, instead you go inside the folder and zip the contents.
    It would be something like below screenshot.

    • Create lambda function and upload zip

    Now go to AWS Lambda, create a lambda function, give correct run-time and all that. Then select upload zip file in code entry type. Select your zip and click on upload.
    Make sure to give correct Handler.
    It follows naming convention as:

    The filename.handler-method value in your function. For example, "main.handler" would call the handler method defined in main.py.

    Since in this case I have uploaded connector.py file and entry function was called lambda_handler() so correct Handler would be connector.lambda_handler

    Click on Save and you are done.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题