ALT logo
Home
Articles
Case Study: How to Deploy your Code with Cloudflare and Rust
Alexandre Therrien
By Alexandre Therrien
LinkedIn Github Gmail
Article content
As a developer, I often find myself exploring new technologies and platforms to expand my skillset. Recently, I embarked on a journey to deploy my Rust-based backend on Cloudflare, a popular web hosting company. Little did I know that this endeavor would be filled with unexpected challenges and a steep learning curve. 
1. Initiating the Project

The initial plan seemed straightforward. Cloudflare has its own tool, called "wrangler," which is designed to simplify the deployment process with Cloudflare's systems. However, as I delved deeper into the documentation, I quickly realized that the reality was far from simple.

One of the first hurdles I encountered was the inability to download templates for Rust projects. Instead of the convenient command Cloudflare provided, I had to manually copy and paste the repository containing the Rust template I wanted, and then meticulously adjust the variables in the wrangler.toml file to fit my project's needs.

That is because the command mentioned in their documentation:

cargo generate cloudflare/workers-rs

was generating errors for me.

2. The WASM Barrier

Determined to overcome the obstacles of Cloudflare, I forged ahead, drawing on my previous experience with Rust and API development. But as I soon discovered, Cloudflare's ecosystem had its own unique quirks and requirements. The Cloudflare-specific library I had to use for deployment, coupled with the fact that Cloudflare compiles Rust code to WebAssembly (Wasm), added an extra layer of complexity to the process.

I had built many APIs with Tokio and Axium before, but that knowledge had gone to waste with the Cloudflare API framework. Developers, beware! Converting your code for Cloudflare is not something that can be done in a few hours.

3. cargo: not found Error

Undeterred, I pressed on, determined to find a solution. After days of trial and error, I finally managed to link my GitHub repository to Cloudflare and create the backend (the equivalent of "Cloudflare Workers") project.

But just when I thought I was nearing the finish line, I encountered a new obstacle: the dreaded "cargo: not found" error.

16:05:04.586 Running custom build: cargo install -q worker-build && worker-build --release 16:05:04.590 /bin/sh: 1: cargo: not found 16:05:04.591 16:05:04.650 ✘ [ERROR] Running custom build cargo install -q worker-build && worker-build --release failed. There are likely more logs from your build command above.

This error message indicated that the cargo tool, which is essential for building Rust projects, was not installed on the machine attempting to build my code.

4. Workaround Thought Process

First, I tried re-executing the deployment process. Who knows... maybe I had just been unlucky, and that this was the only machine that didn't have cargo installed. Wrong! My second execution did not pass.

How could I possibly resolve this? Building the code is quite instrumental, and there's no way around it. That's when I revisited the reason I was receiving this error.

Where can I find this piece of code? The command is currently defined in the wrangler.toml file, under the build section. Then, I can change that command, because the file is found in my Github repository.

Visual help
Location of the build command (under [build] section)

I decided to remove the command altogether, making the field empty. OK. The deployment process seems to pass, but nothing happens (as expected). How could I make this work?

The command that I had just removed added the executables under the build folder. What if I added the build folder to my Github repository? It's not a good practice, but if it works, I could create an automated process to build my code before pushing it to the repository.

I got back to my IDE. I could clearly see the build folder locally, but I couldn't see it in my repository. What was I missing?

Visual help
Location of the executable files on build command execution

It was a eureka moment when I realized that there was a hidden .gitignore file under the build folder that was excluding the build folder, preventing the executables from being included in the repository.

After removing this exclusion, I was able to successfully push the compiled binaries to GitHub, and Cloudflare was able to deploy my project without any issues.

5. Conclusion

Through this challenging journey, I learned valuable lessons about the importance of thoroughly understanding the deployment platform's requirements and the need to be adaptable and persistent when faced with unexpected obstacles.

I created a Cloudflare ticket and resolved it here:

Workers do not come with cargo

If you're considering deploying your Rust-based project on Cloudflare, I encourage you to heed my experience and be prepared for a potentially arduous, yet rewarding, process. By anticipating the unique challenges and leveraging the tricks I've shared, you can increase your chances of a successful deployment and unlock the benefits of Cloudflare's powerful platform.

Alexandre Therrien
By Alexandre Therrien
LinkedIn Github Gmail