Create a web function

更新时间:
复制 MD 格式

Web functions are designed for HTTP-based workloads. Use a web function to run applications built on popular frameworks like Flask, Express, or Spring Boot, or to migrate an existing HTTP service to Function Compute without rewriting your code. Function Compute manages scaling and resource allocation at the function level, and each function runs independently.

Note

To process event-driven workloads (such as OSS triggers or MNS messages), create an event function instead. For GPU-accelerated inference, create a GPU function. See Technical selection guide to compare all four function types.

Prerequisites

Before you begin, ensure that you have:

  • Logged in to the Function Compute console

  • (Optional) A virtual private cloud (VPC) configured, if the function needs to access VPC resources

  • (Optional) A NAS file system or OSS bucket, if the function needs persistent storage

Create a web function

  1. In the left navigation pane, choose Function Management > Function List.

  2. In the top menu bar, select a region. On the Function List page, click Create Function.

  3. In the dialog box, select Web Function and click Create Web Function.

  4. On the Create Web Function page, configure the following settings, then click Create.

Basic configurations

Configuration itemDescriptionExample
Function NameA unique identifier for the function. Must be unique within the same account and region, and comply with naming conventions.myFunction
Specification PlanSets the vCPU, Memory, and Disk for the function. Billed by specification x usage duration. The vCPU-to-memory ratio must be between 1:1 and 1:4. All disk directories are writable and disk space is shared across the instance. Data on disk is lost when the instance is reclaimed — mount a NAS file system or OSS bucket for persistent storage. See Billing overview.vCPU: 0.35, Memory: 512 MB, Disk: 512 MB (free, up to 10 GB quota)
Concurrency Per InstanceNumber of requests a single instance can handle simultaneously. See Configure concurrency per instance.20

Elastic configurations

Configuration itemDescriptionExample
Minimum InstancesPre-allocates instances to reduce cold start latency. Set to 1 or higher for latency-sensitive workloads. If set to 1 or higher and no elastic policy is configured or currently valid, the current number of minimum instances equals the value you set here. If multiple elastic policies are active, the system uses the highest calculated value as the effective minimum. See Instance scaling rules.Enabled

Function code

Configuration itemDescriptionExample
RuntimeSelect Custom Runtime and choose a language or framework. For event functions, select Built-in Runtime. For GPU functions, select Custom Image. See Code development overview.Custom Runtime > Node.js > Node.js 16
Code Upload MethodHow to provide function code: Use Sample Code (default), Upload A ZIP Package, Upload A Folder, or Upload From OSS (specify bucket and file name).Use Sample Code
Startup CommandThe command that starts your HTTP server. If left blank, create a script named bootstrap in the root directory of your code — Function Compute uses it to start the program.npm run start
Listening PortThe port your HTTP server listens on.9000
Execution TimeoutHow long the function can run before being terminated. Default: 60 seconds. Maximum: 86,400 seconds.60

Permissions, network, and storage

Configuration itemDescriptionExample
Function RoleA Resource Access Management (RAM) role that Function Compute uses to generate temporary credentials for accessing Alibaba Cloud resources. See Use a function role.mytestrole
Allow Access To VPCEnables the function to access resources inside a VPC. Requires VPC, vSwitch, and Security Group. See Configure network settings.Enabled
Allow Default NIC To Access Public NetworkAllows outbound internet access through the default network interface card (NIC).
Important

Disable this if you have configured a static public IP address — otherwise the static IP will not take effect. See Configure a static public IP address.

Enabled
Mount NAS File SystemMounts a NAS file system for persistent shared storage (for example, model files shared across inference functions). With automatic configuration, the system uses or creates a General-purpose NAS file system named Alibaba-Fc-V3-Component-Generated. See Configure a NAS file system.Enabled
Mount OSS Object StorageMounts an OSS bucket for persistent storage of logs and business files. See Configure OSS.Enabled

Logs and tracing

Configuration itemDescriptionExample
Log FeatureSaves function execution logs to Simple Log Service for debugging and analytics. Automatic Configuration selects (or creates) a log project that starts with serverless-<region_id> — one per region. Custom Configuration lets you specify the Log Project and Logstore manually. See Configure the logging feature.Enabled

More configurations

Configuration itemDescriptionExample
Time ZoneSets the time zone for the function. Automatically adds the TZ environment variable with the selected value.UTC
TagsAdds tags to group and manage functions. Requires both a tag key and a tag value.key: value
Resource GroupAssigns the function to a resource group for organization. See Resource groups.Default Resource Group
Environment VariablesPasses configuration values to the function without changing code. See Configure environment variables.{"BUCKET_NAME": "MY_BUCKET"}

Test the function

After creating the function, verify it works before further configuration.

On the Function Details page, click Test Function. The console displays the response and function logs in the output panel.

If the Code tab shows a change indicator (1 in the figure), click Deploy Code first to apply your latest edits, then click Test Function.

image

Edit a function

On the Function Details page, edit code directly on the Code tab. To export your code as a backup or upload new code for deployment, use the import/export options shown below.

imageimage

For other configuration changes, see Configure a function.

Delete a function

On the Function List page, find the function and click Delete in the Actions column. Before confirming, make sure no triggers or elastic policies for minimum instances are still attached to the function.

Get the function ARN

An Alibaba Cloud Resource Name (ARN) uniquely identifies a Function Compute function and can be referenced in your code or other services.

  1. In the left navigation pane, choose Function Management > Function List.

  2. Select a region, then click the function name.

  3. On the Function Details page, click Copy ARN on the right.

image

What's next