Help Center/ FunctionGraph/ Getting Started/ Creating a Function from Scratch and Executing the Function
Updated on 2024-10-23 GMT+08:00

Creating a Function from Scratch and Executing the Function

This section describes how to quickly create and test a HelloWorld function on the FunctionGraph console.

Prerequisites

  1. Register with Huawei Cloud and complete real-name authentication.

    For details, see Registering a HUAWEI ID and Enabling Huawei Cloud Services and Real-Name Authentication.

    If you already have a Huawei account and have completed real-name authentication, skip this step.

  2. View free quota.

    FunctionGraph offers a free tier every month, which you can share with your IAM users. For details, see Free Tier

    If you continue to use FunctionGraph after the free quota is used up, your account goes into arrears if the balance is less than the bill to be settled. To continue using your resources, top up your account in time..

  3. Grant the FunctionGraph operation permissions to the user.

    To perform the operations described in this section, ensure that you have the FunctionGraph Administrator permissions, that is, the full permissions for FunctionGraph. For more information, see section "Permissions Management".

Step 1: Create a Function

  1. Log in to the FunctionGraph console. In the navigation pane, choose Functions > Function List.
  2. Click Create Function in the upper right corner and choose Create from scratch.
  3. On the displayed page, enter HelloWorld for Function Name and retain the default values for other parameters by referring to Figure 1, and click Create Function. The following describes the parameters.
    • Function Type: Select Event Function.
    • Region: The default value is used. You can select other regions.

      Regions are geographic areas isolated from each other. Resources are region-specific and cannot be used across regions through internal network connections. For low network latency and quick resource access, select the nearest region.

    • Project: The default value is the same as the selected region.
    • Function Name: enter HelloWorld.
    • Enterprise Project: The default value is default. You can select the created enterprise project.

      Enterprise projects let you manage cloud resources and users by project.

    • Agency: By default, no agency is used. You can select an existing agency.

      Specify an agency if you want to delegate FunctionGraph to access other cloud services, such as LTS and VPC.

    • Runtime: Select a runtime to compile the function. Default: Node.js 16.17. You can select another runtime.
Figure 1 Configuring basic information
  1. Configure the code source, copy the following code to the code window, and click Deploy.
    The sample code enables you to obtain test events and print test event information.
    exports.handler = function (event, context, callback) {
        const error = null;
        const output = `Hello message: ${JSON.stringify(event)}`;
        callback(error, output);
    }

Step 2: Test the Function

  1. On the function details page, click Test. In the displayed dialog box, create a test event.
  2. Select blank-template, set Event Name to test, modify the test event as follows, and click Create.
    {
        "hello": "function"
    }
    Figure 2 Configuring a test event

Step 3: View the Execution Result

Click Test and view the execution result on the right.

  • Function Output: displays the return result of the function.
  • Log Output: displays the execution logs of the function.
  • Summary: displays key information of the logs.
    Figure 3 Viewing the execution result

A maximum of 2 KB logs can be displayed. For more log information, see section "Querying Function Logs".

Related Information