In this documentation example, we utilize Serverless Cloud Function (SCF), Cloud Object Storage (COS), and Tencent Cloud MySQL Database. Herein, COS is employed to store the log files that require analysis, SCF is used to download these log files from COS and perform statistical analysis, and the results of this analysis are written into the MySQL database.
2. Refer to Creating a Bucket to establish a bucket, with the primary parameter information set as follows:
Name: Designate it as loganalysis.
Region: For this case study, Beijing is selected as the region. When using an external network connection, the Bucket can be in a different region from the MySQL cloud database.
Access Permission: Select "Private Read/Write".
Create a MySQL Cloud Database
1. Since the database requires a purchase, you can opt to buy the TencentDB for MySQL starter model in your target region. For this case study, Beijing is selected as the region.
3. Upon completion of the purchase, add an accessible username and password to the database, and create a new instance. In this case study, the instance is named mason_demo.
Creating SCF functions
1. Log in to the Serverless Console and navigate to the Function Service page.
2. At the top of the "Function Service" page, select the Beijing region, and click on New to navigate to the new function page. Set the following parameter information and click on Next. As shown in the figure below:
Creation method: select Template.
Fuzzy Search: Enter "Log Analysis Write Database" and perform a search. Click on View Details in the template to view related information in the pop-up "Template Details" window. Download operations are supported.
3. The function name is filled in by default and can be modified as needed. Follow the guide to configure environment variables, execution roles, and private networks:
Environment variable
Execution Roles
Virtual Private Cloud
When using this template function, you need to add environment variables in the function configuration as prompted. The filling method can be referred to the following figure:
key
value
dbhost
Please refer to Accessing MySQL Database for retrieval. This document uses the public network as an example, with the format being bj-cdb-xxxxx.sql.tencentcdb.com:00000. The number following the colon represents the dbport.
dbport
dbuser
The username of the created TencentDB for MySQL database.
dbpwd
The set MySQL account password.
dbname
The name of the database instance to be written to, in this case, we use 'mason_demo' as an example.
1Select "Enable" and choose "Configure and use SCF template execution role". This will automatically create and select an SCF template execution role associated with full read/write permissions for COS and CDB. Alternatively, select "Use existing role" and choose an existing role with the aforementioned permissions from the dropdown list. In this document, we use "Configure and use SCF template execution role" as an example. As shown in the figure below:
Note
You can also directly replace the SecretId and SecretKey in the function code with the ones actually used in your account. These can be obtained from the API Key Management page.
If the database uses a private network address, the function needs to enable Private Network and select the same VPC and subnet as the database. As illustrated in the figure below:
Configuring COS Trigger
In "Trigger Configurations", select "Custom Creation" and fill in the relevant parameter information. As depicted in the figure below:
The main parameter information is as follows, please keep the rest of the configurations as default:
Trigger Method: Select "COS Trigger".
COS Bucket: Select the storage bucket 'loganalysis' that was created in the Create COS Bucket step.
Event Type: Select "Create All".
Click Done to complete the creation of the function and trigger.
Testing Function
1. Download the log file from the Test Sample and extract demo-scf1.txt.
2. Switch to the Object Storage Console, select the created storage bucket 'loganalysis', and click Upload File.
3. In the pop-up "Upload File" window, select the downloaded demo-scf1.txt, and click Confirm Upload.
4. Switch to the Serverless Console to view the execution results.
Select the Log Query tab on the function details page to view the printed log information, as shown below:
5. Switch to the MySQL management interface to view the analysis results in the database.
Note
You can write specific processing methods based on your own log format, and the database write method can also be modified to incremental writing.