简体   繁体   English

使用 AWS Lambda 将数据保存到 PostgreSql 数据库

[英]Saving data to a PostgreSql database using AWS Lambda

I am new to using AWS, and I am working on converting an ASP.NET application to a serverless architecture.我刚开始使用 AWS,我正在努力将 ASP.NET 应用程序转换为无服务器架构。 One of the Lambda functions that I am trying to write is take an object and save it to a PostgreSQL database.我尝试编写的 Lambda 函数之一是采用 object 并将其保存到 PostgreSQL 数据库中。 Normally, I do this using Entity Framework, but seeing as to the limit in scope with Lambda functions, I am not sure how to use EF, if possible, with a Lambda function. Does anyone know if this is possible, and if so, how to use EF in a Lambda function?通常,我使用实体框架执行此操作,但看到 scope 和 Lambda 函数的限制,我不确定如何使用 EF,如果可能的话,Lambda function。有谁知道这是否可能,如果可以,如何在 Lambda function 中使用 EF?

I had a thought of trying to build a library hosted on CodeArtifact and import the models and context classes, but not sure if that is good or viable idea or now.我曾想过尝试构建一个托管在 CodeArtifact 上的库并导入模型和上下文类,但不确定这是好主意还是现在可行。 Thoughts on that?对此有何想法? Or would anyone recommended a different approach?或者有人会推荐不同的方法吗?

Edit: If you can't use EF in Lambda, any recommendations on best practice for using Lambda to send data to a database?编辑:如果您不能在 Lambda 中使用 EF,关于使用 Lambda 将数据发送到数据库的最佳实践有什么建议吗?

Thanks!谢谢!

In case anyone is wondering the same thing I asked earlier, I just finished working this and found a solution.如果有人想知道我之前问过的同一件事,我刚刚完成这项工作并找到了解决方案。 This is what I did, hopefully this helps!这就是我所做的,希望这会有所帮助!

  1. First I built a helper library that contained the models and DbContexts that I wanted to use in my Lambda function. I did it this way because I want to use the same models across a few other Lambda's in the future.首先,我构建了一个帮助程序库,其中包含我想在我的 Lambda function 中使用的模型和 DbContext。我这样做是因为我想在未来的几个其他 Lambda 中使用相同的模型。 I published it as a local NuGet package. This will eventually be stored in AWS Code Artifact.我将它发布为本地 NuGet package。这最终将存储在 AWS Code Artifact 中。

  2. Started a new Lambda project in VS2019 using the AWS Toolkit, using the .NET5 Container image template.使用 AWS 工具包在 VS2019 中启动一个新的 Lambda 项目,使用 .NET5 容器图像模板。 I imported the local NuGet helper library into the project.我在项目中导入了本地的NuGet helper库。

  3. I set up the DbContext on the library with a constructor to take a connection string (this is imported from a JSON stored on an S3 bucket).我使用构造函数在库上设置 DbContext 以获取连接字符串(这是从存储在 S3 存储桶上的 JSON 导入的)。 In the Lambda, I call the API like you normally would, parse the response into the DbContext models and then save the changes.在 Lambda 中,我像往常一样调用 API,将响应解析为 DbContext 模型,然后保存更改。

  4. After uploading the completed Lambda, I use an EventBridge to schedule the Lambda to fire once every 24 hours.上传完成的 Lambda 后,我使用 EventBridge 安排 Lambda 每 24 小时触发一次。

Let me know if you need more details, and I can try and write out a longer explanation.如果您需要更多详细信息,请告诉我,我可以尝试写出更长的解释。

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

相关问题 是否可以使用 AWS Lambda 连接到外部 postgresql 数据库? - Is it possible to use AWS Lambda to connect to an external postgresql database? 如何从 Django 项目的 AWS PostgreSQL 数据库中恢复已删除的数据? - How to recover deleted data from AWS PostgreSQL database for Django project? AWS Lambda 为 postgreSQL 运行 NodeJs - 超时错误 - AWS Lambda functions NodeJs for postgreSQL - timeout error 数据库连接挂在 AWS Lambda 上 - Database connection hanging on AWS Lambda CI/CD、Terraform 和 AWS ECS:使用 Lambda 应用数据库迁移? - CI/CD, Terraform and AWS ECS: Applying database migrations using Lambda? 在 AWS Lambda 上的 python 中使用枕头 package 保存图像时出现错误/错误的内容类型 - Error/Wrong Content-Type while saving image using pillow package in python on AWS Lambda 让 AWS lambda 访问 EC2 上的 PostgreSQL - Give AWS lambda access to PostgreSQL on EC2 AWS Lambda 与 RDS Postgresql - 未知问题 - AWS Lambda with RDS Postgresql - Unknown Issue AWS Kinesis Data Firehose 和 Lambda - AWS Kinesis Data Firehose and Lambda pandas dataframe 到 aws postgresql 使用 sqlalchemy 不将数据注入表 - pandas dataframe to aws postgresql using sqlalchemy not injecting data to table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM