Discover more from JK’s Blog
Serverless Rust with Cloud Run
Deploying a serverless Rust based web app in less than 3 minutes.
Rust has been picking up a lot of interest in the last few years, and with the growing interest of serverless, there’s currently no native Rust-based offering of running rust on any of the major clouds, but with Cloud Run it’s able to run any container so I thought I’d throw together a quick getting started with serverless Rust on Google Cloud.
I’ve open sourced a code repository here which has everything you need to get started and it will even deploy a live service.
To get the code running, we’ll need to put it into a container and package it up so that Cloud Run can run it.
FROM rust COPY . . EXPOSE 8080:8080 RUN cargo build --release CMD [ "./target/release/rustless" ]
This is a super simple example, and it just pulls in the latest image from Rust and copies the files in the directory in and uses cargo to build a binary. Finally we run it with the CMD command when the container starts. We also expose port 8080 so that the service can be reached.
One built it needs to be deployed to cloud run. You can either do this through the console once the image has been stored on container registry or you can use the cloudbuild file included in the repo which will build the image and then deploy it to cloud run in us-central1 (the region is configurable).
There’s plenty of guides around that show how to deploy a container using the user interface, so instead, we’ll use the command-line and deploy using the CLI.
Download and install the
gcloud CLI if you haven’t already. Once setup, make sure that Cloud Run and Google Container Registry API’s have been turned on for your project.
Then simply run
gcloud builds submit . from the root of the project directory.
If you check out the
cloudbuild.yaml file you’ll see there are two steps. One to build the file, and another to deploy it to Cloud Run.
At the end of the build process you’ll see a similar output to this:
Step #1: Routing traffic.....done Step #1: Done. Step #1: Service [rustless] revision [rustless-00001-xap] has been deployed and is serving 100 percent of traffic. Step #1: Service URL: https://rustless-3qqsqbdsuq-uc.a.run.app Finished Step #1
Accessing the deployed app.
Notice in the output of the previous step, the service URL. This can be accessed via your browser. If you want to see the service I deployed as above, you can do so here:
That’s it! Hope you found this useful for deploying your Rust based serverless web services.