Configure Several Functions in CFC and Distribute Them to Edge Node
This chapter mainly introduces how to configure multiple functions imported from Baidu CFC in the function application.
Create CFC function
The following will take Python3.6 as an example for illustration.
Enter Function Computing CFC from Baidu AI Cloud console. We select the "South China - Guangzhou" region, click the "Create Function" button, and select "Empty Function".
Then, select the function name as "bie-cfc-demo" and the runtime as "Python 3.6", and click the "Submit" button.
After the successful creation, we click "Enter Code Edit Page".
It will automatically generate the "Hello World" function on the CFC Function Code page.
You can have one function entry only, but it supports to add multiple functions, which can be called with each other. We add two new functions:
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
You add the functions add
and add2
. Then, you can see that the add
function calls the add2
function. Afterward, click the "Save" button for the current code, and modify the entry function to index.add
in the handler, and click the "OK" button.
After the code is added, carry out a test to confirm whether the code is executed normally. Click the "Test" button on the Function Code page as shown in the figure below:
We create a new test case on the Test Case page as follows:
{
"x": 1,
"y": 2,
"z": 3
}
We select the case template as "HelloWorld Template" and the case name as "biecfccompute", enter the above Json code in the following test code box, and click the "Create" button. We will use this test case later.
The pop-up message shows "Created Successfully", and then we click the "Execute" button to test the added functions add
and add2
.
The test result {"result": 6, "result2": 6}
is the result of our add
function and we can see that the output result is correct.
Then, we test the add2
function, we return to the Function Code page, modify the handler to index.add2
, and click the "OK" button.
After modification, we need to test whether the handler is modified successfully. Select the biecfccompute
test case and click the "Execute" button.
You can see that the result is {"result": 6}
, which is correct.
Finally, we need to release a new version to be introduced by the storage volume.
Create CFC Configuration Item
Return to the Baidu IntelliEdge console. Then, you need to create the "Configuration Item" for introducing the CFC function created in the previous step.
You need to set the configuration item function tag, indicating that this is a function configuration item that you can reference by the function application.
Then select the Import Function option, select the CFC function name and version, and click "OK" to complete the import operation.
Create CFC Application
Create the function application, and select the CFC configuration item created in the previous step.
At this point, the CFC function importing is successful.
Create Node
Next, we will configure the edge rule modules baetyl-broker and baetyl-rule to call the CFC function.
Create the node on the Create Node interface.
Bind the CFC application created in the previous step to this node.
Configure baetyl-rule
Select the module baetyl-rule on the "Application Deployment" menu bar, and click "View".
Then click "Data Volume", and select the first configuration item for configuration.
The configuration of baetyl-rule is shown as follows:
rules:
- name: rule1
source:
topic: broker/topic1
target:
topic: broker/topic2
function:
name: bie-cfc-compute/add
- name: rule2
source:
topic: broker/topic1
target:
topic: broker/topic3
function:
name: bie-cfc-compute/add2
logger:
level: debug
encoding: console
Update and save the configuration item.
Configure baetyl-broker
Configure the baetyl-broker by follow the steps above. The configuration files are shown as follows:
listeners:
- address: tcp://0.0.0.0:8004
principals:
- username: test
password: hahaha
permissions:
- action: pub
permit: ["#"]
- action: sub
permit: ["#"]
logger:
level: info
Add port 8004 on the Service Configuration interface of baetyl-broekr, and expose the service to the host.
After successful configuration, we can see the following phenomena when kubectl is executed on the end side.
End side verification
After the above services are deployed to the end side, we send the message to the baetyl-broker with the MQTT Box for testing.
Firstly, we connect it with the MQTT Box, and the connection information is shown as follows:
After successful connection, it is shown as follows:
Our input case is the {"x":1, "y":2, "z":3}
result as shown in the figure below, and the output result is the computing result of the functions add and add2 in the CFC function.