JK, I hear you wanted to run some Rust
code in the cloud without having to manage servers.
Yes, that's right, Martin.
Not all services platforms can support Rust, but I found one
that does.
Welcome to the show, JK.
Happy to be here.
You worked with cloud computing across banking,
insurance, technology, and mining.
You worked on systems that received two requests per day
and on systems that received 2 billion requests per day.
That's right, man.
So why did you want to use Rust in the cloud?
Well, I heard a lot about Rust,
and in some reading that I've been doing lately in some surveys,
it was found that Rust is the most loved language by its users.
So I wanted to try it out in the cloud.
And being a serverless enthusiast, I didn't want a server, so
I had to find a way to do it.
Yes. And Rust is
not available on many serverless platforms right now.
That's right.
It's not it's not available on many at all.
It's not a top ten most used language.
So its popularity isn't quite there
and therefore most cloud providers don't support it out of the box.
So I went looking for a way to find it.
Mmmm... If only there was a way to run
any code in the cloud.
That's right.
Containers can solve that problem.
And with Cloud Run,
we can run any kind of container without having to manage a server.
Show me how it's done.
Okay, so here's the source code.
This HTTP server is using the Hyper web framework.
And as you can see here, the main function here is the entry point
to the application.
It listens to HTTP requests and
the make_service function
that runs has a function which itself takes a service function.
That function accepts an HTTP request
which we call “rustless”.
This function simply returns a HTTP 200 or OK status.
Okay.
And the accompanying text in the classic Hello world, but I
made it a bit more fun to make it
“Hello Rustless”
Right. Great.
That's a great first step because yeah, once you have.
Hello, Rust.
Hello world running, then you can build it out
to be an API for a web app or mobile app, right?
Absolutely.
So all you need is a Docker file to build the container.
And the Docker file that we've got here uses a Rust based image.
What it does is then it copies
over that source code that we've built into the container
and then we expose and map port 8080
to the internal containers, 8080 port,
and then we build our binary using the Cargo tooling.
And finally we set our command to run when the container starts.
And that that command is just going to run the “rustless”
binary that's been produced as part of the build process
and that sits in the release folder. Got it.
And then you need to build the container.
That's right.
The build service does that for us.
And all we need to do in order for cloud
build to do the work for us is to define a cloudbuild.yaml, and that explains
what Cloud Build needs to do and the steps it needs to take to build that artifact.
The first step in the cloud build Docker file commands that we talked
about previously, and then it publishes it to the container registry.
The second step in the in this cloud build file is to deploy the service
to Cloud Run.
And in this case we've opted to use US Central.
But you could replace the region with whatever's closest to you.
Finally, there's a substitution section at the bottom
that we've used to be able to easily swap out service names.
If we need to, or if we mean need to make use of reusable variables.
I'll build and deploy the code now.
Look at it go!
Okay, cool.
Look at that.
The deploy finished and the builds completed and we
if we go to the cloud console and then Cloud Run.
And here. Yep.
We can click on this service here and then you can see
at the top of this page that there's a URL that's the service URL.
So if we click on that URL,
you'll see a browser tab opens up and bang,
there's the service responding with “Hello Rustless”.
Yeah, very nice.
So what makes Rust good for backends like this?
So Rust obviously is a compiled language, so it's
you end up with a binary at the end and that means that you can have
you get a very quick start up time and since Cloud Run can scale up very quickly,
that means that you can scale out to support high, peaky demands
because we end up with a single binary.
There's a bit of simplicity sort of associated with that,
and Rust uses a lot less memory and is more efficient
with the CPU than other languages, like maybe Node or Python.
And with that it means you have a lower cloud bill, which is good
for your wallet.
And finally, it's good for performance critical tasks.
So things that need to perform really, really fast or things that need to
that rely on that sort of performance or a high,
very low latency process is a good option for that.
Got it.
And many Rust programs,
they do some number crunching on the back end like data science and such.
So you may not want to
or need to trigger your code by HTTP right?
Now you don't have to trigger your code with HTTP.
That's right.
You can use other types of triggers such as queues or Cloud Run jobs.
This will skip the HTTP listener altogether
and then your jobs on will run your code
from the beginning all the way through to the end,
and then it will just terminate the container when it's completed.
You also had to write a Docker file.
Yeah, the Docker file is pretty straightforward.
The one that we've used, you can customize obviously to your heart's content,
but this is just an example.
And so what it does is it just uses the Rust based image and it,
it would be similar for most Rust services that you'd use.
You may want to use a different base image if you've got some customizations
going on.
Right.
Well, I just copy and paste the same Docker file for
pretty much all my Node.js projects.
Yeah, well copy paste will get you some of the way,
Martin, but it can, it can be fraught with risk.
So for example, you know,
you might have some security problems or some of the images might have
vulnerabilities in them.
Hmm. Makes sense.
So when working with Docker files, I started to notice some patterns.
You know, you can identify the same sort of thing that's happening,
that's happening in different containers and that sort of thing.
So I highly recommend just having a read through the documentation
and I discovered that it's not too hard to write your own Docker file.
So I want to play around with your Rust program
and maybe add a simple REST API, do some performance testing.
Can I download your code? Absolutely.
I've got a GitHub page which has the code that we've talked about today
along with a blog post to explain how to get it all running.
And I've had a lot of fun getting into Rust.
It's fun to share
what I've been learning about and I hope you enjoyed running it.
Thank you for showing us. JK.
We should talk more about serverless computing some other time.
That would be awesome!
Thanks very much for having me, Martin.
And thank you, everyone for watching.
If you have questions for JK or me, please add them in the comments section.
Also, let me know if there are other serverless topics
you'd like to hear about in future episodes.
I've read every single comment.
Until next time.