Function Management

Function is a combination of code, runtime, resources, and settings required to achieve a specific purpose. It is the minimum unit that can run independently. A function can be triggered by triggers and automatically schedule required resources and environments to achieve expected results.

An app groups functions as a service logic unit for easier management. You can create multiple functions under an app.

Editing Code Inline

FunctionGraph allows you to edit function code in the same way as managing a project. You can create and edit files and file folders. After you upload a ZIP code package, you can view and edit the code on the console. Table 1 describes the menus of the inline editor.

Table 1 Menus of the inline editor

Menu

Description

File

Creates files and folders. You can create files, create folders based on templates, and close all files.

Edit

Edits function code. You can perform the undo, edit, or comment operation.

Find

Finds and replaces code.

Go to

Goes to a certain location in the code. You can go to a specified line, brackets, or the next problematic line.

View

Provides common functions. You can view the command palette and change the display theme.

Test

Tests a function online, and displays the test result, digest, and logs.

  • Java is a compiled language, which does not support editing code inline. If your function does not use any third-party dependencies, you can upload a function JAR file. If your function uses third-party dependencies, compress the dependencies and the function JAR file into a ZIP file, and then upload the ZIP file.
  • If you edit code in Go, compress the compiled file in ZIP format, and ensure that the name of the dynamic library file is consistent with the plug-in name of the handler. For example, if the name of the dynamic library file is function.so, set the handler to function.Handler according to Table 2.
  • When creating a ZIP file, place the handler file under the root directory to ensure that your code can run normally after being decompressed.
  • For details about editing code inline, see Using CloudIDE Inline.

Modifying Function Code

After a function is created, the default version is latest. Each function has the latest version. You can modify a function based only on its latest version.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click a function name.
  3. (Optional) On the function details page, select the latest version and click the Code tab.
  4. On the Code tab page, modify the code information according to Table 2 and Table 3.

    Table 2 Code information

    Parameter

    Description

    Handler

    • For a Node.js or Python function, the handler must be named in the format of [file name].[function name], which must contain a period (.). Enter myfunction.handler, which indicates that the file name is myfunction.js (myfunction.py for a Python function and myfunction.java for a Java function) and the function name is handler.
    • For a Java function, the handler must be named in the format of [package name].[file name].[function name], for example, com.xxxxx.exp.Myfunction.myHandler.
    • For a Go function, the handler must be named in the format of [plug-in name].[function name], for example, function.Handler. The function name must start with an uppercase letter and the handler name can contain a maximum of 128 characters.
    • For a C# function, the handler must be named in the format of [.NET assembly file name]::[namespace and class of the handler function]::[handler function name], for example, HelloCsharp::Example.Hello::Handler.

    Initializer

    You can enable function initialization on the Code tab page after creating a function. The initializer must be named 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].

    NOTE:

    This parameter is not required if function initialization is disabled. For details on how to create an initialization function, see Initializing the Function.

    Dependencies

    Third-party software packages required by the function. You can manage your dependencies by uploading them to FunctionGraph. If the dependencies are too large, upload them through OBS. For more information, see Dependent Libraries.

    NOTE:

    Except your private dependencies, FunctionGraph provides some common dependencies, which you can choose when creating a function.

    Code Entry Mode

    Method of entering the function code. For details about the supported code entry modes, see Table 3.

    Table 3 Code entry modes

    Runtime

    Code Entry Mode

    Description

    Node.js 6.10/Node.js 8.10/Node.js 10.16/Node.js 12.13

    Edit code inline

    Edit code in the code box. For more information, see Table 4.

    Upload ZIP file

    Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 10 MB. If it exceeds 10 MB, upload the ZIP file using an OBS bucket.

    Upload file from OBS

    Paste the link URL of the OBS bucket storing a code ZIP file.

    Python 2.7/Python 3.6

    Edit code inline

    Edit code in the code box. For more information, see Table 4.

    Upload ZIP file

    Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 10 MB. If it exceeds 10 MB, upload the ZIP file using an OBS bucket.

    Upload file from OBS

    Paste the link URL of the OBS bucket storing a code ZIP file.

    Java 8

    Upload ZIP file

    Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 10 MB. If it exceeds 10 MB, upload the ZIP file using an OBS bucket.

    Upload JAR file

    Click Select File and upload a local JAR file to FunctionGraph. The size of the JAR file to be uploaded cannot exceed 10 MB. If it exceeds 10 MB, convert it into a ZIP file, and upload the ZIP file to OBS.

    Upload file from OBS

    Paste the link URL of the OBS bucket storing a code ZIP file.

    Go 1.8/Go 1.x

    Upload ZIP file

    Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 10 MB. If it exceeds 10 MB, upload the ZIP file using an OBS bucket.

    Upload file from OBS

    Paste the link URL of the OBS bucket storing a code ZIP file.

    C# (.NET Core 2.0)/C# (.NET Core 2.1)/C# (.NET Core 3.1)

    Upload ZIP file

    Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 10 MB. If it exceeds 10 MB, upload the ZIP file using an OBS bucket.

    Upload file from OBS

    Paste the link URL of the OBS bucket storing a code ZIP file.

    PHP 7.3

    Upload ZIP file

    Click Select File and upload a local code package to FunctionGraph. The size of the ZIP file to be uploaded cannot exceed 10 MB. If it exceeds 10 MB, upload the ZIP file using an OBS bucket.

    Upload file from OBS

    Paste the link URL of the OBS bucket storing a code ZIP file.

    If the code to be uploaded contains sensitive information (such as account passwords), encrypt the code to prevent information leakage.

    Table 4 Menu description of the inline editor

    Menu

    Description

    File

    Creates files and folders. You can create files, file templates, and Python modules, and close all files.

    Edit

    Edits function code. You can undo or redo your editing, delete or join lines, add comments, fold and unfold lines, and sort lines in ascending or descending order.

    Find

    Finds and replaces code.

    Go to

    Goes to a certain location in the code. You can go to a specified line, brackets, or the next problematic line.

    View

    Provides common functions. You can view the command palette and change the display theme.

    Test

    Tests a function online, and displays the test result, digest, and logs.

    • Java is a compiled language, which does not support editing code inline. If your function does not use any third-party dependencies, you can upload a function JAR file. If your function uses third-party dependencies, compress the dependencies and the function JAR file into a ZIP file, and then upload the ZIP file.
    • If you edit code in Go, compress the compiled file in ZIP format, and ensure that the name of the dynamic library file is consistent with the plug-in name of the handler. For example, if the name of the dynamic library file is function.so, set the handler to function.Handler according to Table 2.
    • When creating a ZIP file, place the handler file under the root directory to ensure that your code can run normally after being decompressed.
    • For details about editing code inline, see Using CloudIDE Inline.

  5. Click Save.

Modifying Function Configurations

You can define variables for your function and modify the function configurations.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click a function name.
  3. (Optional) On the function details page, select the latest version and click the Configuration tab.
  4. Set the parameters described in Table 5.

    Table 5 Configuration parameters

    Parameter

    Description

    Description

    Description of the function, which cannot exceed 512 characters. Modify the description as necessary.

    Agency

    Used to delegate FunctionGraph to access other cloud services. For example, an agency is required when FunctionGraph accesses cloud services such as OBS or SMN.

    Select an agency from the drop-down list. If there is no agency in the drop-down list, click Create Agency to create one on the IAM console.

    For details, see Creating an Agency.

    Memory (MB)

    Value: 128, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584, or 4096. Modify the memory as necessary.

    Initialization Timeout (s)

    Maximum duration the function can be initialized. Set this parameter if you enable function initialization.

    The value ranges from 1s to 300s.

    Execution Timeout (s)

    Maximum duration the function can be executed.

    The value ranges from 3s to 900s.

    VPC Access

    If VPC access is enabled for the function, specify an agency with VPC management permissions.

    Encryption Settings

    Key-value pairs defined to store sensitive information. Keys can contain letters, digits, and underscores (_), and must start with a letter. Values will be encrypted for storage and will not be displayed in plaintext.

    Click Add now? and set keys and values.

    To delete a key-value pair, click Delete next to it.

    Environment Variables

    Key-value pairs defined for the function. Keys can contain letters, digits, and underscores (_), and must start with a letter.

    Click Add now? and set keys and values.

    To delete a key-value pair, click Delete next to it.

    • The maximum duration for returning a result is 90s. If both the set timeout and the actual execution duration of a function exceed 90s, a message is displayed indicating that the function has timed out. However, the backend is still running. You can view the return result on the Logs tab page.
    • When you define environment variables, FunctionGraph displays all your input information in plain text. To ensure security, do not include sensitive information.

  5. Click Save.

Exporting a Function

You can export the functions that you created.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click a function name.
  3. On the displayed function details page, choose Operation > Export function in the upper right corner.

    • A user can export only one function at a time.
    • The exported function resource package cannot exceed 50 MB.
    • The name of the exported function resource package is in the format of function name+MD5 value of function code.zip.
    • The exported function resource package does not include alias information.

Importing a Function

You can import a ZIP file that contains configurations and code into FunctionGraph.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click Import Function in the upper right corner.
  3. On the displayed page, select an existing app or define a new app, and then upload a ZIP file.

    If the ZIP file to be uploaded contains sensitive information (such as account passwords), encrypt it to prevent information leakage.

  4. Click OK.

    • You can import only one function at a time.
    • The resource package to be imported must be a ZIP package that contains a YAML configuration file and code file, as shown in Figure 1. The package cannot exceed 10 MB.
      Figure 1 Resource package
    • The resource package must contain only one YAML configuration file.
    • Fields in the YAML file must meet the same requirements as those configured when you create a function.
    • The code file is located through the codeFilename field in the YAML file.
    • For the Resource field in the YAML file, you can specify only one function.
    • When a dependency package is used, if the code in this package can be pulled from OBS, the import is successful. Otherwise, the import fails.
    • You can import an exported resource package again, but must specify a unique function name. The version of the imported function is latest by default.

    The following shows an example YAML configuration file. The fields in this file are described in Table 6.

    HcCrmTemplateVersion: v1
    Resources:
      HelloWorld:
        Type: HC::Serverless::Function
        Properties:
          Handler: index.handler
          Runtime: Node.js6.10
          CodeType: inline
          CodeFileName: index.js
          DependencyPkg: ""
          Description: ""
          MemorySize: 128
          Timeout: 3
          Version: latest
          Environment:
            Variables: {}
    Table 6 YAML file description

    Parameter

    Description

    HcCrmTemplateVersion

    Fixed value: v1.

    Resources

    Function details.

    Type

    Fixed value: HC::Serverless::Function.

    Properties

    Function information.

    Handler

    Handler of the function.

    Runtime

    Running environment.

    CodeType

    Code entry mode, which can be jar, zip, or inline.

    CodeFileName

    Code file name.

    DependencyPkg

    Dependency address.

    Description

    Description of the function.

    MemorySize

    Memory size.

    Timeout

    Function execution timeout.

    Version

    Function version.

    Environment

    Environment variables.

Disabling a Function

Disabled functions can no longer be executed.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click the name of the function you want to disable.
  3. On the displayed function details page, click Disable in the upper right corner.
  4. On the displayed page, click Yes. The function is disabled.

    • Only functions of the latest version can be disabled.
    • Versions published based on the disabled latest version of a function are also disabled and can never be enabled.
    • After disabling a function, you can modify its code but cannot execute the function.

Enabling a Function

Disabled functions can be enabled again as required.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click the name of the function you want to enable.
  3. On the displayed function details page, click Enable in the upper right corner.

Deleting a Function

You can delete unused functions to release resources.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Select a search criterion (function name, runtime, or app name), enter a keyword, and click to search for the function you want to delete.
  3. Click Delete in the same row as the function.
  4. In the Delete Function dialog box, enter DELETE, and click Yes to confirm the deletion.

Creating a Function App

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click Create Function.
  3. On the Create Function page, set the function information including App. For more information, see Creating a Function.
  4. Click Create Now. A function and app are created.

    1. Each function belongs to only one app.
    2. You can modify the description of the default app default, but you cannot delete this app.
    3. You can create a maximum of 400 function apps in a project.
    4. When you create a function through an API, if the package field is left blank, the function is created under the default app. If the package field is set to an app that does not exist, the app is created and then the function is created under the new app.
    5. Deleting an app will delete all functions of the app. Exercise caution when performing this operation.
    6. Each function name must be unique under a tenant.
    7. To add or modify a description for an app, click after the app and enter a description.

Deleting a Function App

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. In the app list, locate the app you want to delete and click Delete, as shown in Figure 2.

    Figure 2 Deleting an app

  3. In the Delete App dialog box, enter DELETE, select Delete all functions under the app, and click OK.

    Deleting an app will delete all functions of the app. Exercise caution when performing this operation.