All Products
Search
Document Center

Function Compute:Create an event-triggered function

Last Updated:Mar 15, 2026

Create an event-triggered function to run code in response to specific events. These events can be initiated by message-based triggers, such as Function Compute triggers, Object Storage Service (OSS) triggers, or RocketMQ triggers. You must write your function code in accordance with the interfaces defined by Function Compute. Function Compute schedules and runs resources at the function level. Each function is independent and does not affect other functions. This topic describes how to create and manage event-triggered functions in the console.

Create a function

  1. Log on to the Function Compute console. In the navigation pane on the left, choose Function Management > Function.

  2. In the top menu bar, select a region. On the Function page, click Create Function.

  3. In the dialog box that appears, select Event Function as the function type based on your business scenario, and then click Create {title}.

  4. On the Create {title} page, configure the following parameters and click Create.

    • Basic Configurations: Set the function specifications.

      Configuration Item

      Description

      Example

      Function Name

      A unique identifier for the function. The function name must be unique within the same account and region and must follow the naming conventions.

      myFunction

      Specifications

      Set the vCPU, Memory, and Disk specifications for the function as needed. After you set the specifications, the usage of each resource generated by actual function invocations is measured by multiplying the specification by the duration of use. For more information, see Billing overview.

      Note
      • The ratio of vCPU (in cores) to memory (in GB) must be between 1:1 and 1:4.

      • All directories on the disk are writable. The disk space is shared.

      • The disk size is tied to the instance lifetime of the underlying function. When the instance is reclaimed by the system, the data on the disk is also lost. To persist files, you can mount a NAS file system or an OSS bucket. For more information, see Configure a NAS file system and Configure Object Storage Service (OSS).

      • vCPU: 0.35 vCPU

      • Memory: 512 MB

      • Disk: 512 MB (This is not billed. Function Compute provides a free tier of 10 GB disk space.)

    • Scaling Policy: Select an elastic mode.

      configuration item

      Description

      Example

      Minimum Instances

      If your business is sensitive to latency, set **Minimum instances** to 1 or greater to reserve resources in advance and reduce cold start latency.

      Note
      • After you set **Minimum instances** to 1 or greater, if no elastic policy for the minimum number of instances is configured or if no valid elastic policy is in effect for a period, the current minimum number of instances is the value you set here.

      • If you configure multiple elastic policies, the system calculates the Minimum Instances for each policy when it is triggered. The system then uses the maximum value among the valid elastic policies at the current time as the current Minimum Instances.

        For more information, see How is the current minimum number of instances calculated?.

      Enabled

    • Code: Configure the function's runtime environment and code-related information.

      Configuration Item

      Description

      Example

      Sample code:

      Select Built-in Runtimes and choose a familiar language and version, such as Python, Java, PHP, or Node.js. For more information, see Function Compute runtimes.

      This topic provides an example of selecting the Built-in Runtimes.

      Built-in Runtimes

      Code Upload Method

      Select the method to upload your code to Function Compute.

      • Use Sample Code: If you want to create the function first and complete the code later, you can use the Hello World sample code provided by the platform. You can then write and debug the code online on the Code tab of the function details page.

      • Upload ZIP: Select and upload the ZIP package that contains the function code.

      • Upload ZIP: Select and upload the JAR package that contains the function code.

        This applies only to the Java runtime.

      • Upload Folder: Select and upload the folder that contains the function code.

      • OSS: Select the Bucket Name and Object Name of the function code to upload.

      Use sample code

      Handler

      Set the handler. The Function Compute runtime loads and calls your handler to process requests.

      If you set Code Upload Method to Use Sample Code, you do not need to modify the Handler. If you select another code upload method, you must modify the Handler as needed. Otherwise, an error occurs when the function is executed.

      index.handler

      Execution Timeout Period

      Set the timeout period. The default Execution Timeout Period is 60 seconds. The maximum value is 86,400 seconds.

      60

    • Permissions, network, and storage: Configure the function's access role, network, and storage mounts.

      Configuration Item

      Description

      Example

      Function Role

      The Function Compute platform uses this RAM role to generate temporary keys for accessing Alibaba Cloud resources and passes them to your code. For more information, see Use a function role to grant Function Compute permissions to access other Alibaba Cloud services.

      mytestrole

      Access to VPC

      Enable this feature to allow the function to access resources in a VPC. For more information, see Configure a network.

      Enabled

      VPC

      Required when Access to VPC is enabled. Create a new VPC or select the ID of the VPC that you want to access from the drop-down list.

      fc.auto.create.vpc.1632317****

      vSwitch

      Required when Access to VPC is enabled. Create a new vSwitch or select a vSwitch ID from the drop-down list.

      fc.auto.create.vswitch.vpc-bp1p8248****

      Security Group

      Required when Access to VPC is enabled. Create a new security group or select a security group from the drop-down list.

      fc.auto.create.SecurityGroup.vsw-bp15ftbbbbd****

      Allow Default NIC to Access Internet

      Specifies whether to allow the function to access the Internet through the default network interface controller (NIC).

      Important

      When you use the static public IP address feature, you must disable Allow function's default NIC to access the Internet. Otherwise, the configured static public IP address does not take effect. For more information, see Configure a static public IP address.

      Enabled

      Mount a NAS File System

      Configure a NAS file system for the function to persistently store data shared among functions, such as models shared by multiple inference functions.

      If you select automatic configuration, the system uses an existing General-purpose NAS file system named Alibaba-Fc-V3-Component-Generated by default. If no eligible NAS file system is found under the current account, the system automatically creates one.

      Enabled

      Mount OSS

      Mount an OSS object for the function to persistently store logs, business files, and more. For more information, see Configure Object Storage Service (OSS).

      Enabled

    • Logs and tracing

      Parameter

      Description

      Example

      Logging

      Persistently save the execution logs of the function to Simple Log Service. This helps you debug code, analyze faults, and perform data analytics. For more information, see Configure the logging feature.

      • Automatic Configuration: Automatically selects a log project that starts with serverless-<region_id>.

        Only one such log project is created in each region. If the system finds that this log project already exists in the current region, it uses the existing one.

      • Custom Configuration: Manually specify the destination Log Service Project and Logstore.

      Enabled

    • More Configurations

      Configuration

      Description

      Example

      Time zone

      Select the time zone for the function. After you set the time zone, an environment variable TZ is automatically added to the function. Its value is the target time zone you set.

      UTC

      Tag

      Set tags for the function to manage functions by group. You must set both a tag key and a tag value.

      key : value

      Resource Group

      Select the resource group to which the function belongs. Use resource groups to manage functions by group.

      Default Resource Group

      Environment Variables

      Use environment variables to flexibly adjust the function's behavior without modifying the code. For more information, see Configure environment variables.

      {
          "BUCKET_NAME": "MY_BUCKET",
          "TABLE_NAME": "MY_TABLE"
      }

Edit a function

To edit, import, or export function code, follow these steps. For information about how to modify other configuration items, see Function instance specifications and elastic configurations.

  1. On the Function Details page, you can modify the function code on the Code tab. If the branch on the left indicates that changes have been made (as shown by ① in the figure), you must first click Deploy to apply the changes before you click Test Function.

    image

  2. You can also export your code as a backup or upload new code for deployment.

    image

    image

Delete a function

Log on to the Function Compute console. On the Function page, locate the function that you want to delete and click Delete in the Actions column. In the dialog box that appears, confirm that the function has no attached resources, such as triggers or elastic policies for minimum instances, and then confirm the deletion.

Get the ARN of a function

An Aliyun Resource Name (ARN) is used to locate an Alibaba Cloud resource in your code. You can obtain the ARN of a function to reference it.

  1. Log on to the Function Compute console. In the navigation pane on the left, choose Function Management > Function.

  2. In the top menu bar, select a region. On the Function page, click the target function.

  3. On the Function Details page, click Copy ARN on the right to copy the ARN of the target function.

    image

References