Ehud Grand
Ehud Grand

Reputation: 3703

AWS Lambda in Python fail to import module

I have a very simple Python I need to use as an AWS Lambda function:

import pyodbc


def lambda_handler():
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                      "Server=AAA;"
                      "Database=BBB;"
                      "UID=CCC;"
                      "PWD=DDD;")
cursor = cnxn.cursor()
cursor.execute("INSERT INTO [dbo].[log]([opened_by_id],[open_timestamp],[type_id],[title]) VALUES(118,GETDATE(),1,'test_1')")
cnxn.commit()

I've installed pyodbc using pip and have it as a folder where the py file is. both the py file and the pyodbc folder are added to a zip file that I've uploaded to Lambda functions Console. When I test the function I receive an error:

START RequestId: XXX Version: $LATEST
Unable to import module 'detect_last_sample': No module named 'pyodbc'

END RequestId: XXX
REPORT RequestId: YYY   Duration: 1.24 ms   Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 21 MB  

Please help me understand what am I missing here. Many thanks!

--EDIT Looks like there's more to this issue then simply creating a zip with the external libs, there is an issue with pyodbc itself when used in aws lambda. Anyway during my searches I found this repo containing pre complied libraries including pyodbc : https://github.com/Miserlou/lambda-packages

Good luck to all.

Upvotes: 0

Views: 2895

Answers (1)

Gingmeister
Gingmeister

Reputation: 272

Your zip file probably isn't structured properly. Did you establish a virtual environment when you pip installed your dependency and specify the correct version of Python?

Create one folder that contains (i) your code and (ii) the contents of the "site-package" folder - then zip the "contents" of the folder up (not the folder) and upload. Like this:

1) Create your virtual environment

python3.6 -m venv /path_where_you_want_a_virtual_env/

2) Activate your virtual env:

$source my_env/bin/activate

3) Install your dependency inside your virtual env:

python3.6 -m pip install pyodbc

AWS supports either 2.7 or 3.6. If you do not install the dependency in the correct folder it will not be there.

4) Copy the contents of the site-packages folder to a folder of your choice (can be found at /lib/python3.6/site-packages). Personally I use a file manager to do this.

5) Copy your .py script to the same folder.

6) Zip the contents of the folder (not the folder itself)

zip -r myzipfile.zip *

Go to the AWS management console and upload this zip file.

Seriously, its cream cheese once you've done it once ;-)

Additional edits:

(A) The main routine in your .py script should begin exactly like this: def lambda_handler(event, context):

(B) In the AWS Lambda function GUI you need to set the lambda handler as my_py_script_name.lambda_handler (i.e. just lose the .py and add .lambda_handler)

Upvotes: 1

Related Questions