Help Center> FunctionGraph> User Guide> Getting Started> Creating and Initializing a Function

Creating and Initializing a Function

Creating a Function

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click Create Function.
  3. Configure the function information by performing the following steps:

    1. Configure the basic information according to Table 1. The parameters marked with an asterisk (*) are mandatory.
      Table 1 Basic information

      Parameter

      Description

      Template

      In this example, select Create from scratch.

      If you need to use a template, see Function Templates.

      *Function Name

      Name of the function, which must meet the following requirements:

      • Consists of 1 to 60 characters, and can contain letters, digits, hyphens (-), and underscores (_).
      • Starts with a letter and ends with a letter or digit.

      Enter HelloWorld.

      *FunctionGraph Version

      Select FunctionGraph v2.

      NOTE:

      This parameter is available only in the CN Southwest-Guiyang1 and CN North-Beijing4 regions.

      *App

      App to which the function belongs. You can create multiple functions under an app. An app name must meet the following requirements:

      • Consists of 1 to 60 characters, and can contain letters, digits, hyphens (-), and underscores (_).
      • Starts with a letter and ends with a letter or digit.

      Select default, the default app on FunctionGraph.

      Agency

      An agency is required if FunctionGraph accesses other cloud services. For details on how to create an agency, see Creating an Agency.

      No agency is required if FunctionGraph does not access any cloud services.

      In this example, select Use no agency.

      *Enterprise Project

      Select an enterprise project to add the function to it.

      NOTE:

      If Enterprise Project Management Service (EPS) is not enabled, this parameter is unavailable. For details, see Enabling the Enterprise Project Function.

      Description

      Description of the function, which cannot exceed 512 characters. Enter test.

      You can use no agency or specify the same agency for function configuration and execution. To reduce unnecessary performance consumption, select Specify an exclusive agency for function execution and set different agencies for function configuration and execution. Figure 1 shows the agency options.
      Figure 1 Setting agencies
      • Function configuration agency: Specify an agency with DMS or DIS permissions for a function that needs to use DMS or DIS triggers. If such an agency is not specified or the specified agency does not exist, you cannot create DMS or DIS triggers for the function.
      • Function execution agency: After specifying such an agency, you can obtain a token and AK/SK from the context in the function handler for accessing other cloud services.
    2. Configure the code information according to Table 2. The parameter marked with an asterisk (*) is mandatory.
      Table 2 Code information

      Parameter

      Description

      Runtime

      FunctionGraph supports Python 2.7, Python 3.6, Node.js 6.10, Node.js 8.10, Node.js 10.16, Node.js 12.13, Java 8, Go 1.8, C# (.NET Core 2.0), C# (.NET Core 2.1), C# (.NET Core 3.1), and PHP 7.3.

      In this example, select Node.js 6.10.

      *Handler

      For a Node.js function, the handler must be named in the format of [file name].[function name], which must contain a period (.).

      Enter index.handler, which indicates that the file name is index.js and the function name is handler.

      Code Entry Mode

      Select Edit code inline, and use the displayed sample code.

      • When you write code in Python, do not name your package with the same suffix as a standard Python library, such as json, lib, and os. Otherwise, an error indicating a module loading failure will be reported.
      • If the code to be uploaded contains sensitive information (such as account passwords), encrypt the code to prevent information leakage.

      The sample code enables you to obtain test events and print test event information. The code is as follows:

      exports.handler = function (event, context, callback) {
          const error = null;
          const output = `Hello message: ${JSON.stringify(event)}`;
          callback(error, output);
      }

  4. In the right pane of the page, review the function configuration and billing information, and click Create Now. Figure 2 shows the details.

    Figure 2 Creating a function

    After the function is created, the memory is 128 MB and the execution timeout is 3s. To modify them, see Modifying Function Configurations.

Initializing the Function

If initialization is enabled for a function, a specified initializer will be invoked to initialize the function, and then a handler will be invoked to process requests. For details on how to create a function, see Creating a Function.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click the name of the created function.
  3. On the displayed function details page, enable Initialization, and configure the code information, as shown in Figure 3.

    Figure 3 Setting an initializer
    • Set the initializer in the same way as the handler. For example, for a Node.js or Python function, set an initializer name in the format of [file name].[initialization function name].
    • For details on how to configure the code information, see Table 2.
    • If you enable function initialization, the following parameters of a new version cannot be modified and their default values will be used:

      Initializer and Handler on the Code tab page

      Initialization Timeout (s), Max. Instances per Function, VPC, and Subnet on the Configuration tab page (Note that the Max. Instances per Function parameter is available only in the CN Southwest-Guiyang1 and CN North-Beijing4 regions.)

  4. Click the Configuration tab, set the initialization timeout, as shown in Figure 4.

    Figure 4 Setting the initialization timeout
    • The value ranges from 1s to 300s.
    • For details on how to configure the environment information, see Table 4.

  5. Click Save in the upper right corner to save the initialization settings.

Testing the Function

For details on how to configure test events and test functions, see Test Management.