简体   繁体   中英

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. One of the Lambda functions that I am trying to write is take an object and save it to a PostgreSQL database. 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?

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. 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?

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. I published it as a local NuGet package. This will eventually be stored in AWS Code Artifact.

  2. Started a new Lambda project in VS2019 using the AWS Toolkit, using the .NET5 Container image template. I imported the local NuGet helper library into the project.

  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). In the Lambda, I call the API like you normally would, parse the response into the DbContext models and then save the changes.

  4. After uploading the completed Lambda, I use an EventBridge to schedule the Lambda to fire once every 24 hours.

Let me know if you need more details, and I can try and write out a longer explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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