简体   繁体   English

在 AWS Lambda 中使用 Python SQLAlchemy 是个好主意吗?

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

Amazon suggests to not include big libraries/dependencies in lambda functions.亚马逊建议不要在 lambda 函数中包含大型库/依赖项。 As far as I know, SQLAlchemy is quite a big python library.据我所知,SQLAlchemy 是一个相当大的 python 库。 Do you think it is a good idea to use it in lambda functions?您认为在 lambda 函数中使用它是一个好主意吗? An option would be to include it as a Lambda Layer and use it across all related Lambda functions.一种选择是将其包含为 Lambda 层,并在所有相关的 Lambda 函数中使用它。

Anyways, what is the best practise?无论如何,最好的做法是什么?

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. SQLAlchemy 是一个 ORM,它允许您操作数据库对象,如 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).如果您只是编写一些无服务器功能,那么您是数据库上的平均 CRUD 操作,那么您最好编写 SQL 通过组合字符串并通过数据库驱动程序直接执行它(无论如何您都必须安装它) ,即使您使用的是 sqlalchemy)。 If you're building your own framework on top of AWS Lambda then perhaps consider sqlalchemy.如果您在 AWS Lambda 之上构建自己的框架,那么也许可以考虑 sqlalchemy。

  • Do you think it is a good idea to use SQLAlchemy in lambda functions?您认为在 lambda 函数中使用 SQLAlchemy 是个好主意吗?

Yes but you might want to consider AWS Dynamodb or other options first.可以,但您可能需要先考虑AWS Dynamodb或其他选项。 If SQL is your requirement then you can go for an RDS instance with an SQL compatible backend.如果 SQL 是您的要求,那么您可以 go 用于具有 SQL 兼容后端的RDS实例。 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.如果您只需要存储一些数据并将数据存储连接到 AWS Lambda,那么 DynamoDB 通常是一个不错的选择。 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. 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.

From what I read, SQLAlchemy performs in memory caching of data it has read and uses that for future calls.根据我的阅读,SQLAlchemy 在 memory 缓存它已读取的数据并将其用于将来的调用。 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. 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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