通过函数计算的Custom Runtime,您可以使用Rust在函数计算平台编写函数。本文介绍快速部署和调用Rust函数的详细步骤。您可以按需修改Rust的示例代码实现二次开发。

操作步骤

  1. 执行以下命令,初始化项目。
    s init fc-custom-rust-event -d fc-custom-rust-event
    说明 -d用于指定该示例项目文件夹的名称,支持自定义。
  2. 执行以下命令,进入项目目录。
    cd fc-custom-rust-event
  3. 可选:按需修改函数的示例代码,实现二次开发。
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [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. 执行以下命令,远程调用函数。
     s invoke -e "hello"
    输出示例:
    ========= 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