Function Compute provides 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 main difference between a task function and an event function is that task mode is enabled by default for task functions. In task mode, you can submit, view, stop, and retry asynchronous tasks.
Create a function
Log on to the Function Compute console. In the navigation pane on the left, select .
In the top navigation bar, select a region. On the Functions page, click Create Function.
In the dialog box that appears, select the Task Function type as prompted, and then click Create Task Function.
On the Create Task Function page, configure the following configuration items, and then click Create.
Basic Settings: Configure the Function Name parameter.
Code: Configure the runtime and code-related information of the function. The following table describes the parameters.
Parameter
Description
Example
Runtime
We recommend that you select Built-in Runtimes and then select a programming language of the required version, such as Python, Java, PHP, or Node.js. For more information, see the "Function Compute runtimes" section of the Overview topic.
If you want to create a web function, select Custom Runtime. For more information, see Create a web function.
If you want to create a GPU function, select Custom Image. For more information, see Create a GPU function.
In this example, Built-in Runtimes is selected.
Node.js 16
Code Upload Method
The method that you use to upload code to Function Compute. Valid values:
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 package that contains your function code.
Upload Folder: Select and upload the folder that contains your function code.
OSS: Upload code from an OSS bucket. In this case, you must configure 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 the billing of resources, see Billing overview.
NoteThe 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
The size of the disk that you want to use to temporarily store files.
Valid values:
512 MB (default): You are not charged for using a temporary disk of this size. Function Compute provides you with a free disk of 512 MB.
10 GB: You are charged based on a disk size of 9.5 GB.
NoteData 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 disk is cleared. To persist data, you can use File Storage NAS (NAS) or OSS. For more information, see Configure a NAS file system and Configure an OSS file system.
512 MB
Execution Timeout Period
The timeout period of a function execution. Default value: 60. Maximum value: 86400. Unit: seconds.
60
Handler
The handler of the function. The Function Compute runtime loads and invokes the handler to process requests.
NoteIf 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
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
The Resource Access Management (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
Specifies whether to allow the function to access virtual private cloud (VPC) resources. For more information, see Configure network settings.
Yes
VPC
This parameter is required if you set the Access to VPC parameter to Yes. You can create a VPC or select an existing VPC that you want to access from the drop-down list.
fc.auto.create.vpc.1632317****
vSwitch
This parameter is required if you set the Access to VPC parameter to Yes. You can create a vSwitch or select an existing vSwitch from the drop-down list.
fc.auto.create.vswitch.vpc-bp1p8248****
Security Group
This parameter is required if you set the Access to VPC parameter to Yes. You can 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
Specifies 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 by using the default NIC of Function Compute.
ImportantIf you use a static public IP address, you must set the Allow Default NIC to Access Internet parameter 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. Valid values:
Enable: Function execution logs are persistently stored in Simple Log Service (SLS) for code debugging, fault analysis, and data analysis.
Disable: Function execution logs are not stored in SLS, and cannot be queried.
Enable
Environment Variables: Configure the environment variables in the runtime of your function. For more information, see Configure environment variables.
On the Function Details page, select the Code tab and click Test Function.
After a successful execution, view the Response. The response in this example is hello world.
After the function is created, task mode is enabled by default on the Task tab of the function details page.
Modify a function
This section describes how to modify the function code and how to import and export the function code. For more information about how to configure a function, see Function configuration.
On the Function Details page, 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.
Export and back up your code, or upload code for deployment.
Delete a function
Log on to the Function Compute console. In the left-side navigation pane, click Functions. In the top navigation bar, select a region. On the Functions page, find the function that you want to delete and choose in the Actions column. In the dialog box that appears, confirm that the function that you want to delete is not bound to any resources such as triggers and reserved instances. Then, click Delete.
References
After a task function is created, task mode is enabled by default. You can use task mode to manage asynchronous invocation tasks. For more information, see Task management.
To enable asynchronous task mode for an existing function, see Enable the asynchronous task mode for an existing function.
In addition to the console, you can start asynchronous tasks in Function Compute by calling an API. For more information, see InvokeFunction.
If a function execution times out, see FAQ about function management.
Functions that are infrequently invoked may take longer to execute. For more information, see Why do infrequently used functions take longer to invoke?. To eliminate the impact of cold start latency for elastic instances, set the minimum number of instances to 1 or more.
To obtain the Alibaba Cloud Resource Name (ARN) of a function to locate Alibaba Cloud resources in your code, see Obtain the ARN of a function.