Local Debugging with VSCode
Introduction
Huawei Cloud FunctionGraph is a Visual Studio Code (VSCode) plug-in of Huawei Cloud serverless products. With this plug-in, you can:
- Quickly create local functions.
- Run and debug local functions and deploy them to the cloud.
- Pull the function list from the cloud, call cloud functions, and upload ZIP packages to the cloud.
Prerequisites
You have downloaded the VSCode tool (later than 1.63.0) and installed it.
Installing the Plug-in
- Open VSCode, search for Huawei Cloud FunctionGraph in the app store, and install it.
Figure 1 Searching for and installing Huawei Cloud FunctionGraph
- After the installation is successful, Huawei Cloud FunctionGraph is displayed in the plug-in list.
Figure 2 Installed plug-ins
Logging In to FunctionGraph Plug-in
- Click the Huawei Cloud FunctionGraph plug-in icon, click the login link, and select a login mode. If you select login with AK/SK, obtain an AK/SK. For details, see Creating an Access Key.
- Select a region to view function information.
- Show or hide desired regions, or log out of your account by referring to the following figure.
- Show region in the Explorer: Show the regions where you need to perform operations.
- Hide region from the Explorer: Hide the regions you do not need.
- Log Out: Log out of your account.
Creating a Function
- On the plug-in panel, select Create Function or press Ctrl+Shift+p to search for the Create Function command. Then, specify the runtime, template, function name, and local folder as prompted. A function is created in the specified folder.
- After the local function is created, the handler file is automatically opened.
- You can customize the function's configuration by modifying the automatically generated configuration file. The parameters are as follows:
HcCrmTemplateVersion: v2 Resources: Type: HC::Serverless::Function Properties: Name: functionName //Function name Handler: handler // Handler of the function Runtime: runtime // Function runtime CodeType: inline // Default CodeFileName: index.zip // Default CodeUrl: "" Description: '' // Function runtime MemorySize: 128 // Memory for executing the function Timeout: 30 // Function timeout, in seconds. Version: latest // Default Environment: Variables: {} // Environment variables InitializerHandler: "" // Function initializer InitializerTimeout: 0 // Initialization timeout of the function EnterpriseProjectId: "0" // Enterprise project FuncType: v2 URN: "" // Function URN, which is generated after the function is downloaded.
Deploying a Function
- Prerequisites
Ensure that the function code path is correct. The code of Node.js, Python, and PHP functions is stored in the src directory, and the code of other functions is stored in the root directory.
On the plug-in panel, select Deploy Function or press Ctrl+Shift+p to search for the Deploy Function command, and select a function and region as prompted.
- If the deployment is successful, a success message is displayed in the lower right corner of the page. Switch to the target region to view the deployment result.
- If the deployment fails, view the error log in the Output area and rectify the fault.
Local Debugging
- Node.js
- Prerequisites
Node.js has been installed in the local environment.
- Default mode
Click Local Debug of the handler method, configure the event content, and click Invoke for debugging.Figure 3 Clicking Local Debug
Figure 4 Configuring the event content
- Debugging with VS Code
Create the main.js file in the function folder and copy the following content to this file. Click the icon on the left. Then, click Add Config, select Node.js, and press F5 to start debugging.
const handler = require('./index'); //Path of the function handler file. Modify it as required. const event = { 'hello': 'world' }; //Test event. Modify it as required. const context = {}; //Context class. console.log(handler.handler(event, context));
- Prerequisites
- Python
- Prerequisites
Python has been installed in the local environment.
Create the main.py file in the function folder and copy the following content to this file. Click the icon on the left. Then, click Add Config, select Python, and press F5 to start debugging.
import sys import index #Path of the function handler file. Modify it as required. #The main method is used for debugging, and event is the selected debugging event. if __name__ == '__main__': ....event = { 'hello': 'world' } #Test event. Modify it as required. context = '' content = index.handler(event, context) ....print('Returned value:') print(content)
- Prerequisites
- Java
- Prerequisites
Java has been installed. VS Code supports Java testing.
In the test directory of the function folder, open the TriggerTestsTest.java file, and click the icon on the left. Then, click Add Config, select Java, and press F5 to start debugging.
- Prerequisites
Other Functions
- Opening in Portal
Right-click the target function, and choose Open in Portal. The function details page is displayed.
- Executing a Cloud Function
- Right-click the target function and choose Invoke Function... from the shortcut menu.
- In the Invoke Function panel, select the event to be passed and click Invoke. The function log and result are displayed in the Output area.
- Downloading a Cloud Function
- Prerequisites
You have been granted the permission (obs:object:GetObject) for obtaining bucket objects.
Right-click the function you want to download and choose Download... from the shortcut menu. The function code is downloaded from the cloud to the specified local path, and the handler file is automatically opened.
- Prerequisites
- Updating a Cloud Function
Right-click the target function, choose Upload Function... from the shortcut menu, and select a ZIP package to upload.
- Deleting a Cloud Function
- Right-click the function to be deleted and choose Delete... from the shortcut menu.
- In the confirmation dialog box, click Delete to delete the function.
- Copying URN
Right-click the target function and choose Copy URN from the shortcut menu.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.