Rust crate or example for AWS lambda or Netlify Functions
I'm using Netlify Functions (AWS lambda with Rust) to connect to Neon (postgres) and have it working fine.
The only issue I had was to have to config (Fixed: Not sure if it was due to moving my lambda region to match the region I'm using for Neon or updating the node version of the lambda build).
Questions:
- Is there an official rust serverless crate for AWS lambda provided by Neon I should use?
- If there isn't a crate, do you have an example of an AWS lambda endpoint using Rust?
Note: I plan to post an example once I get pooling working. See Pooled Gist below (recommended)
tokio-postgres to use IPv4 due to an error ("Address family not supported by protocol"), but want to use the host instead. I'm sure the IP address could change at any time and will break eventually.6 Replies
stormy-goldOP•11mo ago
AWS Lambda example without connection pooler. TODO: User Verification
stormy-goldOP•11mo ago
Here's a Gist because I had to break up code here: https://gist.github.com/talves/92070c8320f7c5c35c3bc640698e9aec
Gist
AWS Lambda in Rust example for Postgres on Neon (any really)
AWS Lambda in Rust example for Postgres on Neon (any really) - Cargo.toml
fair-rose•11mo ago
Thank you for sharing the example!
stormy-goldOP•11mo ago
@Mahmoud No problem. I have some more to share, got a pooled connection working also (recommended).
stormy-goldOP•11mo ago
Here is the Gist for the pooled connection: https://gist.github.com/talves/cacd40fe705e4872a6ec0bbeaf3767c3
I used the
diesel, diesel-async crates with bb8 pool manager. This lambda function Works with Netlify functions and should work as an AWS lambda function also (not tested yet). Happy New Year!Gist
AWS Lambda in Rust example for Postgres (any) on Neon using Pooled ...
AWS Lambda in Rust example for Postgres (any) on Neon using Pooled connections. Works on Netlify functions. - Cargo.toml
stormy-goldOP•11mo ago
I'm getting some really fast lambda calls to Neon (about 50-100ms) which was very nice to know. These will slow with data and session verification, but was a really good start for my proof of concept.
There are improvements that can be made on using the
lambda_runtime for rust, but this is a great start for anyone who wants to use it. Couldn't get axum routing to work for Netlify functions, but should work on AWS lambda if you use lambda_http instead. If using AWS lambda, there are some good examples that helped me get started with these in the aws-lambda-rust-runtime repo. The examples in the repo are a little outdated, because crates in this area are moving fast, but as of this post, my connections using rustls, tokio_postgress and tokio_postgres_rustls is the most current.