Tutorial - Create a Serverless Function Using C++

In this tutorial, you will create an EDJX serverless function using C++ that prints "Hello World" to the Web browser.

  • The instructions in this tutorial are for Linux. Alternatively, you can build this function using macOS or Windows (install the Windows Subsystem for Linux (WSL) and follow the Linux instructions).

  • EDJX CLI is used for building and deploying the function. Alternatively, you can build and deploy the function using EDJX Console or VS Code.

See the Using C++ to Create an EDJX Serverless Function topic for detailed installation options.

The following are the steps described in this topic for building the function:

Install the SDKs

Install the EDJX C++ SDK and the WASI C++ SDK in the user’s home directory.

  • Ensure that the following tools are installed on your system — make (GNU Make), git, wget, and tar.

  • Create an edjx directory in the user’s home directory:

    mkdir -p ~/edjx
See the release page for all EDJX C++ SDK downloads. Each build of the EDJX C++ SDK targets a specific WASI SDK. For example, wasi-12 releases of the EDJX C++ SDK should be used with WASI SDK 12.

Install the EDJX C++ SDK

Install the EDJX C++ SDK using one of the following options:

  • Option 1:

    Set the EDJX SDK and WASI SDK variables with the specified version.

    EDJX_SDK_VERSION=v21.11.1
    WASI_SDK_VERSION=12

    Set the $INSTALLATION_DIR variable to user’s home directory and change the directory.

    INSTALLATION_DIR="${HOME}/edjx"
    cd "$INSTALLATION_DIR"

    Download the EDJX C++ SDK from the edjx-cpp-sdk GitHub repository.

    wget "https://github.com/edjx/edjx-cpp-sdk/releases/download/${EDJX_SDK_VERSION}/edjx-cpp-sdk-${EDJX_SDK_VERSION}-wasi-${WASI_SDK_VERSION}.tar.gz"

    Unzip and create a soft link to the edjx-cpp-sdk directory.

    tar -xvf "edjx-cpp-sdk-${EDJX_SDK_VERSION}-wasi-${WASI_SDK_VERSION}.tar.gz"
    rm -f edjx-cpp-sdk
    ln -s "edjx-cpp-sdk-${EDJX_SDK_VERSION}-wasi-${WASI_SDK_VERSION}" edjx-cpp-sdk
  • Option 2:

    Set the INSTALLATION_DIR variable to the user’s home directory.

    INSTALLATION_DIR="${HOME}/edjx"

    Get the latest EDJX C++ SDK by cloning the edjx-cpp-sdk repository.

    git clone --depth 1 "https://github.com/edjx/edjx-cpp-sdk.git" "${INSTALLATION_DIR}/edjx-cpp-sdk"
    With the --depth 1 argument, only the latest EDJX C++ SDK build for the specific WASI SDK version is downloaded. See the WASI_SDK_VERSION file for the WASI SDK version against which the EDJX C++ SDK in the repository was compiled.
    The path to the EDJX C++ SDK is expected to be ~/edjx/edjx-cpp-sdk/include and ~/edjx/edjx-cpp-sdk/lib.

Install the WASI C++ SDK

  1. Set the WASI SDK variables with the specified version.

    WASI_SDK_VERSION_FULL=12.0
    WASI_SDK_VERSION="${WASI_SDK_VERSION_FULL%%.*}"
  2. Set the variable to Linux.

    WASI_SDK_OS=linux
  3. Set the $INSTALLATION_DIR variable to the user’s home directory and change the directory.

    INSTALLATION_DIR="${HOME}/edjx"
    cd "$INSTALLATION_DIR"
  4. Download the WASI C++ SDK for Linux from the WASI SDK Releases GitHub repository. This repository provides the necessary C and C++ standard library functions required to build C++ serverless functions.

    wget "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION}/wasi-sdk-${WASI_SDK_VERSION_FULL}-${WASI_SDK_OS}.tar.gz"
    The EDJX C++ SDK is compiled against a specific WASI SDK version. For example, edjx-cpp-sdk-vX.Y.Z-wasi-12.tar.gz must be used with WASI SDK version 12.
  5. Unzip and create a soft link to the wasi-sdk directory.

    tar -xf "wasi-sdk-${WASI_SDK_VERSION_FULL}-${WASI_SDK_OS}.tar.gz"
    rm -f wasi-sdk
    ln -s "wasi-sdk-${WASI_SDK_VERSION_FULL}" wasi-sdk

    You can delete the .tar file, as desired.

    Make sure that the bundled compiler is in the ${HOME}/edjx/wasi-sdk/bin/clang++ directory.
  6. Clone the EDJX C++ Example code repo GitHub repository to your environment. This repository contains samples to help you build C++ serverless functions.

    git clone https://github.com/edjx/edjsamples-cpp
    See the README.md files in the EDJX GitHub repository for advanced installation options.

Create the Function

Using the function init CLI command, create a directory for the function:

  1. Set the organization to be associated with the function.

    edjx config organization -i

    If no organizations display, create one using the Console.

  2. Create an application hello world.

    edjx application create -n hello-world
  3. Set the application to be associated with the function.

    edjx config application -i
  4. Create the directory named NEWCPP with a function called hello-world-func:

    edjx function init NEWCPP
    Function Name: hello-world-func
    ✔ WASM
    ✔ C++
    ✔ HTTP
    ✔ 30
    ✔ 64
    Setting up project with starter files...
    Project successfully initialized at NEWCPP
  5. Open VS Code.

    code .

    VS Code displays the directory and files of your function.

    function build vscode cpp

  6. Modify the code in src/serverless_function.cpp, as desired:

    cat serverless_function.cpp
    #include <edjx/logger.hpp>
    #include <edjx/request.hpp>
    #include <edjx/response.hpp>
    #include <edjx/http.hpp>
    
    using edjx::logger::info;
    using edjx::request::HttpRequest;
    using edjx::response::HttpResponse;
    using edjx::http::HttpStatusCode;
    
    static const HttpStatusCode HTTP_STATUS_OK = 200;
    
    HttpResponse serverless(const HttpRequest & req) {
    
        info("Inside example function");
    
        return HttpResponse("Hello World")
            .set_status(HTTP_STATUS_OK)
            .set_header("Server", "EDJX");
    }
EDJX C++ serverless application has two C++ files: lib.cpp and serverless_function.cpp.

Build the Function

Using the function build CLI command, build the function. Ensure that you are in the function directory to execute this command, that is, the directory that contains the edjconfig.yaml file.

edjx function build

Output

/opt/wasi-sdk/bin/clang++ -MD -MP --target=wasm32-wasi --sysroot=/opt/wasi-sdk/share/wasi-sysroot/ -Wall -Werror -O2 -fno-exceptions -static -I/opt/edjx-cpp-sdk/include -c -o build//serverless_function.o src//serverless_function.cpp
/opt/wasi-sdk/bin/clang++ -MD -MP --target=wasm32-wasi --sysroot=/opt/wasi-sdk/share/wasi-sysroot/ -Wall -Werror -O2 -fno-exceptions -static -I/opt/edjx-cpp-sdk/include -c -o build//lib.o src//lib.cpp
/opt/wasi-sdk/bin/clang++ --target=wasm32-wasi --sysroot=/opt/wasi-sdk/share/wasi-sysroot/ -Wall -Werror -O2 -fno-exceptions -static -I/opt/edjx-cpp-sdk/include -L/opt/edjx-cpp-sdk/lib -o bin//edjfunction_example_cpp.wasm build//serverless_function.o build//lib.o -ledjx
Project built and file generated successfully at /home/sampleuser/testfunction/bin/edjfunction_example_cpp.wasm

By default, the files are created with the following file structure and the resulting WASM file is saved in <application>/bin/<app>.wasm, as shown below.

Project Directory Name

|-- bin
    |-- edjfunction_example_cpp.wasm
|-- build
    |-- lib.d
    |-- lib.o
    |-- serverless_function.d
    |-- serverless_function.o
|-- src
    |-- lib.cpp
    |-- serverless_function.cpp
|-- edjconfig.yaml
|-- LICENSE
|-- Makefile
|-- README.md

Where each function directory consists of the following subdirectories:

  • bin — Saves the compiled WASM executable of the example application, which is generated when compiling the function

  • build — Contains the intermediate object files of the example application, which are generated when compiling the function

  • src — Contains the source code of the example application

Deploy the Function

To deploy the function:

  1. Using the function deploy command, deploy the function.

    edjx function deploy

    Alternatively, you can deploy the function using -p option to specify the .wasm file.

    edjx function deploy -p ./edjfunction_example_cpp.wasm

    Output

    Successfully initiated function deployment
  2. Read the function.

    edjx function read hello-world-func

    Output

    Name                hello-world-func
    Function ID         f2fff891-124c-45de-88ee-7d8d2b0787a7
    Trigger             HTTP
    Language            C++
    Runtime             WASM
    Timeout             30s
    Memory Allocated    64.0 MB
    Compute (GB-Sec)    0 GB-SEC
    Network (B)         0 B
    Requests            0
    Organization        edjdocs
    Created By          Sample User
    Created At          2022-06-08 13:35:33.187 +0530 IST
    Last Updated        2022-06-08 13:42:40.627 +0530 IST
    Execution URL       https://90309513-d3ae-449b-8ea0-0563fe942b5d.fn.edjx.net/hello-world-func
  3. Copy the Execution URL.

  4. Paste the URL in a Web browser to verify function deployment to the EDJX network.

    example cpp