Creating AWS Lambda function using python pyodbc library throw an error "module 'pyodbc' has no attribute ' connect'

Question:

I am trying to create a simple Lambda function to query the msdb of AWS RDS SQL server to monitor for failed jobs. I downloaded the python module pyodbc from https://github.com/Miserlou/lambda-packages/tree/master/lambda_packages/pyodbc
changed the file type from .tar.gz to zip and extracted the two file libodbc.so.2 and pyodbc.so I then put the two files in a zip folder structured like this
pythonjob.zippythonlibpython3.7site-packagespyodbc and in pyodbc exists libodbc.so.2 and pyodbc.so
I then uploaded the pythonjob.zip file on Lambda Layer and tested my script

import pyodbc
print(dir(pyodbc))

#ConnectionValues

endpoint = 'myservername.us-east-2.rds.amazonaws.com'
username = 'username'
password = 'password'
database_name = 'msdb'
conn = pyodbc.Connect( server= endpoint, user= username, password=password, database= database_name)

However I am getting the error [ERROR] AttributeError: module 'pyodbc' has no attribute 'Connect' and the print(dir(pyodbc)) prints ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

I am not sure what to do now. Your help is appreciated. Thank You!

Asked By: Esu

||

Answers:

@Esu Please change the connection statement to below:

conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+endpoint+';DATABASE='+database_name+';UID='+username+';PWD='+ password)

Also, The lambda layer which you’re using doesn’t contain the required packages. You can download the file from the below link, upload zip to the lambda layer and try the connection again.

https://github.com/alexanderluiscampino/lambda-layers/blob/master/pyodbc-9ef8961d-ce5b-4603-b397-03c9a6316eca.zip

Hope it works.

Answered By: Aditya

The error here seems to be from pyodbc.Connect. you have pyodbc.Connect instead of pyodbc.connect. Your C is in capital letters rather than small letter c.

So this will work:

import pyodbc
print(dir(pyodbc))

#ConnectionValues

endpoint = 'myservername.us-east-2.rds.amazonaws.com'
username = 'username'
password = 'password'
database_name = 'msdb'
conn = pyodbc.connect( server= endpoint, user= username, password=password, database= database_name)
Answered By: MEdwin