All Products
Search
Document Center

Function Compute:Create a web function

Last Updated:Dec 16, 2024

If you want to write a program based on a popular framework, such as Flask, Express, and Spring Boot, or migrate existing framework applications to Function Compute, select Web Function when you create a function. 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 web functions in the Function Compute console.

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 Web 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 Custom Runtime and then select a programming language or framework. For more information, see the "Custom runtimes" section of the Overview topic.

      • If you want to create an event function, select Built-in Runtimes. For more information, see Create an event function.

      • If you want to create a GPU function, select Custom Image. For more information, see Create a GPU function.

      In this example, Custom Runtime 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 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

      Startup Command

      The startup command of the program. If you do not configure the startup command, you must manually create a startup script named bootstrap in the root directory of the code. You can use the bootstrap script to start your program.

      npm run start

      Listening Port

      The port on which the HTTP server in your code listens.

      9000

    • Advanced Settings: Configure the 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.

      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

      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.

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

      Instance Concurrency

      Function Compute supports the instance concurrency feature, which allows an instance to process multiple requests at a time. This parameter specifies the maximum number of requests that a function instance can process at a time. For more information, see Configure instance concurrency.

      20

      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 through the default NIC of Function Compute.

      Important

      If 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: You cannot use SLS to store or query function execution logs.

      Enable

    • Environment Variables: Configure the environment variables in the runtime of your function. For more information, see Configure environment variables.

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.

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

    image

  2. Export and back up your code, or upload code for deployment.

    image

    image

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 More > Delete 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.

image

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.