0

How can I use external python libraries in aws lambda function? I have tried to make a virtual environment using the following command:

py -m venv .lambda

Then I activate the venv using the following command:

.\.lambda\Scripts\activate

Then I install pyodbc in venv using the following command:

py -m pip install pyodbc

All of this goes well.

Now, when I want to use pyodbc in my aws lambda function I have zip the venv folder (.lambda) and upload it to a layer. I zip the folder by using "Compressed (zipped) folder".

I am uploading the zipped folder by going to layers in aws lambda function and press "create layers".

After creating the layer I use it in my aws lambda function. But even then when I try to import pyodbc library I get the error:

No module named 'pyodbc'

I am importing the library in my aws lambda function using the following command:

import pyodbc
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Can it have something to do with the way i Call it? – Saifullah Babrak Jul 27 '21 at 08:40
  • Impossible to tell when you don't reveal how you do that; but generally speaking, yes it can. Please [edit] your question to replace the images with text, and include the command you are using to run the code etc. – tripleee Jul 27 '21 at 08:41
  • I have added the commands. Is it clear or do you want more information? – Saifullah Babrak Jul 27 '21 at 08:54
  • I see no indication that you `activate` before you run the code which fails. – tripleee Jul 27 '21 at 08:55
  • I activate the venv by using .\.lambda\Scripts\activate? – Saifullah Babrak Jul 27 '21 at 08:56
  • Before you run the Python code? In the same session? How do you run the Python code? And again, why do you insist on installing in a virtual environment? Your lambda is already isolated from anything else. – tripleee Jul 27 '21 at 08:57
  • Yeah I activate the venv before I install pyodbc. But the problem is not importing pyodbc locally. The problem occurs when I try to import it in aws lambda. Maybe I have not make myself clear? – Saifullah Babrak Jul 27 '21 at 08:59
  • Actually, I do not insist on making a venv. I want to be able to use pyodbc in my lambda function. If you know a way, tell me please :) – Saifullah Babrak Jul 27 '21 at 09:03
  • Just `pip install` it without a virtual environment is absolutely the simplest solution. If you use a virtual environment, you need the actual lambda code to also run `activate` somehow. – tripleee Jul 27 '21 at 09:11
  • But how do I use the lib when I have deployed the aws lambda function? – Saifullah Babrak Jul 27 '21 at 09:20
  • Are you looking for this? https://stackoverflow.com/questions/60311148/pip-install-python-package-within-aws-lambda – tripleee Jul 27 '21 at 09:25
  • Its recommended packaging all third-party packages into a folder called `packages` within your deployment ZIP file as a standalone portable application. – Prav Jul 27 '21 at 12:07
  • @PraveenPremaratne, this might be a stupid question. But would you just then install third-party packages in the aws lambda function without zipping them? Or should I zip them? – Saifullah Babrak Jul 27 '21 at 12:25
  • You don't need to, AWS Lambda will install all the packages that are in the `packages` within the ZIP file as if you were to run `pip install`. So you don't actually need to do anything, Lambda takes care of that. This is how we packages things. `mkdir packages; cd packages; pip install -r ../requirements.txt --target .; zip -r9 ../deployment.zip .; cd ..; zip -g deployment.zip app.py; rm -rf packages` – Prav Jul 27 '21 at 14:04
  • Hi @PraveenPremaratne. I have tried as you suggested but still without luck :( I have also tried as in following youtube video https://www.youtube.com/watch?v=ebhcs-9FYJA but again without luck. Thx for your answear anyway. – Saifullah Babrak Jul 30 '21 at 12:58

0 Answers0