SQL server connection works fine from lambda container but fails after uploading zip to aws lambda

空扰寡人 提交于 2020-06-09 05:20:09

问题


I was following this medium blog to try to establish a connection to a MSSQL db from a lambda function. I used lambci/lambda:build-python3.8 image to build the container. I followed all the steps mentioned and was able to successfully run the python test script that verifies the connection.

Below is the content of the zip file inside the container.

However, when I zip it up and upload to lambda, I am receiving below error.

START RequestId: 6f3ec44e-aac0-4cc3-b5ed-f8935c4dff4d Version: $LATEST
['bin', 'include', 'lib', 'msodbcsql', 'odbc.ini', 'odbcinst.ini', 'pyodbc.so', 'test.py']
[ERROR] Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/var/task/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2' : file not found (0) (SQLDriverConnect)")
Traceback (most recent call last):
  File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/test.py", line 9, in <module>
    conn=pyodbc.connect("DRIVER={ODBC Driver 13 for SQL Server};SERVER=***;PORT=1433;DATABASE=***;UID=***;PWD=***")END RequestId: 6f3ec44e-aac0-4cc3-b5ed-f8935c4dff4d
REPORT RequestId: 6f3ec44e-aac0-4cc3-b5ed-f8935c4dff4d  Duration: 1583.04 ms    Billed Duration: 1600 ms    Memory Size: 128 MB Max Memory Used: 15 MB  
Unknown application error occurred

Both the environment are using python -3.8.

test.py

import sys
import logging
import pyodbc
import os

logger=logging.getLogger()
arr = os.listdir('/var/task')
print(arr)
conn=pyodbc.connect("DRIVER={ODBC Driver 13 for SQL Server};SERVER=***;PORT=1433;DATABASE=***;UID=***;PWD=***")

logger.info("SUCCESS: Connection to SQLSever succeeded")
def handler(event,context):
    item_count=0
    crsr=conn.cursor()
    rows=crsr.execute("select @@VERSION").fetchall()
    print(rows)
    logger.info(rows)
    crsr.close()
    conn.close()

odbcinst

[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/var/task/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2
UsageCount=1

Can anyone please help me in debugging this.

UPDATE

confirming that the file is indeed present

After putting below in the python code, I see the file is present in lambda.

arr = os.listdir('/var/task/msodbcsql/lib64')
print(arr)


回答1:


Finally after days of scratching my head, I was able to resolve this. For anyone who faces similar problem, please follow below steps.

  1. try with python - 3.7. It worked for me in 7 and not in 8.
  2. Use msodbcsql17 instead of msodbcsql
  3. Use unixODBC 2.3.7 here
  4. Change the driver info in odbc, obdcinst config files to ODBC Driver 17 for SQL Server.

After doing all of this, I was able to zip it up and successfully deploy in aws lambda.

Hope it helps someone !!



来源:https://stackoverflow.com/questions/62001501/sql-server-connection-works-fine-from-lambda-container-but-fails-after-uploading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!