If the built-in runtimes in Function Compute do not meet your business requirements, you can use custom image functions. This topic describes how to create a custom image function in the Function Compute console or using the Serverless Devs tool.
Usage notes
To create a custom image function in Function Compute, you must use an image from an Alibaba Cloud Container Registry (ACR) repository that is in the same region and belongs to the same Alibaba Cloud account. For computers that use an ARM architecture, such as Mac computers with Apple silicon, you must specify the build platform of the image as Linux/Amd64. For example, run the command
docker build --platform linux/amd64 -t $IMAGE_NAME ..When Function Compute parses the domain name of an image from an ACR Enterprise Edition instance, it uses the access IP address that is resolved by the default DNS server of the VPC configured for the image repository instance or automatically resolved by PrivateZone. The following scenarios apply:
Scenario 1: If the Default Resolution identifier is not present in the Access IP list on the Access Control page of an ACR Enterprise instance, all IP addresses in the list are automatically resolved by Cloud DNS PrivateZone. In this case, any VPC configuration can be used.

Scenario 2: If an IP address in the Access IP list on the Access Control page of an ACR Enterprise instance is marked with the Default Resolution identifier, that IP address is the default resolved IP address for a specific virtual private cloud (VPC). For example, as shown in the following figure, you can select only the first VPC.

When you use an ACR Enterprise Edition instance, you can select only non-accelerated images. Each time you update the image configuration of a function, the latest accelerated image is generated based on the newly selected original image. If an accelerated image already exists, it is overwritten. Do not delete the original image or the accelerated image. Otherwise, function invocations fail.
If the image configured for your function changes, you must update your function promptly. Otherwise, function invocations fail.
Make sure that the original image exists. Otherwise, the function enters a Failed state and cannot be invoked. Function Compute caches your function to accelerate cold starts, but function invocations still depend on the existence of the original image.
Make sure that the image used in any of your functions is not overwritten. If its digest is overwritten, you must redeploy your function using the latest image information. Function Compute records both the tag and digest of the image version that you select during function creation and updates. If the image version is updated to a different digest elsewhere, function invocations fail.
Prerequisites
Container Registry
- Note
ACR Personal Edition is intended for individual developers and is available as a free trial with a limited quota during the public preview. No Service-Level Agreement (SLA) is provided, and usage limits apply. For more information about the limits, see Notes on creating a Personal Edition instance.
Serverless Devs (required only if you use Serverless Devs to create a function)
Create a function in the console
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.
In the dialog box that appears, select the GPU Function type as prompted based on your scenario, and then click Create GPU Function.
On the Create GPU Function page, set the following parameters and then click Create.
Basic Settings: Configure Function Name.
Elastic Configuration: Select an instance type. You cannot use elastic instances and reserved instances at the same time. After a function is created, you cannot switch the instance type.
Elastic Instances
Parameter
Description
Example
Instance Type
Select Elastic Instance. Instances are automatically scaled based on the number of requests and released when there are no requests. You are charged based on usage. You are not charged if you do not use the instances.
Elastic Instance
GPU Type
Select a GPU type. For more information about the specifications supported by different GPU types, see Instance types and specifications.
Ada Series
Specifications
Set the GPU Memory, vCPU, Memory, and Disk specifications for the function as needed. After you set the specifications, the usage of each resource is calculated by multiplying the specification by the duration of use. For more information, see Billing overview.
NoteAll directories on the disk are writable and share the disk space.
The disk lifetime is the same as the instance lifetime of the underlying function. When the instance is released by the system, the data on the disk is also lost. To store files persistently, you can mount a NAS file system or an OSS bucket. For more information, see Configure a NAS file system and Configure OSS access.
GPU Memory: 48 GB
vCPU: 8 vCPU
Memory: 64 GB
Disk: 512 MB (free of charge, Function Compute provides a free disk quota of 512 MB)
Minimum Instances
If your business is sensitive to latency, after you select Elastic Instance, set Minimum Instances to 1 or greater to lock in resources in advance and reduce cold start latency.
NoteIf you set Minimum Instances to 1 or greater but do not configure a scaling policy for the minimum number of instances, or if no scaling policy is active during a specific period, the current minimum number of instances is the value you set here.
If you configure multiple scaling policies, the system calculates the Minimum Instances for each policy when it is triggered. The current Minimum Instances is the maximum value among all active scaling policies.
For more information, see How is the current minimum number of instances calculated?.
1
Instance Concurrency
You can configure multiple concurrencies for a single GPU function instance, which means a single function instance can process multiple requests simultaneously. For more information, see Configure instance concurrency.
Reserved Instances
Parameter
Description
Example
Instance Type
Select Reserved Instance to allocate instances to the function from a purchased resource pool.
We recommend that you use reserved instances for scenarios that require predictable costs, low business latency, and high resource utilization to ensure business stability.
Reserved Instance
Resource Pool
A resource pool is a pool of reserved instances that can be allocated to the target function. If the remaining quota in your resource pool is insufficient, click Scale-out in the Actions column and follow the prompts to scale out. For more information, see Resource Pools (Subscription).
Resource Pool: fc-pool-****
GPU Type: Ada
Specifications
Set the GPU Memory, vCPU, Memory, and Disk specifications for the function as needed. After you set the specifications, the usage of each resource is calculated by multiplying the specification by the duration of use. For more information, see Billing overview.
NoteAll directories on the disk are writable and share the disk space.
The disk lifetime is the same as the instance lifetime of the underlying function. When the instance is released by the system, the data on the disk is also lost. To store files persistently, you can mount a NAS file system or an OSS bucket. For more information, see Configure a NAS file system and Configure OSS access.
GPU Memory: 48 GB
vCPU: 8 vCPU
Memory: 64 GB
Disk: 512 MB (free of charge, Function Compute provides a free disk quota of 512 MB)
Number Of Reserved Instances
Allocate a number of reserved instances to the target function based on the resources in the resource pool.
1
Instance Concurrency
You can configure multiple concurrencies for a single GPU function instance, which means a single function instance can process multiple requests simultaneously. For more information, see Configure instance concurrency.
20
Function Code: Configure the runtime and code-related information for the function.
Parameter
Description
Example
Runtime
Use Sample Image: Select a sample image provided by Function Compute to quickly deploy an image function. You need to select the target image from the image list under the Container Image parameter.
Use Image from ACR: Click Select Image From ACR under the Container Image parameter. In the Select Container Image panel, select the created Container Registry Instance and ACR Image Repository. Then, find the target image in the image list below and click Select in the Actions column.
NoteYou cannot create a function using a public image from an ACR repository that belongs to another account.
If the image configured for your function changes, update your function promptly. Otherwise, function invocations will fail. For more information, see Usage notes.
When you use an Enterprise Edition instance, you can select only non-accelerated images. Do not delete the original image or the accelerated image. Otherwise, function invocations will fail. For more information, see Usage notes.
When you use an Enterprise Edition instance, image addresses in a custom domain name format are not supported.
You cannot create functions using images from repositories in ACR Enterprise Edition (Standard and Advanced) instances where image acceleration is enabled in Index-only Mode.
Since ACR introduced the Index-only Mode feature, images in newly created image repositories with image acceleration enabled in ACR Enterprise Edition (Basic) instances cannot be used to create functions. However, images in existing accelerated image repositories in ACR Enterprise Edition (Basic) instances can still be used to create functions. For more information about the Index-only Mode, see Load container images on demand.
Container Image
Select the target image.
SpringBoot Web App Sample Image
Startup Command
The startup command for the program. If you do not configure a startup command, the Entrypoint/CMD from the image is used by default.
None
Listener Port
The port on which the HTTP server in your code listens.
9000
Execution Timeout
Set the timeout period. The default Execution Timeout is 60 seconds, and the maximum is 86,400 seconds.
60
Advanced Settings: Configure instance details and the function execution timeout period.
Parameter
Description
Example
GPU Type
Select a GPU type. For more information, see Instance specifications.
Tesla Series
Specifications
Select values from the GPU Memory Size, vCPU Capacity, and Memory Capacity drop-down lists based on your business requirements. You can manually enter those values. For billing information, see Billing overview.
GPU Memory Size: 4 GB
vCPU Capacity: 2 vCPUs
Memory Capacity: 4 GB
Size of Temporary Disk
Specify the size of the disk for temporary file storage based on your business requirements. Function Compute offers a complimentary temporary disk of 512 MB. There will be a charge for using a temporary disk that is larger than this size.
NoteData can be written to any directory on the temporary disk, which allocates its space across all directories.
The temporary disk has the same lifecycle as the associated function. When a function is reclaimed by the system, all associated data is removed from the temporary disk. To store files persistently, you can mount a File Storage NAS (NAS) system or Object Storage Service (OSS) bucket to your function. For more information, see Configure a NAS file system and Configure an OSS file system.
512 MB
Execution Timeout Period
Specify the execution timeout period of a function. The timeout period is 60 seconds by default, and can be extended up to a maximum of 86,400 seconds.
60
Instance Concurrency
Function Compute provides an instance concurrency feature that allows a function to process multiple requests simultaneously. The Instance Concurrency parameter specifies the maximum number of concurrent requests a function can process. For more information, see Configure instance concurrency.
20
Time Zone
Select the time zone for your function. Once you configure the time zone of your function, an environment variable named TZ is automatically added to the function. The variable value corresponds to the time zone that you configure.
UTC
Function Role
Specify the Resource Access Management (RAM) role of the function. Function Compute uses this role to generate a temporary AccessKey pair to access 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 your function to access 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 the function 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) of Function Compute.
ImportantWhen you use a static public IP address, make sure that Allow Default NIC to Access Internet is set to No. For more information, see Configure static public IP addresses.
Yes
Logging
Specify whether to integrate with Simple Log Service. Valid values:
Enable: Function execution logs are persistently stored in Simple Log Service, which facilitates code debugging, fault analysis, and data analysis.
Disable: You cannot use Simple Log Service to store or query function execution logs.
Enable
Permissions, Network, and Storage: Configure the function access role, network, and storage mounts.
Parameter
Description
Example
Function Role
Function Compute uses this RAM role to generate temporary keys for accessing Alibaba Cloud resources and passes them to the code. For more information, see Use a function role to grant Function Compute permissions to access other Alibaba Cloud services.
mytestrole
Allow Access To VPC
Enables the function to access resources within a VPC. For more information, see Configure network settings.
Enable
VPC
This parameter is required if you set Allow Access To VPC to Yes. Create a new VPC or select the ID of the VPC you want to access from the drop-down list.
fc.auto.create.vpc.1632317****
VSwitch
This parameter is required if you set Allow Access To VPC to Yes. Create a new vSwitch or select a vSwitch ID from the drop-down list.
fc.auto.create.vswitch.vpc-bp1p8248****
Security Group
This parameter is required if you set Allow Access To VPC to Yes. Create a new security group or select a security group from the drop-down list.
fc.auto.create.SecurityGroup.vsw-bp15ftbbbbd****
Allow Internet Access From Default NIC
Specifies whether to allow the function to access the Internet through the default network interface card (NIC).
ImportantWhen using a static public IP address, you must disable Allow Internet Access From Default NIC. Otherwise, the configured static public IP address will not take effect. For more information, see Configure a static public IP address.
Enable
Mount NAS File System
Configure a NAS file system for the function to persistently store shared data, such as models shared by multiple inference functions.
If you choose automatic configuration, the system uses an existing General-purpose NAS file system named Alibaba-Fc-V3-Component-Generated by default. If no such NAS file system exists under the current account, the system automatically creates one.
Enable
Mount OSS Object Storage
Mount an OSS bucket to the function for persistent storage of logs, business files, and more. For more information, see Configure OSS access.
Enable
Logs and Tracing
Parameter
Description
Example
Logging
Set this to persistently store function execution logs in Simple Log Service for code debugging, troubleshooting, and data analytics. For more information, see Configure the logging feature.
Automatic Configuration: Automatically selects a log project that starts with
serverless-<region_id>.Only one such log project is created per region. If the system finds an existing log project with this name in the current region, it will be used directly.
Custom Configuration: You must manually specify the target Log Project and Logstore.
Enable
More Configurations
Parameter
Description
Example
Time Zone
Select the time zone for the function. After you set the time zone, an environment variable TZ is automatically added to the function with the value of the selected time zone.
UTC
Tags
Set tags for the function to manage functions by group. You must set both a tag key and a tag value.
key : value
Resource Group
Select the resource group to which the function belongs to manage functions by group.
Default Resource Group
Environment Variables
Use environment variables to flexibly adjust the function's behavior without modifying the code. For more information, see Configure environment variables.
{ "BUCKET_NAME": "MY_BUCKET", "TABLE_NAME": "MY_TABLE" }
After the function is created, you can view and update it in the function list.
When you update a function, you can only change the configured listener port. You cannot delete or add listener ports. If a listener port is configured when you create the function, that port is retained when you update the function if you do not specify a new one.
Use Serverless Devs to create a function
You can use Serverless Devs to build and push a container image and deploy a function with a single command.
Run the following command to initialize the project.
sudo s initFollow the prompts to select an Alibaba Cloud account, a custom image template, and a programming language. This topic uses Node.js as an example. Then, set a project name, select a region for deployment, and enter your ACR image information.
Run the following command to go to the project directory.
cd start-fc3-custom-container-nodejsEdit the
s.yamlfile. For more information about the parameters in the YAML file, see YAML specifications.The following sample code shows an example.
In the example,
imageis your ACR image. Replace <your namespace>, <your image>, and <your tag> with the actual namespace name, image repository name, and image version. If you entered the correct ACR image when you initialized the project in Step 1, you do not need to modify it here.edition: 3.0.0 name: hello-world-app # access: Specifies the key information required by the current application. # For information about how to configure keys, see https://www.serverless-devs.com/serverless-devs/command/config # For information about the key usage order, see https://www.serverless-devs.com/serverless-devs/tool#Key-usage-order-and-specifications access: "default" vars: # Global variables region: "cn-hangzhou" resources: hello_world: # To perform operations only on the hello_world service, add hello_world to the command. Example: # Build only hello_world: s hello_world build # If you run s build directly without specifying hello_world, the tool builds all services at the same level as hello_world in the YAML file (such as the commented-out next_function below) in a specific order. component: fc3 # Component name actions: # Custom execution logic pre-deploy: # Run before deployment - component: fc3 build --dockerfile ./code/Dockerfile # The component to run. Format: "component: ComponentName Command Parameter" props: region: ${vars.region} # For more information about how to use variables, see https://docs.serverless-devs.com/serverless-devs/yaml#Variable-assignment functionName: "start-nodejs-ufrz" runtime: "custom-container" description: 'hello world by serverless devs' timeout: 30 memorySize: 512 cpu: 0.5 diskSize: 512 code: ./code customContainerConfig: image: 'registry.${vars.region}.aliyuncs.com/<your namespace>/<your image>:<your tag>' # Your ACR image. Replace <your namespace>, <your image>, and <your tag> with the actual namespace name, image repository name, and image version. # triggers: # - triggerName: httpTrigger # Trigger name # triggerType: http # Trigger type # description: 'xxxx' # qualifier: LATEST # The version of the service to trigger # triggerConfig: # authType: anonymous # Authentication type. Valid values: anonymous, function # disableURLInternet: false # Specifies whether to disable public access to the URL # methods: # HTTP methods supported by the HTTP trigger. Valid values: GET, POST, PUT, DELETE, HEAD # - GET # - POSTRun the following command to deploy the project.
sudo s deploySample output:
Steps for [deploy] of [hello-world-app] ==================== DEPRECATED: The legacy builder is deprecated and will be removed in a future release. BuildKit is currently disabled; enable it by removing the DOCKER_BUILDKIT=0 environment-variable. Sending build context to Docker daemon 5.12kB Step 1/7 : FROM node:14-buster 14-buster: Pulling from library/node 2ff1d7c41c74: Already exists b253aeafeaa7: Already exists 3d2201bd995c: Already exists 1de76e268b10: Already exists d9a8df589451: Already exists 6f51ee005dea: Already exists 5f32ed3c3f27: Already exists 0c8cc2f24a4d: Already exists 0d27a8e86132: Already exists Digest: sha256:a158d3b9b4e3fa813fa6c8c590b8f0a860e015ad4e59bbce5744d2f6fd8461aa Status: Downloaded newer image for node:14-buster ---> 1d12470fa662 Step 2/7 : WORKDIR /usr/src/ ---> Running in 70a8e2e4d1ea Removing intermediate container 70a8e2e4d1ea ---> 0d67b8fa2901 Step 3/7 : COPY package*.json ./ ---> 09eb15f8770a Step 4/7 : RUN npm install ---> Running in 8ae492be973b Step 5/7 : COPY . . ---> 7560c7b14431 Step 6/7 : EXPOSE 9000 ---> Running in 66b38e54ced0 Removing intermediate container 66b38e54ced0 ---> f73cce48d2ae Step 7/7 : ENTRYPOINT [ "node", "server.js" ] ---> Running in 2fb2f83fd6c0 Removing intermediate container 2fb2f83fd6c0 ---> fe51ae71448c Successfully built fe51ae71448c Successfully tagged registry.cn-hangzhou.aliyuncs.com/z****/z****:latest [2024-01-29 16:33:06][INFO][hello_world] get instanceName= and region=cn-hangzhou from registry.cn-hangzhou.aliyuncs.com/z****/z**** [2024-01-29 16:33:06][INFO][hello_world] try to docker push registry.cn-hangzhou.aliyuncs.com/z****/z**** ... WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded Using default tag: latest The push refers to repository [registry.cn-hangzhou.aliyuncs.com/z****/z****] 85c1ec915b45: Pushed 37c36543a431: Pushed e4afd7f70434: Pushed 0d5f5a015e5d: Layer already exists 3c777d951de2: Layer already exists f8a91dd5fc84: Layer already exists cb81227abde5: Layer already exists e01a454893a9: Layer already exists c45660adde37: Layer already exists fe0fb3ab4a0f: Layer already exists f1186e5061f2: Layer already exists b2dba7477754: Layer already exists latest: digest: sha256:6bf1ed4119d197a46c99082577632957056cb625f2ee0276d2af53f60d22837d size: 2841 [hello_world] completed (688.45s) Result for [deploy] of [hello-world-app] ==================== region: cn-hangzhou cpu: 0.5 customContainerConfig: image: registry.cn-hangzhou.aliyuncs.com/z****/z**** resolvedImageUri: registry.cn-hangzhou.aliyuncs.com/z****/z****@sha256:6bf1ed4119d197a46c99082577632957056cb625f2ee0276d2af53f60d22837d description: hello world by serverless devs diskSize: 512 functionName: start-nodejs-ufrz handler: handler instanceConcurrency: 1 internetAccess: true lastUpdateStatus: Successful memorySize: 512 role: runtime: custom-container state: Active timeout: 30 A complete log of this run can be found in: /root/.s/logs/0129162246Run the following command to test the function.
sudo s invoke -e "{\"key\":\"val\"}"Sample output:
Steps for [invoke] of [hello-world-app] ==================== ========= FC invoke Logs begin ========= FC Invoke Start RequestId: 1-65b764db-15eb737f-0c67ab5cd968 FC Invoke Start RequestId: 1-65b764db-15eb737f-0c67ab5cd968 hello world! FC Invoke End RequestId: 1-65b764db-15eb737f-0c67ab5cd968 Duration: 42.27 ms, Billed Duration: 43 ms, Memory Size: 512 MB, Max Memory Used: 47.77 MB ========= FC invoke Logs end ========= Invoke instanceId: c-65b764db-15fa2aa8-bc50f7839399 Code Checksum: undefined Qualifier: LATEST RequestId: 1-65b764db-15eb737f-0c67ab5cd968 Invoke Result: OK [hello_world] completed (4.96s) A complete log of this run can be found in: /root/.s/logs/0129164202
References
Using custom image functions requires additional time to download and decompress the container image. To reduce cold start latency, see Best practices for reducing cold start latency in Function Compute.
You can also call an API to create a function. For more information, see CreateFunction.
For information about the scenarios for and differences between built-in runtimes, custom runtimes, and custom image runtimes in Function Compute, see Select a runtime.