You can use the custom runtime of Function Compute to write Rust functions in Function Compute. This topic describes how to deploy and invoke a Rust function. You can modify the Rust sample code as needed for custom development.

Procedure

  1. Run the following command to initialize a project:
    s init fc-custom-rust-event -d fc-custom-rust-event
    Note -d specifies the name of the folder in which the sample project resides. You can customize the folder name as required.
  2. Run the following command to go to the project directory:
    cd fc-custom-rust-event
  3. Optional: Modify the sample code as needed for custom development.
  4. Run the following command to deploy the project:
    s deploy -y
    Sample command output:
    [2022-02-22 08:43:57] [INFO] [S-CLI] - Start the pre-action
    [2022-02-22 08:43:57] [INFO] [S-CLI] - Action: make build
    docker build -t fc-rust-env -f Dockerfile.build .
    Sending build context to Docker daemon    571MB
    Step 1/4 : FROM python:3.7.4-stretch
     ......
    Step 4/4 : CMD ["bash"]
     ---> Using cache
     ---> f10570a7b2a6
    Successfully built f10570a7b2a6
    Successfully tagged fc-rust-env:latest
    docker run --rm -it -v $(pwd):/opt/rust-demo fc-rust-env bash -c "cd /opt/rust-demo/bootstrap && /root/.cargo/bin/cargo build"
        Updating crates.io index
      Downloaded remove_dir_all v0.5.3
     ......
      Downloaded 109 crates (5.1 MB) in 3.09s
    WARN rustc_metadata::locator no metadata found: incompatible metadata version found: '/opt/rust-demo/bootstrap/target/debug/deps/libpin_project_internal-a0759cb2a8262070.so'
    WARN rustc_metadata::locator no metadata found: incompatible metadata version found: '/opt/rust-demo/bootstrap/target/debug/deps/libpin_project_internal-a0759cb2a8262070.so'
        Finished dev [unoptimized + debuginfo] target(s) in 1m 35s
    mkdir -p pkg
    cp bootstrap/target/debug/bootstrap pkg/
    [2022-02-22 08:45:51] [INFO] [S-CLI] - End the pre-action
     Checking Service, Function (2.73s)
     Creating Service, Function (12.75s)
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       rustEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 512
        timeout:    30
  5. Run the following command to remotely invoke the function:
     s invoke -e "hello"
    Sample command output:
    ========= FC invoke Logs begin =========
    Not all function logs are available, please retry
    FC Invoke End RequestId: e1d309c2-321a-46a8-8568-4f648cacfdb6
    Duration: 6.40 ms, Billed Duration: 7 ms, Memory Size: 512 MB, Max Memory Used: 8.05 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    Rust demo function invoked.
    
    
    End of method: invoke