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 sample C++ code.

Prerequisites

Before you begin, make sure that you have completed the following operations:
  • Install Node.js 8.6.0 or later. For more information, visit Downloads.

Procedure

  1. Run the following command to clone the sample project to the on-premises machine:
    git clone https://github.com/awesome-fc/fc-custom-demo          
    Note If you do not install Git, you can download the sample project from https://github.com/awesome-fc/fc-custom-demo.
  2. Optional:Modify the sample code in the cpp-demo directory to implement your business logic.
  3. Run the following command to go to the cloned sample project:
    1. Run the following command to go to the cpp-demo directory:
      cd cpp-demo
  4. Run the following command to deploy the project to Function Compute:
    make deploy
    The following output is returned:
    docker build -t fc-cpp-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  2.048kB
    Step 1/3 : FROM aliyunfc/runtime-custom:base
     ---> 5bbdcf6377bd
    ...
    Step 3/3 : RUN apt-get install -y cmake
     ---> Using cache
     ---> a244cd26cec2
    Successfully built a244cd26cec2
    Successfully tagged fc-cpp-runtime:latest
    docker run --rm -it -v $(pwd):/tmp fc-cpp-runtime bash -c "cd /tmp && ./build.sh"
    -- The C compiler identification is GNU 6.3.0
    -- The CXX compiler identification is GNU 6.3.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    ...
    Scanning dependencies of target cppruntime
    [ 33%] Building CXX object CMakeFiles/cppruntime.dir/src/handler.cpp.o
    [ 66%] Building CXX object CMakeFiles/cppruntime.dir/src/logging.cpp.o
    [100%] Linking CXX shared library /tmp/bin/libcppruntime.so
    ...
    -- Build files have been written to: /tmp/sample/release
    ...
    [100%] Built target bootstrap
    fun deploy -y
    
    Waiting for service cpp_demo to be deployed...
            Waiting for function fc_cpp_event to be deployed...
                    Waiting for packaging function fc_cpp_event code...
                    The function fc_cpp_event has been packaged. A total of 2 files were compressed and the final size was 446.51 KB
            function fc_cpp_event deploy success
            Waiting for function fc_cpp_http to be deployed...
                    Waiting for packaging function fc_cpp_http code...
                    The function fc_cpp_http has been packaged. A total of 2 files were compressed and the final size was 446.51 KB
                    Waiting for HTTP trigger http_t to be deployed...
                    triggerName: http_t
                    methods: [ 'GET', 'POST', 'PUT', 'DELETE' ]
                    url: https://123456789.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/cpp_demo/fc_cpp_http/
                    Http Trigger will forcefully add a 'Content-Disposition: attachment' field to the response header, which cannot be overwritten
                    and will cause the response to be downloaded as an attachment in the browser. This issue can be avoided by using CustomDomain.
    
                    trigger http_t deploy success
            function fc_cpp_http deploy success
    service cpp_demo deploy success
    
    Detect 'DomainName:Auto' of custom domain 'my_domain'
    Fun will reuse the temporary domain http://1******1-123456789.test.functioncompute.com, expired at 2020-05-06 20:41:51, limited by 1000 per day.
    
    Waiting for custom domain my_domain to be deployed...
    custom domain my_domain deploy success
  5. Run the following command to invoke the deployed function:
    fun invoke cpp_demo/fc_cpp_event -e "Hello World"
    The following output is returned:
    ...
    ========= FC invoke Logs begin =========
    /invoke is called.
    FC Invoke Start RequestId: bf282a87-0f0b-4953-b159-a31792bad22a
    2020-04-27T08:01:08 bf282a87-0f0b-4953-b159-a31792bad22a [INFO] handling invoke
    FC Invoke End RequestId: bf282a87-0f0b-4953-b159-a31792bad22a
    
    Duration: 9.11 ms, Billed Duration: 100 ms, Memory Size: 512 MB, Max Memory Used: 4.25MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    Hello World
    $ curl http://1******1-123456789.test.functioncompute.com -d "Hello World"
    Hello World