Rust

Overview

Rust is a system programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. You can build Rust web services with frameworks like Actix or Iron.

Clever Cloud allows you to deploy Rust web applications. This page will explain you how to set up your application to run it on our service.

Create an application on Clever Cloud

With the web console

Refer to Quickstart for more details on application creation via the console.

With the Clever Tools CLI

  1. Make sure you have clever-tools installed locally or follow our CLI getting started guide.
  2. In your code folder, do clever create --type <type> <app-name> --region <zone> --org <org> where :
    1. type is the type of technology you rely on
    2. app-name the name you want for your application,
    3. zone deployment zone (par for Paris and mtl for Montreal)
    4. org the organization ID the application will be created under.

Refer to clever create for more details on application creation with Clever Tools.

Setting up environment variables on Clever Cloud

With the Clever Cloud console

  1. Go to the Clever Cloud console, and find the app you want to fine tune under it’s organization.
  2. Find the Environment variables menu and select it.
  3. In this menu, you will see a form with VARIABLE_NAME and variable value fields.
  4. Fill them with the desired values then select Add.
  5. Don’t forget to “Update Changes” at the end of the menu.

With the Clever Tools CLI

  1. Make sure you have clever-tools installed locally. Refer to our CLI getting started.
  2. In your code folder, do clever env set <variable-name> <variable-value>

Refer to environment variables reference for more details on available environment variables on Clever Cloud.

You can of course create custom ones with the interface we just demonstrated, they will be available for your application.

Configure your Rust application

Mandatory configuration

Make sure that:

  • you have pushed in master branch
  • you listen on port 8080
  • you have committed Cargo.lock
  • you have at least one binary target in Cargo.toml

The result of cargo build --release --locked must be an executable which starts a web server listening on 0.0.0.0:8080.

For instance, a minimal iron application can look like this:

extern crate iron;

use iron::prelude::*;
use iron::status;

fn main() {
    fn hello_world(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((status::Ok, "Hello World!")))
    }

    let _server = Iron::new(hello_world).http("0.0.0.0:8080").unwrap();
    println!("On 8080");
}

Multiple binary targets

If your Cargo.toml defines multiple targets, you must specify the one you want to run, with the CC_RUST_BIN environment variable. If CC_RUST_BIN is specified, then the executable produced by this target is used to start the application.

Custom run command

If you need to run a custom command (or just pass options to the program), you can specify it through the CC_RUN_COMMAND environment variable.

For instance, you can have CC_RUN_COMMAND=./target/release/myapp <options>.

Dependencies

Make sure to list all your dependencies in Cargo.toml. For the example above, you need:

[package]
name = "my-app"
version = "0.1.0"
authors = []

[dependencies]
iron = "0.4.0"

Cached dependencies

Enabling dependencies caching

You can enable dependencies caching by adding the CC_CACHE_DEPENDENCIES=true environment variable in your application.

Disabling dependencies caching

You can disable dependencies caching completely by removing the CC_CACHE_DEPENDENCIES environment variable from the Clever Cloud console, in the Environment variables menu of your application. Or by setting it to CC_CACHE_DEPENDENCIES=false.

To fully remove cached dependencies, you have to rebuild your application from scratch. You can select “rebuild and restart” from the Clever Cloud console or launch clever restart --without-cache with the Clever Tools CLI.

Private dependencies

If you use dependencies on a private git repository inside your project, it needs a bit of configuration until this cargo issue has been resolved.

First, you need to use the HTTPS url as the git url for your dependency in your Cargo.toml:

private-dep = { git = "https://github.com/user/my-private-dep.git" }

Then, you need to create a personal access token. It allows to not use your password:

Once you have the token, we need to tell Git to use a credential store. For that, we are going to create it.

Create a clevercloud/pre-build.sh file at the root of your application and paste:

#! /usr/bin/env bash

git config --global credential.helper store
echo "https://${GIT_USERNAME}:${GIT_PASSWORD}@gitlab.com" > ~/.git-credentials
chmod 600 ~/.git-credentials

If you have multiple private repositories, add them accordingly.

Now, go into the environment variables page of your application and create those environment variables:

  • GIT_USERNAME: your Github / gitlab / other username
  • GIT_PASSWORD: your Github / gitlab / other password
  • CC_PRE_BUILD_HOOK: clevercloud/pre-build.sh
  • CC_POST_BUILD_HOOK: rm /home/bas/.git-credentials

This adds the git configuration before the build start and it cleans it after the build has been done.

Rust channels

By default, your application is built with the latest stable rust version. If you require beta, nightly or a specific Rust version, you can set CC_RUSTUP_CHANNEL environment variable value to beta, nightly or a specific version (eg. 1.36.0).

The build uses rustup to select the Rust version you need.

Cargo features

You can enable specific features for your crate by settings the CC_RUST_FEATURES environment variable to the list of features to enable.

To access environment variables from your code, just get them from the environment with std::env::var(<KEY_NAME>).

If some environment variables are critical to your application, here is an approach you can use:

use std::env;

fn main() {
    let my_config_value = env::var("MY_VAR").expect("Missing env var `MY_VAR`");

    something_that_runs_a_web_server(my_config_value);
}

This loads the environment variable in your main function and use .expect to fail early. This way, the application will refuse to start with an helpful error message if MY_VAR is not defined.

Git Deployment on Clever Cloud

You need Git on your computer to deploy via this tool. Here is the official website of Git to get more information: git-scm.com

Setting up your remotes

  1. The “Information” page of your app gives you your Git deployment URL, it looks like this:

    1. git+ssh://git@push.clever-cloud.com/<your_app_id>.git
    2. Copy it in your clipboard
  2. Locally, under your code folder, type in git init to set up a new git repository or skip this step if you already have one

  3. Add the deploy URL with git remote add <name> <your-git-deployment-url>

  4. Add your files via git add <files path> and commit them via git commit -m <your commit message>

  5. Now push your application on Clever Cloud with git push <name> master

Refer to git deployments for more details.

Deployment Video

Linking a database or any other add-on to your application

By linking an application to an add-on, the application has the add-on environment variables in its own environment by default.

On add-on creation

Many add-ons do exist on Clever Cloud: refer to the full list and check add-ons dedicated pages for full instructions.

During add-on creation, an Applications screen appears, with a list of your applications. You can toggle the button to Link and click next. If you finish the process of add-on creation, the application is automatically linked to it.

Add-on already exists

In the Clever Cloud console, under the Service Dependencies menu of your application, you can use the Link add-ons dropdown menu to select the name of the add-on you want to link and use the add button to finish the process.

You can also link another application from the same page in the Clever Cloud console, using the Link applications dropdown menu.

More configuration

Need more configuration? To run a script at the end of your deployment? To add your private SSH key to access private dependencies?

Go check the Common configuration page.

You may want to have an advanced usage of your application, in which case we recommend you to read the Administrate documentation section.

If you can’t find something or have a specific need like using a non supported version of a particular software, please reach out to the support.

Enable health check during deployment

The healthcheck allows you to limit downtimes. Indeed, you can provide Clever Cloud with paths to check. If these paths return something other than 200, the deployment will fail.

Add one (or several) environment variable as such:

CC_HEALTH_CHECK_PATH=/my/awesome/path

Or

CC_HEALTH_CHECK_PATH_0=/my/awesome/path
CC_HEALTH_CHECK_PATH_1=/my/other/path

The deployment process checks all paths. All of them must reply with a 200 OK response code.

By default, when no environment variable (for ex: APP_HOME) is defined, the monitoring checks your repository root path /.

Example

Using the path listed above, below are the expected logs:

Response from GET /my/awesome/path is 200
Response from GET /my/other/path is 500
Health check failed:
- GET /my/other/path returned 500.
If the deployment fails after this message, please update your configuration and redeploy.

In this example, the first path is OK, but the second one failed. This gives you a hint on what failed in your application.

Best practice for healthcheck endpoints

To make the most of a healthcheck endpoint, have it check your critical dependencies. For example:

  • execute SELECT 1 + 1; on your database
  • retrieve a specific Cellar file
  • ping a specific IP through a VPN
Last updated on