All Products
Search
Document Center

Function Compute:Create a web function

Last Updated:Jul 18, 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, you can choose to create a web function. You can use the instance concurrency feature to effectively manage resource usage during traffic peaks and mitigate the impact of cold starts. This helps improve performance and control costs.

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 the following parameters, and then click Create.

    • Basic Settings: Configure Function Name.

    • Code: Configure the runtime and code-related information of the function.

      Parameter

      Description

      Example

      Runtime

      Select a runtime that you prefer, such as a Python, Java, PHP, Node.js, or Custom Container Image.

      Node.js 16

      Code Upload Method

      Specify how you want code to be uploaded 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 Images: Select a sample image that comes with Function Compute.

      • Use Container Registry Images: Select an image from Container Registry. In the Select Container Image panel, specify the Container Image Instance and Container Registry Repository parameters, find the desired image, and click Select in the Actions column.

      Use Sample Code

      Startup Command

      Configure 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. The bootstrap script is used to start your program.

      npm run start

      Listening Port

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

      9000

    • In the Advanced Settings section, configure instance settings and other items such as the function execution timeout period. The following table describes the parameters.

    • Parameter

      Description

      Example

      Specifications

      Configure instance specifications, such as vCPU Capacity and Memory Capacity 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 (in GB) must be set within a range of 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.

      Valid values:

      • 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 the 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 size of the temporary disk changes with the lifecycles of underlying instances. After an instance is recycled by the system, the corresponding data on the disk is cleared. To persist files, you can use Apsara 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

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

      Note

      If the execution of the function times out, the function fails to be executed. If you require a longer timeout limit, join the DingTalk group 11721331 for technical support.

      60

      Instance Concurrency

      Note

      This parameter is required if you select Web Function or Container Images to create a function.

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

      20

      Time Zone

      Specify the time zone of the function. After you configure the time zone of the function, the environment variable TZ is automatically added to the function. The value is the time zone that you configure.

      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 virtual private cloud (VPC) resources. For more information, see Configure network settings.

      Yes

      VPC

      Specify the 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.

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

      vSwitch

      Specify the vSwitch. This parameter is required if you set Access to VPC 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 through 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

      Specify whether to enable the logging feature. Valid values:

      • 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 for the runtime of the function. For more information, see Configure environment variables.

  4. After the function is created, go to the Function Details page. On the Code tab, you can modify, deploy, and test the function.

Note
  • Update the function: After the function is created, you can modify its configurations on the Configurations tab based on your business requirements.

  • Delete the function: You can also delete the function in the function list or on the function details page if you no longer need the function

Configure instance concurrency

Instance concurrency specifies the maximum number of concurrent requests that each function instance can process at the same time. You can use the instance concurrency feature of Function Compute to manage resource usage in an efficient manner during traffic peaks and mitigate the impact of cold starts. This helps improve performance and reduce costs.

Background

Function Compute is billed based on the execution duration of instances. For example, 3 requests need to be processed at the same time. The processing of each request lasts 10 seconds. The following items describe the execution duration for different concurrency values:

  • If instance concurrency is set to 1, each instance can process only 1 request at the same time. Function Compute creates 3 instances to process the 3 requests. The total execution duration is 30 seconds.

  • If instance concurrency is set to 10, each instance can process 10 requests at the same time. Function Compute creates only 1 instance to process the 3 requests. The total execution duration is 10 seconds.

Note

By default, instance concurrency of a function is 1, which indicates that each instance can process only 1 request at the same time. If you set instance concurrency to a value greater than 1, Function Compute creates a new instance only when the number of requests that are concurrently processed by existing instances exceeds the specified value.

The following figure shows the difference in request execution when instance concurrency is set to different values.

image

Common scenarios

If a large amount of time is required for a function to wait for responses from downstream services, we recommend that you use the instance concurrency feature to allow a single instance to concurrently process multiple requests. In most cases, waiting for responses does not consume resources. Concurrent processing of multiple requests by one instance saves costs and improves the responsiveness and throughput of applications.

Benefits

  • Shorter execution duration and lower costs

    For example, for functions that involve a great number of input/output (I/O) operations, you can use a single instance to concurrently process multiple requests. This reduces the number of instances that are used to process requests and reduces the total execution duration of requests.

  • Status shareable among requests

    Multiple requests can share the connection pool of a database in one instance to minimize connections between requests and your database.

  • Lower frequency of cold starts

    One instance can process multiple requests, which reduces the number of new instances and reduces the frequency of cold starts.

  • Fewer IP addresses used in VPCs

    For a fixed number of requests to be processed, the number of required instances is reduced if each instance can process multiple requests. This reduces the number of IP addresses used in VPCs.

    Important

    Make sure that the vSwitch associated with your VPC has at least two available IP addresses. Otherwise, the service may be unavailable and requests fail to be executed.

Limits

Item

Limit

Supported runtimes

  • Custom Runtime

  • Custom Container

Instance concurrency value

1~200

Logs in the X-Fc-Log-Result response header

Not supported if instance concurrency is set to a value greater than 1

Procedure

You can specify the instance concurrency of a function when you create or update the function.

Note

If you select Web Function to create a function, you can configure the instance concurrency when you create the function.

dg-instance-concurrency

Provisioned instances can also concurrently process multiple requests. For more information, see Configure provisioned instances.

Impacts

This section compares the instance concurrency of a value of 1 (instance concurrency = 1) and the instance concurrency of a value greater than 1 (instance concurrency > 1).

Billing

The execution duration is different for different instance concurrency values. As a result, costs are different. For more information, see Billing overview.

Instance concurrency = 1

An instance can process only one request at the same time. The billing duration starts when the first request starts to be processed and ends when the last request is processed.

image

Instance concurrency > 1

Each instance can concurrently process multiple requests. The actual running duration of instances is used to measure the function execution duration. The billing duration starts when the first request starts to be processed and ends when the last request is processed.

image

Concurrency throttling

By default, the maximum number of on-demand instances of Function Compute in a region is 300. The maximum number of requests that can be processed at the same time in a region is 300 × Instance concurrency. For example, if you set instance concurrency to 10, a maximum of 3,000 concurrent requests can be processed at the same time in a region. If the number of concurrent requests exceeds the maximum number of requests that Function Compute can handle, the throttling control error ResourceExhausted is reported.

Note

To increase the upper limit of on-demand instances in a region, contact us.

Logging

  • If instance concurrency is set to 1, Function Compute returns function logs in the X-Fc-Log-Result response header if you specify X-Fc-Log-Type: Tail in the HTTP request header when you invoke a function. If instance concurrency is set to a value greater than 1, the response header does not contain function logs because the logs of a specific request cannot be obtained among concurrent requests.

  • For a Node.js runtime, console.info() was used to return logs, which include request IDs. If instance concurrency is set to a value greater than 1, console.info() cannot print request IDs as expected. All the request IDs are printed as req 2. The following example shows a sample log:

    2019-11-06T14:23:37.587Z req1 [info] logger begin
    2019-11-06T14:23:37.587Z req1 [info] ctxlogger begin
    2019-11-06T14:23:37.587Z req2 [info] logger begin
    2019-11-06T14:23:37.587Z req2 [info] ctxlogger begin
    2019-11-06T14:23:40.587Z req1 [info] ctxlogger end
    2019-11-06T14:23:40.587Z req2 [info] ctxlogger end
    2019-11-06T14:23:37.587Z req2 [info] logger end
    2019-11-06T14:23:37.587Z req2 [info] logger end                    

    In this case, we recommend that you use context.logger.info() to print logs. This method allows request IDs to be printed as expected. The following sample code shows an example:

    exports.handler = (event, context, callback) => {
        console.info('logger begin');
        context.logger.info('ctxlogger begin');
    
        setTimeout(function() {
            context.logger.info('ctxlogger end');
            console.info('logger end');
            callback(null, 'hello world');
        }, 3000);
    };                   

Error handling

When an instance concurrently processes multiple requests, unexpected process quits caused by failed requests affect other concurrent requests. Therefore, you must compile logic to capture request-level exceptions in the function code and prevent the impact on other requests. The following example shows sample Node.js code:

exports.handler = (event, context, callback) => {
    try {
        JSON.parse(event);
    } catch (ex) {
        callback(ex);
    }

    callback(null, 'hello world');
};                    

Shared variables

When an instance concurrently processes multiple requests, errors may occur if multiple requests attempt to modify a variable at the same time. You must use mutual exclusion in your code to protect non-thread-safe variables upon modifications. The following example shows the sample Java code:

public class App implements StreamRequestHandler
{
    private static int counter = 0;

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        synchronized (this) {
            counter = counter + 1;
        }
        outputStream.write(new String("hello world").getBytes());
    }
}                    

Monitoring metrics

After you set instance concurrency to a value greater than 1, you can see that the number of used instances is reduced in the instance monitoring chart.实例数据监控图

Additional information

  • In addition to the Function Compute console, Function Compute also allows you to use API operations to manage functions. For more information, see Function.