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

Procedure

  1. Run the following command to initialize a project:
    s init fc-custom-cpp-event -d fc-custom-cpp-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-cpp-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-01-26 08:39:21] [INFO] [S-CLI] - Start the pre-action
    [2022-01-26 08:39:21] [INFO] [S-CLI] - Action: make build
    docker build -t fc-cpp-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  2.048kB
    ......
    Reading package lists...
    Building dependency tree...
    Reading state information...
    The following additional packages will be installed:
      cmake-data libarchive13 libjsoncpp1 libuv1
    Suggested packages:
      codeblocks eclipse ninja-build lrzip
    The following NEW packages will be installed:
      cmake cmake-data libarchive13 libjsoncpp1 libuv1
    0 upgraded, 5 newly installed, 0 to remove and 49 not upgraded.
    Need to get 4709 kB of archives.
    .......
    -- Build files have been written to: /tmp/sample/release
    Scanning dependencies of target bootstrap
    [ 33%] Building CXX object CMakeFiles/bootstrap.dir/src/register_handler.cpp.o
    [ 66%] Building CXX object CMakeFiles/bootstrap.dir/src/handlers/event_handler.cpp.o
    [100%] Linking CXX executable /tmp/bin/bootstrap
    [100%] Built target bootstrap
    [2022-01-26 08:41:55] [INFO] [S-CLI] - End the pre-action
     Checking Service, Function (1.46s)
     Creating Service, Function (4.28s)
    
    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:       cppEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 512
        timeout:    10
  5. Run the following command to remotely invoke the function:
    s invoke -e "{}"
    Sample command output:
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: 9c7ff302-6a67-42b3-9076-3790d1c1****
    FC Initialize End RequestId: 9c7ff302-6a67-42b3-9076-3790d1c1****
    /invoke is called.
    FC Invoke Start RequestId: 9c7ff302-6a67-42b3-9076-3790d1c1****
    2022-01-26T08:46:00 9c7ff302-6a67-42b3-9076-3790d1c19e4b [INFO] handling invoke
    FC Invoke End RequestId: 9c7ff302-6a67-42b3-9076-3790d1c1****
    
    Duration: 0.72 ms, Billed Duration: 1 ms, Memory Size: 512 MB, Max Memory Used: 2.11 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    null
    
    End of method: invoke