Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Docker - Build pyOdbc Microsoft SQL AWS Lambda Layer
FROM lambci/lambda:build-python3.6
WORKDIR /root
RUN yum -y update
RUN curl ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.5.tar.gz -O && \
tar xvzf unixODBC-2.3.5.tar.gz && \
cd unixODBC-2.3.5 && \
./configure --sysconfdir=/opt/python --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
RUN curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo && \
yum -y install e2fsprogs && \
ACCEPT_EULA=Y yum -y install msodbcsql --disablerepo=amzn*
RUN export CFLAGS="-I/root/include" && \
export LDFLAGS="-L/root/lib" && \
pip install pyodbc -t .
RUN cp -r /opt/microsoft/msodbcsql .
# odbc.ini contents:
# [ODBC Driver 13 for SQL Server]
# Driver = ODBC Driver 13 for SQL Server
# Description = My ODBC Driver 13 for SQL Server
# Trace = No
ADD odbc.ini /root
# odbcinst.init contents
# [ODBC Driver 13 for SQL Server]
# Description=Microsoft ODBC Driver 13 for SQL Server
# Driver=/opt/python/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2
# UsageCount=1
ADD odbcinst.ini /root
# This is a good place to add any other py modules related to db access
RUN mkdir -p /opt/python && \
cp -r /root/* /opt/python && \
mv /opt/python/lib /opt && \
mv /opt/python/bin /opt && \
cd /opt && \
rm -fr microsoft && \
zip -r /python-odbc.zip .
@jconway4

This comment has been minimized.

Copy link

jconway4 commented Nov 5, 2019

Build failed:

Step 8/10 : ADD odbc.ini /root
ADD failed: stat /var/lib/docker/tmp/docker-builder209335974/odbc.ini: no such file or directory

Original repo's code with the cat invocations worked fine for .ini files. I'll post a workaround if anyone else is having this problem.

@thanuj11

This comment has been minimized.

Copy link

thanuj11 commented Nov 13, 2019

@jconway4 can you please post your workaround which works. I am figuring out how to install pyodbc for AWS Lambda and not successful. Maybe I will be successful with your workaround.

@ca0abinary

This comment has been minimized.

Copy link
Owner Author

ca0abinary commented Nov 13, 2019

@thanuj11 Try using AWS SAM, put pyodbc in the requirements.txt and build with sam build --use-container. That should work fine for using pyodbc in a Lambda function, even testing locally should work.

@thanuj11

This comment has been minimized.

Copy link

thanuj11 commented Nov 15, 2019

@thanuj11 Try using AWS SAM, put pyodbc in the requirements.txt and build with sam build --use-container. That should work fine for using pyodbc in a Lambda function, even testing locally should work.

@ca0abinary Thanks for the reply, I tried using AWS SAM but still it fails with the error below
Build Failed
Error: PythonPipBuilder:ResolveDependencies - {pyodbc==4.0.25(sdist)}
Running PythonPipBuilder:ResolveDependencies

Details
python_verison:3.6
SAM template version:3.6

Any comments on how to make this work for AWS lambda will be a great help

@ca0abinary

This comment has been minimized.

Copy link
Owner Author

ca0abinary commented Nov 18, 2019

@thanuj11 - these are the commands I use to build and create a lambda layer zip file. Simply including the uploaded lambda layer in your lambda function should enable you to use pyodbc without a requirements.txt.

docker build -t pyodbc .
docker run -d --name pyodbc pyodbc true
docker cp pyodbc:/python-odbc.zip ./python-odbc.zip
docker rm -f pyodbc
@rags1357

This comment has been minimized.

Copy link

rags1357 commented Nov 19, 2019

@ca0abinary I am facing a similar problem trying to run MSSQL from a Lambda using pyodbc. The instructions you mentioned above doesn' t seem to be building the container. I am trying to do it on my Mac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.