Customize Function Running Packet and Distribute It to Edge Node
Typically, we need to refer to the third-party dependency during the writing of functions. This section introduces how to upload the code and third-party dependency to BOS and introduces this function into the function application.
The following takes Python3.6 as an example for illustration.
Create Configuration Item
Code development
Before the code is uploaded to the cloud, we need to develop and test the code on the development machine.
We write the case to get the headers information of https://cloud.baidu.com/ , and create a new folder with the code file naming index.py
as follows:
#!/usr/bin/env python3
# -*-coding: utf-8 -*-
import requests
def handler(event, context):
"""
data: {"action": "A"}
"""
if 'action' in event:
if event['action'] == 'A':
r = requests.get('https://cloud.baidu.com/')
if str(r.status_code) == '200':
event['info'] = dict(r.headers)
else:
event['info'] = 'exception found'
else: event['info'] = 'action error'
else:
event['error'] = 'action not found'
return event
You can see that we use the requests
library. Next, we need to download the requests
source code to this folder and type the codes in the command line:
pip3 download requests
After downloading, we find that several compressed packages end with .whl in the folder.
Decompress the .whl compressed package to get the source code.
unzip -d . "*.whl"
In this way, we get the source code, but we do not need the compressed packages and .dist-info. To minimize the file size, we can delete these files by typing the following codes in the command line:
rm -rf *.whl *.dist-info
Then, we can carry out a simple test to view index.py
whether outputs what we want correctly.
Create a test.py
file in this folder, whose code is shown as follows:
import index
event = {}
event['action'] = "A"
print(index.handler(event, ""))
You can see that it outputs the handler content we want correctly. After the test, we can delete this file.
Compress index.py
and 5 source code folders into the ZIP package, and then upload it to BOS.
Create configuration item
We need to create the "Configuration Item" to import the function uploaded to BOS in the previous step.
We need to set the configuration item function tag to indicate that this is a function configuration item that can be referenced by the function application.
Then select "Import File", select the file uploaded to BOS at just, select "Decompress", and click "OK" to complete the import operation.
Application Creation
Create the function application, and select the configuration item created in the previous step.
Create Node
Next, we configure the edge rule modules baetyl-broker and baetyl-rule to call the functions above.
Create the node on the Create Node interface.
And bind the applications 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-python/process
logger:
level: debug
encoding: console
The full path of function is [Service Name/Function Entry] and the full call path of python function in the above process is bie-python/process.
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 the 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 {"action":"A"}
result as shown in the figure below, and the output result is the header content of the request <25>https: //cloud.baidu.com/</25>.