If you want to use a specific event to trigger a function, such as an Object Storage Service (OSS) or ApsaraMQ for RocketMQ trigger, you can create an event function. You must write functions and programs based on the interfaces defined by Function Compute. Resources of Function Compute are scheduled and run based on functions. Functions are independent of each other. This topic describes how to create and manage event functions in the Function Compute console.
Create 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, click Create Function.
On the Create Function page, select Event Function, configure parameters in the following sections, 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 tab, click the Code tab and click Test Function.
After the function is executed, the execution result hello world is returned.
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.
Obtain the ARN of a function
You can use an Alibaba Cloud Resource Name (ARN) to locate the corresponding Alibaba Cloud resource in code. You can obtain the ARN of a function for easy reference.
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, click the function that you want to manage.
On the Function Details tab, click the Configurations tab. On the Basic Configurations tab, view and copy the ARN of the function.
References
Function Compute provides four types of functions for different scenarios: event function, web function, task function, and GPU function. For more information about how to select a function type in different scenarios, see Selection of methods to create functions.
In addition to the Function Compute console, Function Compute also allows you to call API operations to manage functions. For more information, see Function.
If the function timeout period elapses, see What do I do if the function execution times out and the error "Function time out after" is reported?
If it takes a long time to invoke a function that is not frequently used, see Why do functions that are infrequently used take a longer period of time to invoke? To mitigate the impact of cold starts, you can use provisioned instances. For more information, see How to keep instances alive to eliminate the impact of cold starts?