Tutorial - Create an EdjFunction 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
-
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. -
Open a terminal instance on your system.
-
Navigate to the
$HOME/
or~/
directory. -
Install the Rust WASM target.
rustup target add wasm32-unknown-unknown
-
Install edjCLI.
-
Install VS Code Extension.
Create the rust-hello Function
-
Open a terminal instance on your system.
-
Log into the EdjPlatform.
root@edjx:~ # edjx login Username: <email> Password: ********** Logged in successfully root@edjx:~ #
-
Set the organization to be associated with all EDJX operations.
edjx config organization -i
If no organizations display, create one using the Console.
-
Create an application.
edjx application create -n rust-hello
-
Set the application to be associated with any new functions.
edjx config application -i
-
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
-
Change to the function directory.
~$ cd hello-app ~/hello-app$
-
Open VSCode or edit the file in the terminal session.
code .
If using VSCode, the directory and files of your function display.
-
Open the serverless_function.rs file.
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()) }
-
Modify the line
HttpResponse::from("Welcome to EDJX".to_string())
to readHttpResponse::from("Hello World!".to_string())
. -
Save the file.
Build the rust-hello Function
-
With VS Code open, select the
edjconfig.yaml
file. -
Click Build.
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.
-
When the process is successfully completed, the
artifact_path
is added to theedjconfig.yaml
file and the target folder is created under the project folder.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
-
With VS Code open, click Deploy.
To deploy the function using the edjCLI, issue the edjx function deploy command while in the function directory.
~/hello-app$ edjx function deploy
-
Using the edjCLI, initialize the application you created.
edjx config application -i
-
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
-
Copy the Execution URL.
-
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.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