This topic explains the core concepts involved in each phase of the function creation process, helping you understand the basics of Function Compute. For more information about the steps to create a function, see Use event functions to process events generated by Alibaba Cloud services.
Function Compute function
A function is the basic unit by which Function Compute schedules and runs resources. It consists of function code and configurations. The following figure illustrates the key concepts related to a Function Compute function.
Function creation
During the function creation phase, you can select a function type and an appropriate runtime that best meet your business requirements, and upload the function code to Function Compute. Other parameters can be configured later in the function configuration phase as needed.
Function type selection
Event function
If you want to use a specific event to trigger your function, such as an Object Storage Service (OSS), ApsaraMQ for Kafka, or Simple Log Service event, you can create an event function. In this case, you must write programs to handle those events based on the interfaces defined by Function Compute. For more information, see Create an event function.
Web function
If you want to write programs based on popular frameworks, such as Flask, ThinkPHP, Express, and Spring Boot, or migrate existing framework applications to Function Compute, you can create web functions. For more information, see Create a web function.
Task function
Task functions are particularly suitable for processing asynchronous requests and dealing with offline scenarios that involve scheduled tasks, audio and video processing, or data processing. After a task function is created, task mode is enabled by default, allowing you to track the status of each task and manually start and stop the task. For more information, see Create a task function.
GPU function
When you need to deploy AI projects that require GPU-accelerated instances for faster computation, we recommend that you set up GPU functions by using container images. Examples of such AI projects include Stable Diffusion WebUI, ComfyUI, retrieval-augmented generation (RAG), and TensorRT projects. For more information, see Create a GPU function.
Execution environment
Built-in runtime
Built-in runtimes are predefined execution environments in Function Compute. In these runtimes, you must write the handler function according to the interfaces defined by Function Compute. Built-in runtimes only allow a function instance to process one request at a time, but they offer the shortest cold start durations. These runtimes are ideal for scenarios where you want to use specific events, such as OSS, ApsaraMQ for Kafka, and Simple Log Service events, to trigger your functions. For more information, see Function Compute runtimes.
Event (
event
)Event data is passed to your function as a JSON document: The runtime converts the event into an object and passes the object to the input parameter
event
of the function. If an event is generated by another Alibaba Cloud service, then the event format will depend on the service that generates the event. For more information, see Formats of event for different triggers.Context (
context
)When Function Compute runs a function, it passes the context object to the input parameter
context
of the function. This object contains information about the invocation, service, function, and execution environment. For more information, see Context.
Custom runtime
Custom runtimes support mainstream web frameworks such as Flask, ThinkPHP, Express, and Spring Boot. You only need to write programs based on the interfaces defined by these frameworks to create web applications. In a custom runtime, your code file in the ZIP format is an HTTP server program. To start the HTTP server, you only need to configure the Startup Command and Startup Parameter parameters for your function. For more information, see Principles.
Custom Container runtime
Function Compute provides Custom Container runtimes that allow you to deploy your functions as container images. You can either use an image uploaded to Container Registry or a sample image. For more information, see Create a Custom Container function.
Note that GPU functions can only be deployed with a Custom Container runtime.
Version
Publishing a version creates a snapshot that includes the function code and configurations, but excludes resource attributes such as triggers, asynchronous task configurations, and provisioned instance policies. A version is similar to a
commit
inGit
. A Gitcommit
captures all changes made to one or more code files or settings, representing a snapshot of your repository's state at a specific point in time. For more information, see Manage versions.Alias
An alias is a pointer to a specific version of your function. When you access a function using an alias, Function Compute parses the alias to the function version it points to. As a user, you do not need to know which version the alias points to. You can use an alias to publish a version, roll back to a previous version, or implement a canary release. An alias is similar to a
tag
inGit
. You can addtags
tocommits
to markrelease
points. For more information, see Manage aliases.CPU instance
As the standard instance type for Function Compute, CPU instances are well-suited for scenarios involving burst traffic and requiring significant computing power. For more information, see Instance types and usage modes.
GPU-accelerated instance
Based on the Turing architecture, GPU-accelerated instances use GPU hardware to process workloads more efficiently. These instances are particularly suitable for scenarios involving audio and video processing, AI workloads, and image processing. For more information, see Instance types and usage modes.
Function configuration
After creating a function, you may need to adjust the following configuration items based on your business requirements.
Basic configurations
Specifications
Function Compute offers a variety of instance specifications for functions. You can choose the specifications that best align with your business needs. For more information, see Instance types and usage modes.
Size of temporary disk
Function Compute provides temporary storage for functions in the root directory. Note that data in this temporary storage is automatically cleared when an instance is recycled. To persist your data, we recommend that you use File Storage NAS (NAS) or OSS. For more information, see Configure a NAS file system and Configure an OSS file system. These file systems ensure that your data is retained and can be easily accessed and managed, even in the event of a system restart or failure.
NoteFor a GPU function, you can set the temporary disk size to 512 MB, 10 GB, 30 GB, or 60 GB. For other types of functions, the temporary disk size can be either 512 MB or 10 GB. The 512 MB option is available at no cost.
Trigger
Certain Alibaba Cloud services can directly invoke Function Compute functions using triggers. These services push events to Function Compute, and when the specified event occurs, the function is invoked immediately. For more information, see Trigger overview.
A function can have more than one trigger. Each trigger acts as a client that invokes your function independently, and each event that Function Compute passes to your function contains data from only one trigger. Function Compute converts the event document into an object and passes the object to the handler of your function.
Runtime
Function Compute supports multiple programming languages through the use of runtimes. A runtime is the language-specific environment your function runs in. It relays event information, context information, and responses between Function Compute and your function. For more information, see Overview.
Environment variable
Environment variables are key-value pairs stored in function configurations. Every function operates with its own set of environment variables. You can use environment variables to modify the behavior of your function without updating your code. For more information, see Configure environment variables.
Layer
Layers are
.zip
files that contain additional code or data. They usually contain library dependencies, a custom runtime, supplementary data, or configuration files. You can use both the official public layers provided by Function Compute and the custom layers that you create. For more information, see Examples of using official public layers and Create a custom layer. Using layers offers several advantages:Reducing the size of your deployment packages
Put all of your function dependencies in a layer to keep your deployment package small, which speeds up code deployment.
Separating core function logic from dependencies
With layers, you can update function dependencies without modifying your function code and, conversely, update your function code without altering the dependencies. This helps you focus on your function logic by separating concerns.
Sharing dependencies across multiple functions
You can put public functions or dependencies in a layer and apply the layer to any number of functions in your account. Without layers, you have to include the same dependencies in each individual deployment package.
NoteYou cannot use layers with functions that run in a Custom Container runtime. For these functions, bundle them along with their required runtime and all code dependencies in a container image. For more information, see Custom Container runtimes and Create a Custom Container function.
Permissions
To give your function permissions to access other Alibaba Cloud services, you specify the Resource Access Management (RAM) role for the function. When the function is invoked, Function Compute assumes this role and generates a temporary AccessKey pair that is used to access your Alibaba Cloud resources before passing it to your function code. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
Logging
Simple Log Service (SLS) can be integrated with Function Compute. After you configure the logging feature for your function, Function Compute automatically collects function logs and sends them to a specified Logstore in Simple Log Service. For more information, see Configure the logging feature.
Network
By default, the functions that you create in Function Compute can access the Internet but cannot access resources within a virtual private cloud (VPC). If you want your function to access resources in a VPC or allow requests from a VPC to invoke your function, you must manually configure the network settings and permissions for that function. For more information, see Configure network settings.
Storage
You can configure a File Storage NAS (NAS) or an OSS file system to store and share your function data. For more information, see Configure a NAS file system and Configure an OSS file system.
Provisioned instance
You can configure provisioned instances in the Function Compute console. Once configured, the provisioned instances remain active and continue to generate charges, even if no requests are being processed, until you choose to release them. For more information, see Configure provisioned instances.
Asynchronous mode configurations
Function Compute can execute asynchronous requests in task mode. In task mode, Function Compute records the execution status of each task in each stage, and provides features such as task status query, task queue status query, task deduplication, and active task termination. Asynchronous tasks are ideal for long-running operations. On the other hand, if your asynchronous requests are latency-sensitive—requiring a response time of less than 100 milliseconds—or if you need a high task submission rate, potentially reaching 1,000 tasks per second or more, then asynchronous tasks may not be the best option. For more information, see Asynchronous invocations.
Instance lifecycle
Function instances are dynamically created and destroyed based on incoming requests. The lifecycle of each instance comprises three phases: Creating, Invoke, and Destroy. For more information, see Configure instance lifecycles.
Health check
You can use the health check feature of Function Compute to periodically assess the health of web and GPU function instances. This helps prevent failed invocations caused by abnormal instances. For more information, see Configure a health check policy for function instances.
DNS
You can configure a custom Domain Name System (DNS) to implement specific features, such as access acceleration. Custom DNS is supported only in the built-in and custom runtimes of Function Compute. For more information, see Configure custom DNS.
Custom domain name
You can bind a custom domain name to your application or function. This allows users to access your application or function through a fixed domain name. You can also configure the custom domain name as the origin domain and add a CDN-accelerated domain name to it. This allows users to access resources more quickly, thereby enhancing their service experience. For more information, see Configure custom domain names.
Function invocation
After you deploy a Function Compute function, you can invoke it in several ways. For example, you can use the Function Compute console to quickly create a test event to call your function. Alternatively, you can invoke your function by using one of the SDKs, calling an API operation, accessing the dedicated HTTP(S) endpoint for your function, or through a trigger. The following are some key concepts related to function invocation.
Synchronous invocation
When you invoke a function synchronously, you must wait for it to finish processing the event completely and return a response before continuing. For more information, see Synchronous invocations.
Asynchronous invocation
When you invoke a function asynchronously, Function Compute immediately returns a success response and queues the event for reliable processing. After the function processes the event, Function Compute does not provide the invocation details or the execution status. If you want to obtain that information, you must configure asynchronous invocation destinations for your function. For more information, see Overview.
Invocation analysis
The invocation analysis feature helps you track function invocations and executions. After you enable this feature, the system collects metrics about each invocation of a function. For more information, see Request-level metric logs.
Instance concurrency
Instance concurrency specifies the maximum number of requests that a function instance can process at a time. For more information, see Configure instance concurrency.
Idle mode
You can enable idle mode for provisioned instances. In this mode, the GPU or vCPU resources allocated to the instances are frozen when no requests are being processed, putting the instances into an idle state. Idle resource usage is billed at a much lower rate than active usage, significantly reducing the cost of provisioned instances.
On-demand mode
In on-demand mode, Function Compute automatically allocates and releases function instances based on incoming requests. For more information, see Instance types and usage modes.
Provisioned mode
In provisioned mode, you allocate and release instances for your function. Function Compute preferentially forwards incoming requests to provisioned instances. If the number of provisioned instances is not sufficient to handle all incoming requests, the excess requests are handled by the dynamically allocated on-demand instances. For more information, see Instance types and usage modes.
Provisioned instances help mitigate cold starts, as they are always prepared to respond immediately to incoming requests.
If you create a fixed number of provisioned instances, they may not be fully utilized. To improve resource utilization, you can enable scheduled or metric-based auto scaling for those provisioned instances.
Scheduled auto scaling
Scheduled auto scaling allows you to set up actions that adjust the number of provisioned instances to planned levels at specific times. For more information, see Configure provisioned instances.
Metric-based auto scaling
Metric-based auto scaling dynamically adjusts the number of provisioned instances in response to the values of specified tracking metrics. For more information, see Configure provisioned instances.
Maximum number of total instances
There is a maximum number of instances that you can create for your function. The default limit is 300 instances per region per Alibaba Cloud account. If you need to adjust this limit, join our DingTalk group (group ID:
64970014484
) to submit an application.
Other related concepts
Tag
Tags are used to classify service resources, facilitating resource search and aggregation. You can also use tags to group services and assign varying permissions on those service groups to different roles. For more information, see Manage tags.