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”.
Then, name the function “bie-cfc-demo”, select “Python 3.6” for operation, and click “Submit”.
After it is created successfully, click “Enter the code editing page”.
The CFC function code page automatically generates a “Hello World” function.
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.
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:
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.
A pop-up message shows “Create it successfully”, and then click “Execute” to test the added add
and add2
functions.
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.
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.
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 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.
Enter the CFC storage volume page, and click “Import a CFC Function”.
Select the CFC function bie-cfc-demo
just created.
After import, release the new version.
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.
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'
Lastly, you have to release the new version.
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.
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"
Similarly, you have to release the new version.
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.
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.
Mount three storage volumes - bie-demo-hub-conf
, bie-demo-hub-log
and bie-demo-hub-data
.
Finally, you can see that a total of three storage volumes are mounted.
baetyl-function-manager Service Configuration
Click “Add a service”, name it local-function-manager
, and select baetyl-function-manager
for the module.
Mount the bie-cfc-func-conf
storage volume.
baetyl-function-python36 Service Configuration
Click “Add a service”, name it function-compute
, and select baetyl-function-python36
for the module.
Mount the bie-cfc-compute-conf
and bie-cfc-compute-code
for the storage volumes.
View the Final Configuration Result and Distribute it
The service configuration is completed, and our core services are as follows:
Check that the service configuration is correct, and click “Release” button to release this version.
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.
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
.