How can i import and properly run pip imported lib

2019-01-20 19:19发布

问题:

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 referred to the docs,to this link to be exact https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html and realized i need to create a deployment package the issue is i didn't understand the docs well enough to keep up and would like them to be explained again in a simpler way by someone that has already done the required steps.

回答1:

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.



标签: aws-lambda