AWS Lambda使用Python和Pyodbc连接到SQL [英] AWS Lambda connecting to SQL with Python and pyodbc
问题描述
我有一个AWS Lambda,我想连接到我的Prem SQL Server上以读取\ to数据.我正在使用Python和pyodbc.我已经安装了pyodbc(将S3存储桶中的压缩zip文件通过一层添加到lambda),但是当我尝试运行此代码时,我得到了一个奇怪的错误:
I have an AWS Lambda that I want to connect to my on prem SQL server to read and write data from\to. I am using Python and pyodbc. I have got pyodbc installed (compiled zip file in an S3 bucket added to the lambda through a layer), but when I try and run this code I get an odd error:
import boto3
import pyodbc
s3 = boto3.client('s3')
def lambda_handler(event, context):
# print(help(pyodbc))
server = "Server"
database = "Database"
username = "AWS-Lamdba-RO"
password = "Password"
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
这是错误:
[ERROR] AttributeError: module 'pyodbc' has no attribute 'connect' Traceback (most recent call last): File "/var/task/lambda_function.py", line 13, in lambda_handler cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
我在网上发现的所有对象都是无法首先安装pyodbc库的人,因此,越过了那个症结,我以为我是自由而明确的.谁能解释我现在遇到的问题?
All I'm finding online is people who are unable to get the pyodbc library installed in the first place, so having got past that sticking point I thought I was free and clear. Can anyone explain what I've run into now?
我从这里得到了pyodbc: https://github.com/Miserlou/lambda-packages/tree/master/lambda_packages/pyodbc
I got pyodbc from here: https://github.com/Miserlou/lambda-packages/tree/master/lambda_packages/pyodbc
AWS无法识别.tar.gz文件,因此我将其更改为zip文件,还添加了文件夹结构,另一个谷歌网站告诉我这是必要的: \ python \ lib \ python3.7 \ site-packages \ pyodbc 该文件夹包含: libodbc.so.2 pyodbc.so
AWS didn't recognise .tar.gz files, so I changed it to a zip file and also added in the folder structure which another googled site told me was necessary: \python\lib\python3.7\site-packages\pyodbc that folder contains: libodbc.so.2 pyodbc.so
我将此Zip文件上传到S3存储桶,并在其中指向Lambda层.
I uploaded this Zip file to an S3 bucket and pointed a Lambda layer at it.
我对此做了些愚蠢的事吗?
Have I done something silly with this?
推荐答案
根据您的描述,我认为您可能误解了文件夹结构.
From your description, I believe you may have gotten your folder structure wrong.
如果要查看zip文件,应该看到以下结构:
If you were to look in your zip file, you should see the following structure:
layerZip.zip
└ python
└ lib
└ python3.7
└ site-packages
└ pyodbc
但是我相信你可能真的有
But I believe you may actually have
layerZip.zip
└ \
└ python
└ lib
└ python3.7
└ site-packages
└ pyodbc
但说实话,只需使用以下结构:
But honestly, just use this structure:
layerZip.zip
└ python
└ pyodbc
它也将正常工作,只是将模块设置为全局模块,而不是每个用户.
It'll work just as well, it's just setting the module up as global instead of per user.
这篇关于AWS Lambda使用Python和Pyodbc连接到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!