0
votes

I am trying to connect to SQL Server using PYODBC inside AWS Lambda. I set up an EC2 instance and installed all dependencies and packages needed. I am able to query SQL within EC2 but not within Lambda.

Seems like I am missing a config or a library to set up the connections when packaging. I have copied the SQL driver in /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1 location. Copied the odbc.ini and odbcinst.ini files as needed. Copied the libtds and libodc* as well.

The error that I get when I package all the libraries and dependencies into lambda package is below:

[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)

My odbc.ini and odbcinst.ini is below:

odbc.ini 
[DEV] 
Driver = ODBC Driver 17 for SQL Server 
Description = DEV 
Trace = No 
Server = abc.net,1234

odbcinst.ini 
[ODBC Driver 17 for SQL Server] 
Description=Microsoft ODBC Driver 17 for SQL Server 
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1 
UsageCount=1 

I tried providing the DSN and also explicitly mentioning the Driver and Server name in pyodbc.connect. Still no luck!

Any help or direction would be appreciated!

TIA

1
Can you provide the contents of your odbc.ini and odbcinst.ini files? Do you have the driver specified correctly? Also, if you have a connection string, could you provide that? Perhaps there is an issue with that instead.gmiley
You should edit your question and add the information to that rather than here as a comment.gmiley
Is your odbcinst.ini really missing a line break? Driver= should be at the beginning of a line.Gord Thompson
Formatting issue ... fixing it!Sandesh
@Sandesh - It depends on what you mean. If you mean the location of the odbc.ini file(s) then no, they are determined by the driver manager (unixODBC), but odbcinst -j will show you where it is looking. If you mean to create a separate file to define your DSN then you can do that and then use something like pyodbc.connect('FILEDSN=/path/to/my.dsn').Gord Thompson

1 Answers

0
votes

After digging through multiple blogs and links, the answer is as simple as explicitly mentioning the location of driver when you connect using pyodbc.

conn = pyodbc.connect('Driver=msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1;Server=server,port;UID=uname;PWD=pwd;database=db_name;Encrypt=YES;TrustServerCertificate=Yes')