Video není dostupné.
Omlouváme se.
Rust On AWS (Really FAST Lambdas)
Vložit
- čas přidán 17. 08. 2024
- Learn about why you should consider the Rust language when using AWS Lambdas, especially when you need to minimize cold start times.
We walk through using the Rust AWS SDK and creating a Rust based Lambda.
Code can be found here: github.com/Me1...
00:00 Why Rust On AWS
00:42 Creating a Rust Lambda Project
01:20 Dependencies
02:54 Use Statements
03:29 Main Function
04:59 CustomEvent Struct
05:25 Lambda Handler Function
06:02 DynamoDB with Rust
06:50 Handler Return Value
07:07 Building For Lambda
07:50 Build Shell Script
09:36 Uploading To The AWS Console
09:55 Invoking The Lambda
10:20 Conclusion
As I write this, a solitary tear trickles down my cheek. Thanks so much for doing this video, I sorely needed it. I'm running Python Lambdas all day and the slow speed is contributing to long run times and significant costs. I've just got to move to a faster language and was really considering Node but Rust seems like a much more future aware move. That being said, knowing you need to move to Rust and actually writing code in Rust are vastly different things, so thanks for this explainer.
Best comment ever LOL! Very happy that you found the video valuable, let us know how the migration to Rust goes if you decide to go that route!
Great production level of the video, really appreciate the effort put in. And your teaching style is really good! Hope to see more Rust videos.
Thank you and thanks for watching! I'm pretty excited about Rust as well!
I accidentally came across your channel in the recommendations, I was very surprised that with such a high quality of content you have so few subscribers. Your Rust lessons are some of the best on CZcams, I wish you success!
Thank you so much, I'm really happy it was recommended to you! More to come....
@@codetothemoon Can't wait to see! I can suggest an idea - why not to make a video about creating a whole website using Rust - i.e. write a Backend on something like Actix Web or Tokio, and write a Frontend on Yew or any other framework, that can provide easy UI creation and can be compiled down to Webassembly?
You have to remind people/ask for people to subscribe or they won’t think about it, they’re going to be thinking about the content of the video.
This is a great straightforward tutorial, I think a great way to improve it is to show the end result before starting the project (so we see better what is the problem we're trying to solve)... I know little to nothing about both rust and AWS (I might not exactly be the target audience) but it would help understand a few of the steps along the way
Thank you, and this is great feedback! I think it makes sense to show the end result in the beginning, even for folks that may be familiar with Rust and AWS.
Definitely a much better guide than the official AWS rust lambda guide. Regarding al2build function, here's another docker based solution using cross: cross build --release --target aarch64-unknown-linux-gnu
Thanks, glad you found it helpful! re: cross build, interesting I'll give that a try!
This tutorial was great and helped me get started with Rust. It would be cool to see how you test this locally before deploying to AWS.
Glad you liked it! and good idea - typically the go-to way for testing locally would be via AWS SAM, but I haven't tried it for a Rust based lambda
@@codetothemoon with CDK's "new" hot swap for lambda functions, combined with streaming cloudwatch logs to your terminal for the function while it's in hot swap mode, it's getting easier to ignore local testing. Would be cool to see a video from you on that.
Awesome video. Clear explanation of all the tricky parts and needed scaffolding code. I can use this information immediately to start experimenting.
Nice Rich, really happy you found the video valuable!
Thank you. Your teaching style is great. Your videos are short and to the point. Awesome!
Thanks Adam! More to come!!
This was great! Your explanation was to the point and the topic of Rust and AWS was interesting. You have yourself a subscriber :)
Thank you, very happy to have you onboard!!!
Great stuff!
My one wish would be scaling the screen to be equal to the maximum line length (or using new lines in between method calls to shorten the longer lines), it makes it v hard to fully absorb the longer lines of code when they cant be read
Thanks MrTickely - thats a great idea - just the sort of feedback I'm looking for! It's probably frustrating when you want to see something that's just slightly off screen....
0:20 look python thou i didn't expect that
yeah odd how it seems to have an advantage at 128mb, didn't see this as relevant in most real world scenarios as Lambda doesn't give you much CPU with that amount of memory
I know almost no Rust and still felt like I could get the gist of this. Thanks!
nice! I do try to make these as accessible as possible to those not familiar with Rust - glad I succeeded here!
I love this! Thank you for making a video that covers a practical use case.
Thanks, glad you found it helpful!
Really nice explanations of each piece of code. Thanks a lot!
Glad you found it valuable, thanks for watching!
Great video and tutorial in general. Subscribed. I will definitely like to see more rust content in the future because there are limited video resources for learning Rust, which makes it harder for newer developers to get into.
Thanks, great to have you onboard! I'm really enjoying Rust, so there's definitely more to come....
You can write once, run anywhere with Rust.
Not sure how production ready that is, but it does the simpler things fine.
It's called WASM+WASI.
Great point! I'm very interested in applications of Rust in the WASM space, and I plan to explore this - there'll likely be a video about it!
@@codetothemoon I do hope that this will liberates from the need for proprietary platforms like the JRE per language (or that other languages have to adapt, often forcing them to conform to bad design choices).
We will see however. It has to be said, that this approach requires (or at least much prefers) a small runtime by the language targeting wasm. Languages with a larger runtime will suffer from large binaries.
Probably the reason the available material does recommend Rust or C(++).
Doesnt rustup support the necessary cross-compilation natively, without the need for Docker? rustup target add or stg like that, no need for Docker stuff.
Not that it's not nice to know, but it does let rust look worse than it really is.
It does, but there appears to be a problem when cross compiling with the aws-lambda-rust-runtime library on the Mac. See the Building on MacOS Using Docker section here github.com/awslabs/aws-lambda-rust-runtime/blob/master/README.md. I tried it without Docker and did indeed run into an error. Googling the error seemed to indicate that the problem is related to specific versions of dependencies not being available, but I didn't dig any deeper at that point.
I just went through this process in the last couple of days on MacOS as well. I use cargo to build using x86_64-unknown-linux-musl target. It was pretty straightforward. I did spend a lot of time setting up the configuration which doesn't appear to exist in this video though :) I found that Terraform+rust was the path of least resistance. Two days from scratch to a severless API with lambda and API gateway.
The fireship editing inspiration does it for me. Def getting the sub just for that.
Thanks Khari!! You've correctly identified my biggest source of inspiration! His newer one about creating a stock trading app is hilarious.
@@codetothemoon definitely one of my favorite coding creators for sure
good video! especially liked the attention to detail of speeding up the non speaking parts
Thanks, glad you enjoyed it!
Great tutorial with comparisons to JS
thanks, glad you found it valuable!
Guarantee it will be 100x faster if you deploy the rust code as a wasm module to cloudflare workers. Cloudflare workers have a sub millisecond cold startup time and are globally distributed by default
Drew, would this also eliminate the need for Docker?
Interesting, Cloudflare wasn't on my radar, I'll have to check it out!
great thing about that is, Rust already has a wasm toolchain.
A fair warning: as of June 2022, Cloudflare Workers work with Rust/WASM, but do not allow Rust connections to cloud MongoDB Atlas (which includes a free tier), due to missing "MongoDB App Services" SDK for Rust. The cloud database directly on Cloudflare is Cloudflare KV (key-value), which, under the free tier, allows only 1,000 write requests per day (less than 1 write request per minute).
Awesome content!
Maybe you can make a follow-up where you use this lambda code in a cdk project
Thank you, and that's a great idea!
Thanks for the video!
Lambda functions are completely new to me so this was a nice intro to it!
Can you try to write the same lambda function in different languages like JS, Python, Go etc, deploy them and then compare the performance (to really see how fast Rust is in comparison to others)?
You're very welcome, I didn't give much context around Lambda itself so it makes me happy to know that it is useful for Lambda newbies as well!
Re: your benchmark comparison request, actually this is a fantastic blog post that does just that! filia-aleks.medium.com/aws-lambda-battle-2021-performance-comparison-for-all-languages-c1b441005fd1
Nice video. Can you show us the init duration of the lambda during cold starts?
Thanks, glad you liked it! Check out Aleksandr Filichkin's blog post! filia-aleks.medium.com/aws-lambda-battle-2021-performance-comparison-for-all-languages-c1b441005fd1
How was cargo already available in the container when he built it? Does arch have cargo preinstalled?
when I zip the file with the docker script after building with the al2build function, It gives me a no such file or directory error. Can anyone help me out with this?
hey Michael - what file is it looking for that it can't find?
Is this in a VPC so the db is not exposed to the outside world? Last time I checked that requires a minimum $30/mo investment due to the need of an NAT gateway, if you need your function to access the internet.
I write a tonnnnn of lambas in my job and would love to give rust a shot. Where did you learn all the semantics of getting rust up and running in Lamba? I’m not seeing a lot of docs from them on it
yeah docs are a bit sparse, which is part of the reason I created this video. I thought this would certainly have been addressed in the year since I created the video, but maybe not. Here's a promising looking guide docs.aws.amazon.com/sdk-for-rust/latest/dg/lambda.html
@@codetothemoon thanks you’re the man!
Great video! Just would really like to see the performance numbers.
Thank you! I didn't actually do any benchmarking myself - check out Aleskandr's blog post! filia-aleks.medium.com/aws-lambda-battle-2021-performance-comparison-for-all-languages-c1b441005fd1
Very good video, I really enjoyed, you have a new Subscriber!
Thanks and welcome aboard, very happy to have you!
Is there any full out courses around that teaches one how to use Rust with AWS?
Fantastic tutorial! Subscribed
Thank you and thanks for watching! More to come.
Very nice explanation, great video
Thank you, glad you liked it!
I started testing rust SDK. I must say it's still quite unstable compared to other languages SDK and getting things done in rust is not so easy or straightforward but I think it's worth a try
your videos are really excellent
Thank you! More to come…
Cool video, rust seems like a cool language to pick up. I'm doing all my API work in python at the moment but would like to try Rust at some point.
Nice! Yeah there's a bit of a learning curve but the performance advantage and type system is pretty compelling. If you decide to try it, definitely let us know how it goes!
@@codetothemoon thanks, nice production value and content 👍
A while ago I wrote a digit recognition machine learning console application in Rust that was a copy of a .NET application from a book. I had also previously written a Python version of it. I then compared timings. This is what I found.
Python - 22s
C# - 1.03s (.NET 4.8)
C# - 0.52s (.NET 6.0)
Rust Debug - 5.72s
Rust Release - 0.28s
(Rust release builds are much faster than debug builds. A release build typically runs much faster than a debug build. 10-100x speedups over debug builds are common!)
So it will be interesting to see what happens when you redo your work in Rust. As this is 9 months later perhaps you've progressed since?
Question ... can't you just add a target for AWS's Linux and compile to that target instead of whipping out Docker? I'd have thought at least the M1 apple silicone (or other ARM arch) would be able to do this.
oops ... I see that this question was already asked and discussed right below my scroll point in the questions . :)
np, hoping we don't have to rely on docker to do this in the future!
I liked the default_provider().or_elase() command
definitely nice to be able to do this in one line!
@@codetothemoon lol, I was actually commenting on the typo :P but yes it is convenient that this is a one liner
Thanks!
thanks for watching!
Any chance of benchmarking rust lambdas against other languages?
Aleksandr Filichkin made a blog post about this! Check it out here filia-aleks.medium.com/aws-lambda-battle-2021-performance-comparison-for-all-languages-c1b441005fd1
@@codetothemoon thanks, I didn't catch the name as you mentioned it ...
That's a cool comparison. I wish they didn't use such old C# and Node versions though.
The post "clearly shows rust as the best performer" ?
Isn't the conclusion of the post saying : "Golang and Rust are the winners. They have the same brilliant performance." ?
Go seems to provide great performance as well! It looks like they are neck and neck for warms starts, but most languages seem to be in the same ballpark there. Usually the sticking point in considering Lambda over other compute solutions is cold start performance, and Rust seems to have a non-negligible advantage over Go there. Probably hard to go wrong with either language though!
Great video, definitely subscribing -- the example seemed a bit useless though 😅 writing to a database seems way too simple of a function to me
Thank you and thanks for the feedback! I'm looking forward to sharing something a bit more substantial on here - in the meantime, I'd love to hear some success (or failure!) stories incorporating Rust Lambdas into a larger system.
Very nice tutorial! I am pretty confident that I configured AWS Lambda and DynamoDB correctly, including the necessary permissions. I still get an Error from AWS Lambda that GLIBC is not found. :(
Hi! I used a slightly different method that seems to work. If you have a problem with `cross build` you may need to add `docker` to a group to avoid using `sudo` (if on linux).
Don't forget to `source *.bash` (or *zsh) before calling the functions.
Also do not forget to create a DynamoDB table named `users` with a partition (primary) key as `uid`, the code does not create the table automatically. Finally, create a lambda function with custom runtime `Custom runtime on Amazon Linux 2`.
Hope it helps!
# deploy.bash or deploy.zsh
# source *.bash or *.zsh
RUST_TARGET="x86_64-unknown-linux-musl" # should solve the GLIBC problem
PROJECT_NAME="rust-aws"
build() {
cargo install cross \
&& cross build --release --target ${RUST_TARGET}
}
package() {
cp target/${RUST_TARGET}/release/${PROJECT_NAME} ./bootstrap \
&& zip lambda.zip bootstrap \
&& rm bootstrap
}
Thanks! Did you build using the Docker approach? I'm wondering if Lambda changed the custom runtime since this video was created...
@@codetothemoon I actually did not use Docker, I directly uploaded the .zip file to AWS lambda!
I'm having the same problem
@@amaurymaillard5284 this fixed it for me. Thank you for sharing.
i did a double take when i saw "43 subscribers" lmao
Hopefully this means you were number 44? :) if so, very happy to have you on board!
@@codetothemoon of course!
Subscribed.
Awesome, really happy to have you onboard!
Great stuff! How do you run this locally like serverless-offline?
Thank you! Great question, I haven't actually explored this in a Rust context, in other languages I've used AWS SAM, not sure if that is actually an option for Rust Lambdas. Worth looking into though.
Thanks for the video, excellent as always!
I'm getting an error when running the function on AWS:
/var/task/bootstrap: /lib64/libc.so.6: version `GLIBC_2.29' not found (required by /var/task/bootstrap)
/var/task/bootstrap: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by /var/task/bootstrap)
It seems that has something to do with the latest rust versions using GLIBC, which al2 doesn't support.
I hate to punt on this, but I haven't run this in awhile and I'm wondering if AWS changed the linux runtime in some way. Are you building it using Docker?
@@codetothemoon Yes, exactly same process as in your video. It seems to be something with the newer rust version. I think in your video you were using 1.57.0 and I'm using 1.64.0, don’t know if there's a compatibility issue with the glibc version provided by al2.
This is cool, but why can't AWS just build it for you and run it in whatever arch they see fit? This kind of things is a bit why no one really uses AWS directly. 😅
I agree that would be really nice! Hoping to see more and more Rust support in AWS, starting with a built-in Lambda runtime...
Hey, couldn’t you just compile right into the architecture instead of using dockers?
Liked and subscribed.
Thanks Nathan, great to have you here!
I really like rust but I find the code to be extremely verbose, kinda hoping someone makes a language using rust that’s a bit more abstracted but allows you to use some of the really nice features
I agree! There are a ton of compelling reasons to use Rust at the moment, but I think the perfect language still remains elusive.
Depending on the kind of applications you want to write you might find F# more attractive if verbosity is a major issue for you. It overlaps a bit in the syntax and features, as both languages draw from ML. F# is also now cross-platform, since .NET is.
pretty sure --bin is the default behavior of cargo. No need to type it out😀
Ohh nice, I'm going to try omitting it next time!
aws can scale. why would you save their resources if you'll pay the same anyway
Lambda charges based on (number of executions + GB/second of runtime). Since Rust generally achieves better performance than other languages given the same allocation of memory (which also determines CPU resource allocation), you can choose to pay the same and have faster APIs, or pay less money (by allocating less memory) and get similar performance.
@@codetothemoon fair enough, I didn't know about that GB/second thing
I mean no offense to anyone, but discovering that ahead of times compiled languages who have minimal runtimes, and no garbage collection, supported by Optimizing compilers and optimizing linkers, are faster than interpreted/JIT compiled garbage collected languages, is not news.
That fact hold true on most operations in AWS lambda and elsewhere…
None taken, thanks for watching!
You deserve moar subs.
Have mine~
Thank you, and very happy to have you onboard!
This aims to be a Rust on AWS tutorial, but you fast forward everything both Rust and AWS related.
The intent is appreciated, but how is someone to learn something from this tutorial? It’s basically a code snippet in a video format…
Thanks for the feedback! How could I change the format to make it more valuable? Less fast forwarding and more explanation of what I'm doing?
@@codetothemoon That’s a tough one, because I assume you also wanted to keep it concise.. Easier to critic than give easy to follow suggestions. If you really want to hear I’d be happy to attempt to give constructive suggestions over a chat session, comments are.. too concise haha
Your thumbnail is against foundation policy 😂
My recommendation would be to stop fast forwarding when writing the code, some areas where you were writing functions I wasn't familiar with the fast forwarding made it extremly difficult to follow and required me to rewind multiple times.
Thanks Anthony, i'm glad you brought this up - Ive been on the fence about the fast forwarding, as like you said it's probably a bit jarring. I'll tone it down a notch!
@@codetothemoon also your face kinda covered bits and pieces of code, but I guess having the GitHub link is good enough in the end! Thanks for the video
Disliked because you have multiple coding errors like not actually defining uuid and misspelling results, and you didn't show fixing those or that they were mistakes at all, and that might confuse newcomers.
Edit: I should have made it clear before, but I did love the video for showing off that rust can do lambda. Thanks for the video regardless!
Thanks for the feedback Daniel! Sorry about those mistakes, I hope you got some value from the video despite them. It sounds like you figured out the corrections yourself already, but you can find the final code here if you like github.com/Me163/youtube/tree/main/RustOnAWS. Cheers!
> alex jones
nice
alex jones?
@@codetothemoon At the end near 10:20 :)
ohh yeah I chose that name completely at random (as i did for bobby smith), is that an actual famous person?
Rust n00b here. Wouldn't `request.send().await?` actually be anync and non-blocking? However, since its lambda you aren't actually freeing up the "thread" and still charged until the "Ok()" call as if we were blocking? IOW: aws gets the resources you freed up instead of the CPU in an EC2 instance, no?
I *think* send() is async and "await" causes it to block, so yes the thread should yield to any other tasks that need to be performed, and in this case there aren't any. So yeah Lambda would still charge us for runtime while we're waiting for the DDB response. Would be great if there was a way around this!
@@codetothemoon Also a lambda n00b but i think you'd effectively have to use "step functions" or you'll pay for the lambda that is "orchestrating" the calls while it waits for the response.
Where this get even trickier (to me) is lambda might reuse your context. Does that mean it effectively hibernates deamon threads between requests when it suspends you?
lambda is fast ok but cold start exceeds easily 3 seconds
it's very abnormal to have a cold start exceeding 3 seconds, unless maybe you're using the Java runtime and your package is huge - like 100s of megabytes or gigabytes. Can you share more details about your specific setup?