Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run SQLAlchemy on AWS Lambda in Python

I preapre very simple file for connecting to external MySQL database server, like below:

from sqlalchemy import *

def run(event, context):
    sql = create_engine('mysql://root:[email protected]/scraper?charset=utf8');
    metadata = MetaData(sql)

    print(sql.execute('SHOW TABLES').fetchall())

Doesn't work on AWS, but localy on Windows works perfectly.

Next, I install by pip install sqlalchemy --target my/dir and prepare ZIP file to upload packages to AWS Lambda.

Run, but with failed message No module named 'MySQLdb': ModuleNotFoundError.

Then, I use pip install mysqlclient --target my/dir, create ZIP and again upload to AWS Lambda.

Run, but with new failed message cannot import name '_mysql': ImportError.

So, what I should doing now?

like image 873
kicaj Avatar asked Feb 08 '19 00:02

kicaj


People also ask

Does SQLAlchemy work with AWS?

SQLAlchemy is compatible with all these engines. For this post, we provision an Aurora PostgreSQL database. To deploy the database for our sample application, follow the instructions in Deploy an Amazon Aurora PostgreSQL DB cluster with recommended best practices using AWS CloudFormation.

Is SQLAlchemy built in package in Python?

SQLAlchemy is a popular SQL toolkit and Object Relational Mapper. It is written in Python and gives full power and flexibility of SQL to an application developer. It is an open source and cross-platform software released under MIT license.

Should I use SQLAlchemy core or ORM?

If you want to view your data in a more schema-centric view (as used in SQL), use Core. If you have data for which business objects are not needed, use Core. If you view your data as business objects, use ORM. If you are building a quick prototype, use ORM.

Is SQLAlchemy part of Python?

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.


1 Answers

SQLAlchemy includes many Dialect implementations for various backends. Dialects for the most common databases are included with SQLAlchemy; a handful of others require an additional install of a separate dialect.

The MySQL dialect uses mysql-python as the default DBAPI. There are many MySQL DBAPIs available, including MySQL-connector-python and OurSQL

Instead of mysql you may use mysql+mysqlconnector

sql = create_engine('mysql+mysqlconnector://root:[email protected]/scraper?charset=utf8')

Then use:

pip install mysql-connector --target my/dir

Create Zip and again upload to AWS Lambda.

like image 170
I Bajwa PHD Avatar answered Sep 18 '22 03:09

I Bajwa PHD