You can use the Custom Runtime of Function Compute to write functions in C++ (CPP). This topic describes how to quickly deploy and call a CPP function.
Step 1: Prepare the environment
An easy and simple way to install Funcraft is to download the executable binary files.
- Install Funcraft on the local machine. For more information, see installation instructions.
fun --versionto check whether the installation is successful.
fun configto configure Funcraft. Then configure Account ID, Access Key ID, Access Key Secret, and Default region name as prompted.
$ fun config Aliyun Account ID 1234xxx Aliyun Access Key ID xxxx Aliyun Access Key Secret xxxx Default region name cn-xxxx The timeout in seconds for each SDK client invoking 300 The maximum number of retries for each SDK client 5 Allow to anonynously report usage statistics to improve the tool over time? (Y/n)
Step 2: Deploy and call a function
- Run the following command to clone the sample project to your local machine.
git clone https:// github.com/awesome-fc/fc-custom-demoNote If you do not have git installed, enter https://github.com/awesome-fc/fc-custom-demo in the address bar of your browser. Click the download button to download directly.
- Run the following command to access the cloned sample project:
cd fc-custom-demo cd cpp-demo
- Run the following command to deploy the project to Function Compute:
make deployThe following result 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://17904911-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
- Run the following command to call the deployed function:
fun invoke cpp_demo/fc_cpp_event -e "Hello World"The following result 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://17904911-123456789.test.functioncompute.com -d "Hello World" Hello World