百度智能云

All Product Document

          Intelligent Edge

          How to configure multiple functions in the Cloud Function Compute (CFC)

          The Cloud Function Compute (CFC) can only call one entry, but it does not mean that Cloud Function Compute (CFC) can only write one function. You can write multiple functions and call each other.

          Before the operations described herein are started, create a core according to Getting Started - Build a Core.

          Create a Storage Volume for Computing Services

          CFC Create a Function

          In this article, it takes Python 3.6 as an example for explanation.

          First, create a corresponding CFC function before creating a CFC storage volume. Enter Cloud Function Compute (CFC) from the Baidu AI Cloud console, click “Create a function”, and select “Blank function”.

          cfc-create-buttom.png

          Then, name the function “bie-cfc-demo”, select “Python 3.6” for operation, and click “Submit”.

          cfc-create.png

          After it is created successfully, click “Enter the code editing page”.

          cfc-create-success.png

          The CFC function code page automatically generates a “Hello World” function.

          cfc-edit-page.png

          There is only one function entry, but it supports to add many functions for calling each other. Here, two functions are added:

          def add(event, context): 
              result = {} 
              result['result'] = event['x'] + event['y'] + event['z'] 
              result['result2'] = add2(event, context)['result'] 
              return result 
          
          def add2(event, context): 
              result = {} 
              result['result'] = event['x'] + event['y'] + event['z'] 
              return result 

          Two functions add and add2 are added, and you can see that add2 function is called in add function. Click the “Save” button for the current code. Modify the entry function as index.add in the processing program, and click the “OK” button.

          cfc-edit-code.png

          After the code is added, you can conduct a test to check whether the code is executed normally. Click the “Test” button in the function code page, as shown in the figure below:

          cfc-edit-test-button.png

          In the test event page, a test example is created as follows:

          { 
              "x": 1, 
              "y": 2, 
              "z": 3 
          } 

          Select “HelloWorld template” as the event template and name the event “biecfccompute”. Enter the above json code in the test code box below, and click the “Create” button. This test event will be used again later.

          cfc-edit-test-create.png

          A pop-up message shows “Create it successfully”, and then click “Execute” to test the added add and add2 functions.

          cfc-edit-test-success.png

          Test result {"result": 6, "result2": 6} is the result of our add function, and you can see that the output result is correct.

          Then, test the add2 function. Return to the function code page, modify the processing program as index.add2, and click “OK” button.

          cfc-edit-change.png

          After modifying the processing program, you still have to test to check whether it is modified successfully. Select the biecfccompute test event to test again, and click “Execute” button.

          cfc-edit-test-add2.png

          You can see the result is {"result": 6} and the output result is correct.

          Lastly, a new version has to be released and then it can be imported by the storage volume.

          cfc-edit-publish.png

          CFC Create a Storage Volume

          Return to the Baidu IntelliEdge console, and two CFC-related storage volumes are required - bie-cfc-compute-code and bie-cfc-compute-conf, which are used to save code and configuration respectively.

          bie-cfc-compute-code Storage Volume

          First, click “Create a storage volume” on the storage volume page, select CFC function template, name it bie-cfc-compute-demo and click OK.

          cfc-compute-volume-create.png

          Enter the CFC storage volume page, and click “Import a CFC Function”.

          cfc-compute-volume-create02.png

          Select the CFC function bie-cfc-demo just created.

          cfc-compute-volume-addcfc.png

          After import, release the new version.

          cfc-compute-volume-publish.png

          bie-cfc-compute-conf Storage Volume

          On the storage volume page, click “Create a storage volume”, name it bie-cfc-compute-conf , and select baetyl template configuration for the template.

          cfc-conf-volume-create.png

          For service.yml of the storage volume, enter the following configuration and save it.

          functions: 
            - name: 'add' 
              handler: 'index.add' 
              codedir: 'var/db/baetyl/compute' 
            - name: 'add2' 
              handler: 'index.add2' 
              codedir: 'var/db/baetyl/compute' 

          cfc-conf-volume-edit.png

          Lastly, you have to release the new version.

          6-publish-cfc-conf.png

          baetyl-hub Create a Storage Solume for Services

          There are three storage volumes, namely:

          • bie-demo-hub-conf
          • bie-demo-hub-log
          • bie-demo-hub-data

          For details, see Release and distribute a configuration . You only need to create these three storage volumes.

          baetyl-function-manager Create a Storage Volume for Services

          On the storage volume page, click “Create a storage volume” button, and select baetyl template configuration for the template.

          cfc-manager-volume-create.png

          Enter the bie-cfc-func-conf storage volume, modify the service.yml, enter the following configuration and save it.

          hub: 
            address: tcp://local-hub:1883 
            username: test 
            password: hahaha 
          rules: 
            - clientid: localfunc-1 
              subscribe: 
                topic: t 
              function: 
                name: add 
              publish: 
                topic: t/add 
            - clientid: localfunc-2 
              subscribe: 
                topic: t 
              function: 
                name: add2 
              publish: 
                topic: t/add2 
          functions: 
            - name: add 
              service: function-compute 
            - name: add2 
              service: function-compute 
          logger: 
            path: var/log/baetyl/service.log 
            level: "debug" 

          cfc-manager-volume-edit.png

          Similarly, you have to release the new version.

          cfc-manager-volume-publish.png

          Add/Create a Service

          All the 6 storage volumes to be used are created.

          Then, enter the Baidu IntelliEdge core page, select the core created in our first step, and configure it.

          baetyl-hub Service Configuration

          Click “Add a service” button.

          10-create-service.png

          Name it local-hub, select baetyl-hub as the module, enter 1883:1883 for port mapping, and then click storage volume mounting to mount the three corresponding storage volumes.

          It is not mandatory for the mapping port to be 1883, but the specific mapping port should be consistent with the configuration file of each service. For example, for tcp connection, the local-hub service monitors the port 8883, and when other services establish connection with the local-hub service, set the connection port to be 8883.

          11-hub.png cfc-core-hub-create04.png

          Mount three storage volumes - bie-demo-hub-conf, bie-demo-hub-log and bie-demo-hub-data.

          12-hub-conf.png 13-hub-log.png 14-hub-data.png

          Finally, you can see that a total of three storage volumes are mounted.

          15-three-vol.png

          baetyl-function-manager Service Configuration

          Click “Add a service”, name it local-function-manager, and select baetyl-function-manager for the module.

          16-service-manager.png

          Mount the bie-cfc-func-conf storage volume.

          20-cfc-config.png 21-cfc-config.png

          baetyl-function-python36 Service Configuration

          Click “Add a service”, name it function-compute, and select baetyl-function-python36 for the module.

          22-python36.png

          Mount the bie-cfc-compute-conf and bie-cfc-compute-code for the storage volumes.

          23-compute.png 18-man-ser.png 24-code.png

          View the Final Configuration Result and Distribute it

          The service configuration is completed, and our core services are as follows:

          19-all-ser.png

          Check that the service configuration is correct, and click “Release” button to release this version.

          cfc-core-publish.png

          After the version is released, you can see that the latest version is the “v2” version. Click “Download” button, and note that the master program should be selected to download.

          cfc-core-download.png

          For the startup steps, see Getting Started.

          Lastly, use mqttbox to send messages for testing. For the use of mqttbox, see Use MQTTBox to Subscribe Data Check Results. Use MQTTBox to Subscribe Data Check Results). The input example is {"x":1,"y":2,"z":3}, and the result is shown in the figure. The output result is exactly the result computed in the CFC functions add and add2.

          mqttbox-result.png

          Previous
          How to add and call python codes in a custom storage volume
          Next
          Application Template