All Products
Search
Document Center

Function Compute:Create a task function to process asynchronous requests in task mode

Last Updated:Nov 14, 2024

Function Compute provides you with a fully managed, ready-to-use, and observable platform for large-scale task processing. You can create task functions in the Function Compute console. The only difference between a task function and an event function is that the task mode is enabled by default for a task function. You can submit, view, stop, and retry asynchronous requests by using the task mode.

Create a function

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

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

  3. On the Create Function page, select Task Function, configure the following parameters, and then click Create.

    • Basic Settings: Configure Function Name.

    • Code: Configure the runtime and code-related information of the function. The following table describes the parameters.

      Parameter

      Description

      Example

      Runtime

      Select a programming language, such as Python, Java, PHP, or Node.js.

      Node.js 16

      Code Upload Method

      Specify how to upload code to Function Compute.

      • Use Sample Code: You can select the sample code provided by Function Compute to create a function based on your business requirements. This is the default method.

      • Upload ZIP: Select and upload a .zip file that contains your function code.

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

      • OSS: Upload code from an Object Storage Service (OSS) bucket. In this case, you must specify the Bucket Name and Object Name parameters.

      Use Sample Code

    • Advanced Settings: Configure instance information and the function execution timeout period.

      Parameter

      Description

      Example

      Specifications

      Configure the vCPU Capacity and Memory Capacity parameters based on your business requirements. For more information about billing of resources, see Billing overview.

      Note

      The ratio of vCPU capacity to memory capacity (GB) must be set from 1:1 to 1:4.

      0.35 vCPUs, 512 MB

      Size of Temporary Disk

      Specify the size of the disk used to temporarily store files based on your business requirements.

      Options:

      • 512 MB: the default value. You are not charged for using a temporary disk of this size. Function Compute provides you with a free disk space of 512 MB.

      • 10 GB: You are charged based on a disk size of 9.5 GB.

      Note

      Data shares the space of the temporary disk and can be written to all directories in the disk.

      The lifecycle of the temporary disk is consistent with the lifecycle of the underlying instance. After the instance is recycled by the system, the data on the hard disk is cleared. To persist files, you can use File Storage NAS (NAS) or Object Storage Service (OSS). For more information, see Configure a NAS file system and Configure an OSS file system.

      512 MB

      Execution Timeout Period

      Specify the timeout period of a function execution. The default timeout period is 60 seconds, and the maximum value is 86,400 seconds.

      60

      Handler

      Specify the handler of the function. The Function Compute runtime loads and invokes the handler to process requests.

      Note

      If you set the Code Upload Method parameter to Use Sample Code, retain the value of the Handler parameter. If you select another code upload method, modify the value of the Handler parameter based on your business requirements. Otherwise, an error is reported when the function runs.

      index.handler

      Time Zone

      Select the time zone of your function. After you specify the time zone of the function, the environment variable TZ is automatically added to the function. The value is the time zone that you specify.

      UTC

      Function Role

      Specify the RAM role of the function. Function Compute uses this role to generate a temporary AccessKey pair for accessing your Alibaba Cloud resources and passes the AccessKey pair to your code. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.

      mytestrole

      Access to VPC

      Specify whether to allow the function to access VPC resources. For more information, see Configure network settings.

      Yes

      VPC

      This parameter is required if you set Access to VPC to Yes. Create a VPC or select the ID of an existing VPC that you want to access from the drop-down list.

      Important

      You can create a maximum of 10 vSwitches for the selected VPC.

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

      vSwitch

      This parameter is required if you set the Access to VPC parameter to Yes. Create a vSwitch or select the ID of an existing vSwitch from the drop-down list.

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

      Security Group

      Specify the security group. This parameter is required if you set Access to VPC to Yes. Create a security group or select an existing security group from the drop-down list.

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

      Allow Default NIC to Access Internet

      Specify whether to allow the function to access the Internet by using the default network interface controller (NIC). If you select No, the function cannot access the Internet through the default NIC of Function Compute.

      Important

      If you use a static public IP address, you must set Allow Default NIC to Access Internet to No. Otherwise, the configured static public IP address does not take effect. For more information, see Configure static public IP addresses.

      Yes

      Logging

      Specifies whether to enable the logging feature. Value description:

      • Enable: Function execution logs are persistently stored in Simple Log Service for code debugging, fault analysis, and data analysis.

      • Disable: You cannot use Simple Log Service to store or query function execution logs.

      Enable

    • In the Environment Variables section, configure environment variables of the runtime of the function. For more information, see Configure environment variables.

  4. On the Function Details page, click the Code tab and click Test Function.

    image

    After the execution, a response is returned in Response. In this example, hello world is returned.

    image

After the function is created, the task mode is enabled by default in the Asynchronous Mode Configurations tab of the Configurations page.

image

Modify a function

  1. On the Function Details page, you can modify the code of the function on the Code tab. If the left-side navigation tree shows that changes are made to the code, you must click Deploy to make the modified code take effect before you click Test Function.

    image

  2. You can also export and back up your code, or upload code for deployment.

    image

    image

Delete a function

  • Log on to the Function Compute console, click Functions, select a region, find the function that you want to delete, and then choose More > Delete in the Actions column to delete the function.

    image

  • On the Function Details page, click Delete Function to delete the function.

    image

More information