I've been stuck trying to connect to an SQL Server using AWS Lambda functions for a long while now.
To do so i'm trying to use any library (tried with pyodbc, pypyodbc, etc), packaging everything into a zip file and uploading the code.
The code is pretty much the same for every library, but the errors are different.
The code:
import pypyodbc
def lambda_handler(event, context):
conn = pypyodbc.connect('DRIVER={SQL Server};'
'SERVER=1.1.1.1;'
'DATABASE=dbname;'
'UID=user;'
'PWD=pwd')
cur = conn.cursor()
cur.execute("SELECT * FROM Table")
item_count = 0
for row in cur:
item_count += 1
print(item_count)
cur.close()
conn.close()
return item_count
Common issues that i have covered: - I'm adding to the zip the project contents, not the folder. - I'm also adding to the zip file the libraries needed for the code to run.
If i try to use pyodbc, the zip i'm uploading looks like this:
.idea (dir)
pyodbc (dir)
lambda_function.py
pyodbc.pyd
The error i get:
Unable to import module 'lambda_function': No module named pyodbc
After searching for quite a while about this, i couldn't find anything that helps. Only one comment that said that pyodbc needed to be instaled on a linux enviroment for the lambda function to work. But i don't have that Enviroment available, also i don't know if that will fix this.
If i try to use pypyodbc, the zip i'm uploading looks like this:
The error i get:
module initialization error: 'ODBC Library is not found. Is LD_LIBRARY_PATH set?'
For this one i tried to install multiple python packages suggested by other stackoverflow posts (python-pyodb, unixodbc), but i failed every time.
Then there was one comment around saying "Make sure to put native ODBC libraries under the lib folder in your zip deployment package"
Maybe that is some help? I don't know how to get native ODBC libraries..
Oh and one last thing. Both libraries work if i run them from my local machine. I can get access to the target server. It fails if i do it from the lambda function.
Hopefully someone can help me and, apparently, the whole internet with this.
yum install gcc gcc-c++
wget ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.5.tar.gz
tar xvzf unixODBC-2.3.5.tar.gz
cd unixODBC-2.3.5
configure it with the correct sysconfdir value
./configure --sysconfdir=/var/task --disable-gui --disable-drivers --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --prefix=/home
make install
[ODBC Driver 13 for SQL Server] Description=Microsoft ODBC Driver 13 for SQL Server Driver=/var/task/msodbcsql/msodbcsql/lib64/libmsodbcsql-13.1.so.9.1 UsageCount=1
On your computer, in the same root directory create file odbc.ini
[ODBC Driver 13 for SQL Server] Driver = ODBC Driver 13 for SQL Server Description = My ODBC Driver 13 for SQL Server Trace = No
on your python program use pyodbc:
import pyodbc def lambda_handler(event, context): server = "xxxxxxxxxxxxxxxxxxxx" database = "xxxxxxxxxxxxxxxxxxxx" username = "xxxxxxxxxxxxxxxxxxxx" password = "xxxxxxxxxxxxxxxxxxxx" cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) cursor = cnxn.cursor() ...other things....
and now play the game !