SQL Server连接可以从lambda容器正常工作,但是在将zip上传到AWS lambda后失败 [英] SQL server connection works fine from lambda container but fails after uploading zip to aws lambda

查看:111
本文介绍了SQL Server连接可以从lambda容器正常工作,但是在将zip上传到AWS lambda后失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注媒体博客尝试通过lambda函数建立与MSSQL db的连接.我使用了lambci/lambda:build-python3.8镜像来构建容器.我遵循了上面提到的所有步骤,并且能够成功运行验证连接的python测试脚本.

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.

下面是容器内zip文件的内容.

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

但是,当我将其压缩并上传到lambda时,我收到以下错误消息.

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

两个环境都使用python -3.8.

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.

确认文件确实存在

在python代码中放入以下内容后,我看到该文件存在于lambda中.

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

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

推荐答案

最后,经过几天的努力,我终于能够解决这个问题.对于遇到类似问题的任何人,请按照以下步骤操作.

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

  1. 尝试使用python-3.7.它在7而不是8中对我有用.
  2. 使用msodbcsql17代替msodbcsql
  3. 使用unixODBC 2.3.7 此处
  4. 将odbc,obdcinst配置文件中的驱动程序信息更改为ODBC Driver 17 for SQL Server.
  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.

完成所有这些操作后,我便可以将其压缩并成功部署在aws lambda中.

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

希望它对某人有帮助!

这篇关于SQL Server连接可以从lambda容器正常工作,但是在将zip上传到AWS lambda后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆