AWS Lambda connecting to SQL with Python and pyodbc

旧街凉风 提交于 2020-01-16 16:56:09

问题


I have an AWS Lambda that I want to connect to my on prem SQL server to read and write data from\to. I am using Python and pyodbc. I have got pyodbc installed (compiled zip file in an S3 bucket added to the lambda through a layer), but when I try and run this code I get an odd error:

import boto3
import pyodbc

s3 = boto3.client('s3')

def lambda_handler(event, context):
    # print(help(pyodbc))
    server = "Server"
    database = "Database"
    username = "AWS-Lamdba-RO"
    password = "Password"
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
    cursor = cnxn.cursor()

This is the error:

[ERROR] AttributeError: module 'pyodbc' has no attribute 'connect' Traceback (most recent call last):   File "/var/task/lambda_function.py", line 13, in lambda_handler     cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

All I'm finding online is people who are unable to get the pyodbc library installed in the first place, so having got past that sticking point I thought I was free and clear. Can anyone explain what I've run into now?

I got pyodbc from here: https://github.com/Miserlou/lambda-packages/tree/master/lambda_packages/pyodbc

AWS didn't recognise .tar.gz files, so I changed it to a zip file and also added in the folder structure which another googled site told me was necessary: \python\lib\python3.7\site-packages\pyodbc that folder contains: libodbc.so.2 pyodbc.so

I uploaded this Zip file to an S3 bucket and pointed a Lambda layer at it.

Have I done something silly with this?


回答1:


From your description, I believe you may have gotten your folder structure wrong.

If you were to look in your zip file, you should see the following structure:

layerZip.zip
└ python
  └ lib
    └ python3.7
      └ site-packages
        └ pyodbc

But I believe you may actually have

layerZip.zip
└ \
  └ python
    └ lib
      └ python3.7
        └ site-packages
          └ pyodbc

But honestly, just use this structure:

layerZip.zip
└ python
  └ pyodbc

It'll work just as well, it's just setting the module up as global instead of per user.




回答2:


I had the same issue. You must use correct version of python when building the package to upload to lambda.

I did a 'pip install pyodbc -t .'

It placed the following:

drwxr-xr-x 2 root root   4096 Sep 20 21:29 pyodbc-4.0.27.dist-info
-rwxr-xr-x 1 root root 658704 Sep 20 21:29 pyodbc.cpython-36m-x86_64-linux-gnu.so

The lib is very specific to the version of python. In the file name,'-36m-', so it will work the lambda python 3.6 environment.

My initial issue was I was using lambci/lambda:build-python3.7 for my docker environment. So pyodbc installed the python 3.7 version of the library, '-37m-'.

Since lambda was looking for the 3.6 version it did not see the 3.7 version.

I switched to using lambci/lambda:build-python3.6 and all was better.

I followed this article for getting everything working:

https://medium.com/faun/aws-lambda-microsoft-sql-server-how-to-66c5f9d275ed



来源:https://stackoverflow.com/questions/57355418/aws-lambda-connecting-to-sql-with-python-and-pyodbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!