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

Creating an Event Function

Overview

A function is customized code for processing events. You can create a function from scratch and configure the function based on site requirements.

FunctionGraph manages the compute resources required for function execution. After editing code for your function, configure compute resources on the FunctionGraph console.

You can create a function from scratch or by using a template or container image.

When creating a function from scratch, configure the basic and code information based on Table 1. The parameters marked with an asterisk (*) are mandatory.

Each FunctionGraph function runs in its own environment and has its own resources and file system.

Prerequisites

  1. You must be familiar with the programming languages supported by FunctionGraph. For details, see Supported Programming Languages.
  2. You have created a deployment package. For details, see Creating a Deployment Package.
  3. (Optional) You have created an agency. For details, see Configuring Agency Permissions.

Procedure

  1. Log in to the FunctionGraph console. In the navigation pane, choose Functions > Function List.
  2. On the Function List page, click Create Function in the upper right corner.
  3. Click Create from scratch and configure the function information by referring to Table 1. The parameters marked with an asterisk (*) are mandatory.
    Figure 1 Creating a function from scratch
    Table 1 Basic information

    Parameter

    Description

    * Function Type

    • Event functions: triggered by triggers.
    • HTTP functions: triggered once HTTP requests are sent to specific URLs.
      NOTE:
      • HTTP functions do not distinguish between programming languages. The handler must be set in the bootstrap file. You can directly write the startup command, and allow access over port 8000.
      • HTTP functions support APIG and APIC triggers only.
      • For details about how to use HTTP functions, see Creating an HTTP Function.

    *Region

    Select a region where you will deploy your code.

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

    Agency

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

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

    *Enterprise Project

    Select a created enterprise project and add the function to it. By default, default is selected.

    NOTE:

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

    Runtime

    Select a runtime to compile the function.

    NOTICE:

    CloudIDE supports Node.js, PHP, and Python only.

  4. Click Create Function. On the displayed Code tab page, continue to configure the code.

Configuring Code

  1. You can deploy the code based on the runtime you select. For details, see Creating a Deployment Package. After the deployment is complete, click Deploy.

    As shown in the following example, to deploy code in Node.js 10.16, you can edit code inline, upload a local ZIP file, or upload a ZIP file from OBS.

    Figure 2 Deploying code
  2. You can modify the code and click Deploy to deploy the code again.

Viewing Code Information

  1. View code attributes.
    Code attributes show the code size and the time the code was modified.
    Figure 3 Viewing code attributes
  2. View basic information.
    Configuring Basic Settings shows the default memory and execution timeout in each runtime. You can click Edit to switch to the Basic Settings page and modify Handler, Memory (MB), and Execution Timeout (s) as required. For details, see Figure 4.
    Figure 4 Editing basic information

    Once a function is created, the runtime cannot be changed.

    Table 2 Default basic information of each runtime

    Runtime

    Default Basic Information

    Java

    Memory (MB): 512

    Handler: com.demo.TriggerTests.apigTest

    Execution Timeout (s): 15

    Node.js

    Memory (MB): 128

    Handler: index.handler

    Execution Timeout (s): 3

    Custom

    Memory (MB): 128

    Handler: bootstrap

    Execution Timeout (s): 3

    PHP

    Memory (MB): 128

    Handler: index.handler

    Execution Timeout (s): 3

    Python

    Memory (MB): 128

    Handler: index.handler

    Execution Timeout (s): 3

    Go 1.x

    Memory (MB): 128

    Handler: handler

    Execution Timeout (s): 3