0

I am trying to connect to AWS RDS SQL Server instance to query table from AWS Lambda using python script. But, I am not seeing any AWS api so when I try using "import pyodbc" seeing the below error.

Unable to import module 'lambda_function': No module named 'pyodbc'

Connection:

 cnxn = pyodbc.connect("Driver={SQL Server};"
                        "Server=data-migration-source-instance.asasasas.eu-east-1.rds.amazonaws.com;"
                        "Database=sourcedb;"
                        "uid=source;pwd=source1234")

Any points on how to query RDS SQL Server?

3 Answers 3

2

The error you're getting means that the lambda doesn't have the pyodbc module.

You should read up on dependency management in AWS Lambda. There are basically two strategies for including dependencies with your deployment - Lambda Layers or zip with the deployment package.

If you're using the Serverless Framework then Serverless-python-requirements is an excellent package for managing your dependencies and lets you choose your dependency management strategy with minimal changes to your application.

Sign up to request clarification or add additional context in comments.

Comments

1

you need to upload the dependencies of the lambda along with the code. If you deploy your lambda manually (i.e. create a zip file / right from the console), you will need to attach the pyodb library. (More information is available here: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-dependencies).

If you're using any other deployment tool (serverless, SAM, chalice), it will be much easier: https://www.serverless.com/plugins/serverless-python-requirements, https://aws.github.io/chalice/topics/packaging.html#rd-party-packages, https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build.html

Comments

1

Here's my github repo with automated builds of the the ODBC 18 drivers for Python 3.11 and 3.12. Prebuilt packages and Dockerfile for x86_64 and ARM64

https://github.com/davidcomerford/pyodbc-mssql-lambda-layer

I like to pull the latest version of unixodbc from Amazons repo. This feels a lot safer than everyone else's fixed versions, security, bugfixes, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.