简体   繁体   中英

no module name pyodbc in aws lambda

Trying to connect my mssql with aws lambda don't know how to install these libraries there

import json
import urllib.parse
import boto3
import pyodbc


def lambda_handler(event,context):
    s3=boto3.client('s3')
    if event:
        connection = 'DRIVER={SQL Server};SERVER=adasdst-2.rds.amazonaws.com,1433\SQLEXPRESS;' 'DATABASE=asd;''UID=asd;PWD=asd'
        conn = pyodbc.connect(connection)
        cursor = conn.cursor()
        print(conn)

error

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'pyodbc'
Traceback (most recent call last):

anykind of help will be appreciated

在此处输入图像描述

Install the pyodbc package next to your module so that your script will be able to locate the dependency python -m pip install --target./ pyodbc . Here is the AWS doc: https://docs.aws.amazon.com/lambda/latest/dg/python-package-create.html See the section "Create a function with runtime dependencies".

Open a terminal and create a source_code directory.

mkdir my_lambda_source

cd my_lambda_source

Copy the contents of your code into lambda_function.py

import json 
import urllib.parse
import boto3
import pyodbc


def lambda_handler(event,context):
    s3=boto3.client('s3')
    if event:
        connection = 'DRIVER={SQL Server};SERVER=adasdst-2.rds.amazonaws.com,1433\SQLEXPRESS;' 'DATABASE=asd;''UID=asd;PWD=asd'
        conn = pyodbc.connect(connection)
        cursor = conn.cursor()
        print(conn)

Install the required libraries to a new package directory.

pip3 install --target./package boto3

pip3 install --target./package pyodbc

If you are getting following errors while installing pyodbc then You need a driver manager. For Ubuntu run sudo apt-get install unixodbc-dev on RedHat it would be sudo yum install unixODBC-devel

In file included from src/buffer.cpp:12:
src/pyodbc.h:56:10: fatal error: sql.h: No such file or directory
   56 | #include <sql.h>
      |          ^~~~~~~
compilation terminated.

goto package directory zip your libraries into

zip -r ../my-deployment-package.zip .

This generates a my-deployment-package.zip file in your project directory my_lambda_source

Add the lambda_function.py file to the root of the zip file.

cd ..
zip -g my-deployment-package.zip lambda_function.py

Now upload my-deployment-package.zip file to lambda and test you code. It should work fine.

Refer. Creating a Lambda function with runtime dependencies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM