Tutorial - Create a Serverless Function Using Rust

In this tutorial you will clone the Welcome to EDJX! example Rust function and modify it to return Hello World!.

The instructions in this tutorial are for systems running Linux.

This tutorial uses the CLI, the Console, and the VS Code extension.

The following are the steps involved in building the serverless function:

Prerequisites

  1. Download the latest version of Rust.

    Rust requires a C linker to be present on the system. On most Linux distributions, you need to have the gcc package installed.
  2. Open a terminal instance on your system.

  3. Navigate to the $HOME/ or ~/ directory.

  4. Install the Rust WASM target.

    rustup target add wasm32-unknown-unknown
  5. Sign Up for EDJX Account.

  6. Install edjCLI.

  7. Install VS Code Extension.

Create the rust-hello Function

  1. Open a terminal instance on your system.

  2. Log into the EDJX Platform.

    root@edjx:~ # edjx login
    Username: <email>
    Password: **********
    Logged in successfully
    root@edjx:~ #
  3. Set the organization to be associated with all EDJX operations.

    edjx config organization -i

    If no organizations display, create one using the Console.

  4. Create an application.

    edjx application create -n rust-hello
  5. Set the application to be associated with any new functions.

    edjx config application -i
  6. Create a project directory within the application, and then set the function parameters.

    edjx function init hello-app
    Function Name: rust-hello1
    ✔ WASM
    ✔ Rust
    ✔ HTTP
    ✔ 30
    ✔ 64
    Setting up project with starter files......
    Project successfully initialized in hello-app
  7. Change to the function directory.

    ~$ cd hello-app
    ~/hello-app$
  8. Open VSCode or edit the file in the terminal session.

    code .

    If using VSCode, the directory and files of your function display.

  9. Open the serverless_function.rs file.

    rust serverless function selected

    use edjx::{info, HttpRequest, HttpResponse, StatusCode};
    
    pub fn serverless(_req: HttpRequest) -> HttpResponse {
    
        info!("Inside example function");
    
        HttpResponse::from("Welcome to EDJX".to_string())
            .set_status(StatusCode::OK)
            .set_header("Server".parse().unwrap(), "EDJX".parse().unwrap())
    }
  10. Modify the line HttpResponse::from("Welcome to EDJX".to_string()) to read HttpResponse::from("Hello World!".to_string()).

    rust hello world vscode

  11. Save the file.

Build the rust-hello Function

  1. With VS Code open, select the edjconfig.yaml file.

    rust tutorial edjconfig

  2. Click Build.

    vscode function build inline

    To build using the edjCLI, issue the edjx function build command while in the function directory.
    ~/hello-app$ edjx function build

    The build process executes.

  3. When the process is successfully completed, the artifact_path is added to the edjconfig.yaml file and the target folder is created under the project folder.

    rust terminal output

    By default, the .wasm file is built and saved under the $HOME/<username>/<application-name>/target/wasm32-unknown-unknown/release directory.

Deploy the rust-hello Function

  1. With VS Code open, click Deploy.

    rust tutorial vscode deploy

    To deploy the function using the edjCLI, issue the edjx function deploy command while in the function directory.
    ~/hello-app$ edjx function deploy
  2. Using the edjCLI, initialize the application you created.

    edjx config application -i
  3. Issue the function read command.

    edjx function read rust-hello
    Name                rust-hello
    Function ID         f6af8514-8d03-44db-b5db-7344aba64fa0
    Trigger             HTTP
    Language            Rust
    Runtime             WASM
    Timeout             30s
    Memory Allocated    64.0 MB
    Compute (GB-Sec)    0 GB-SEC
    Network (B)         0 B
    Requests            0
    Organization        test_org
    Created By          edjdocs
    Created At          2022-06-12 00:06:43.278 -0700 MST
    Last Updated        2022-06-12 00:06:44.256 -0700 MST
    Execution URL       https://0a068850-5bc0-49fe-b3d1-680ae1b9ec07.fn.edjx.net/rust-hello
  4. Copy the Execution URL.

  5. Paste the URL in a Web browser to verify that the function was successfully deployed to the EDJX network.
    Hello World! is returned within the browser window.

    tutorial rust hello returned

    By default, the .wasm file is built and saved under the $HOME/<username>/<application-name>/target/wasm32-unknown-unknown/release directory with the following file structure.

    Project Directory Name

    |-- src
        |-- lib.rs
        |-- serverless-function.rs
    |-- target
        |-- wasm32-unknown-unknown
            |-- release
                |-- <function>.wasm
    |-- Cargo.lock
    |-- Cargo.toml
    |-- edjconfig.yaml
    |-- LICENSE
    |-- README.md