Updated on 2024-03-05 GMT+08:00

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

  1. Open VSCode, search for Huawei Cloud FunctionGraph in the app store, and install it.
    Figure 1 Searching for and installing Huawei Cloud FunctionGraph
  2. 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

  1. 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.
  1. Select a region to view function information.

  2. 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

  1. 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.

  1. After the local function is created, the handler file is automatically opened.
  2. 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

  1. 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));
  2. 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)
  3. 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.

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
    1. Right-click the target function and choose Invoke Function... from the shortcut menu.
    2. 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.

  • 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
    1. Right-click the function to be deleted and choose Delete... from the shortcut menu.
    2. 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.