Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to install Pyodbc for Sqlserver.
# Start a container that mimic the lambda environment
docker run -it --rm --entrypoint bash -e ODBCINI=/var/task -e ODBCSYSINI=/var/task -v "$PWD":/var/task lambci/lambda:build-python2.7
# Then, download ODBC source code, compile and take the output
curl -O
tar xvzf unixODBC-2.3.5.tar.gz
cd unixODBC-2.3.5
./configure --sysconfdir=/var/task --disable-gui --disable-drivers --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --prefix=/home
make install
cd ..
mv /home/* .
mv unixODBC-2.3.5 unixODBC-2.3.5.tar.gz /tmp/
# Install MSsql odbc driver
curl > /etc/yum.repos.d/mssql-release.repo
ACCEPT_EULA=Y yum -y install msodbcsql
export CFLAGS="-I/var/task/include"
export LDFLAGS="-L/var/task/lib"
# Then you can install pyodbc (or pip install -t . -r requirements.txt)
pip install pyodbc -t .
cp -r /opt/microsoft/msodbcsql .
cat <<EOF > odbcinst.ini
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
cat <<EOF > 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
# Test if it works
python -c "import pyodbc; print(pyodbc.drivers());"
python -c 'import pyodbc;conn = pyodbc.connect("DRIVER={ODBC Driver 13 for SQL Server}; SERVER=YOUr_SERVER:ADD;PORT=1443;DATABASE=TestDB;UID=SA;PWD=<YourStrong!Passw0rd>");crsr = conn.cursor();rows = crsr.execute("select @@VERSION").fetchall();print(rows);crsr.close();conn.close()'
Copy link

matw11180 commented May 28, 2019

Thanks Carlo. That is the exact screenshot that I am getting when trying to complete the package build

Copy link

gayathriramanujam commented May 29, 2019

Confirming that the same steps work in Redhat AMI.

Copy link

Sahilshetye commented Jun 5, 2019

run with

 yum install unixODBC --disablerepo=amzn*

you need to bypass the amzn repolist for installing correct unixODBC

Copy link

ca0abinary commented Jul 30, 2019

I created a Dockerfile based on this and the fixes mentioned since. It creates a .zip file usable as a lambda layer.

Copy link

rags1357 commented Nov 21, 2019

Hi Everyone!

I am facing a similar problem as most of you did before!
May be I am missing something!! :(

The Test Script works locally inside the docker container.
Could you please help me understand the steps that needs to be done after you zip the files.

This is what i did -

I created a and added inside the zip folder
Created a zip of it and uploaded it to the Lambda function ( Not as a layer)
Contents of my zip are :
total 12
drwxr-xr-x 3 root root 4096 Nov 20 23:28 microsoft
drwxr-xr-x 4 root root 4096 Nov 20 23:28 mssql-tools
drwxr-xr-x 3 root root 4096 Nov 20 23:22 python
bash-4.2# cd /opt
When i try to run the lambda it throws me an error saying that no module named "pyodbc"
Appreciate any help here! been stuck at this for a while now!


Copy link

ca0abinary commented Nov 21, 2019

Hi @rags1357! The gist I provided above has been updated with a comment that includes steps to test locally with AWS SAM. It includes a template.yaml with embedded python test code.

Copy link

rags1357 commented Dec 3, 2019

Thanks @ca0abinary! Appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment