Is it a good idea to use Python SQLAlchemy in AWS Lambda?

Viewed 8438

Amazon suggests to not include big libraries/dependencies in lambda functions. As far as I know, SQLAlchemy is quite a big python library. Do you think it is a good idea to use it in lambda functions? An option would be to include it as a Lambda Layer and use it across all related Lambda functions.

Anyways, what is the best practise?

3 Answers

Serverless functions are meant to be small self-contained functions. SQLAlchemy is an ORM, which allows you to manipulate database objects like objects in python. If you're just writing a few serverless functions that do you're average CRUD operations on a database you're better off writing the SQL by composing the strings and directly executing that through your database driver (which you'll have to install anyways, even if you're using sqlalchemy). If you're building your own framework on top of AWS Lambda then perhaps consider sqlalchemy.

From what I read, SQLAlchemy performs in memory caching of data it has read and uses that for future calls. Based on what you are doing, it would be good to check out the SQLAlchemy caching strategy so another Lambda does not change the data from under the first lambda with SQLAlchemy.

  • Do you think it is a good idea to use SQLAlchemy in lambda functions?

Yes but you might want to consider AWS Dynamodb or other options first. If SQL is your requirement then you can go for an RDS instance with an SQL compatible backend. It all depends on your specific needs in this case. It is not a general requirement.

  • Anyways, what is the best practice?

If you just need to store some data and connect the data storage to AWS Lambda then DynamoDB is usually a good choice. SQL can be preferred if you already have a relational database or if you need to perform certain SQL queries which are easier to do with SQL than a non-relational model.

Related