If built-in runtimes in Function Compute cannot meet your business requirements, you can use Custom Container runtimes to create functions. This topic describes how to create a Custom Container function by using the Function Compute console or Serverless Devs.
Usage notes
To create a Custom Container function in Function Compute, you must use an Alibaba Cloud Container Registry image that resides in the same region as your function and belongs to the same account. If you use an ARM-based computer such as a Mac computer that runs on Apple chips, you must specify the image compilation platform as linux/amd64. The following is a sample command: docker build --platform linux/amd64 -t $IMAGE_NAME .
.
Prerequisites
Container Registry
A Container Registry instance is created. For more information, see Create a Container Registry Enterprise Edition instance.
NoteYou can create a Container Registry Enterprise Edition or a Container Registry Personal Edition instance. We recommend the former.
Container Registry Personal Edition is for individual developers, who receive a limited free quota during the public preview period. No service level agreement (SLA) or compensation is offered for this edition, and usage is subject to certain limits. For more information about the limits, see Usage notes.
A namespace is created. For more information, see Create a namespace.
An image repository is created. For more information, see Create an image repository.
Serverless Devs
Create a function in the Function Compute console
Step 1: Create a service and configure permissions
Create a function in the Function Compute console.
For more information, see Create a service.
Attach the AliyunContainerRegistryReadOnlyAccess or AliyunContainerRegistryFullAccess policy to the service.
For more information, see Grant Function Compute permissions to access other Alibaba Cloud services. The preceding policies allow Function Compute to obtain a temporary account that can be used to log on to the default Container Registry instance to push images from your private image repository.
Step 2: Create a function
Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, click Create Function.
On the Create Function page, select Use Container Image, configure the following parameters based on your business requirements, and then click Create.
Basic Settings: Configure the basic information about the function that you want to create. Such information includes the function name and the handler type.
Parameter
Description
Function Name
Enter a name for the function. Function Compute automatically creates a name for your function if you leave this parameter empty.
Handler Type
Specify the type of requests that are processed by the function. The requests can be event requests or HTTP requests. Valid values:
Event Handler: triggers function execution by using a timer, calling API operations, using SDKs, or using triggers of other Alibaba Cloud services.
HTTP Handler: triggers function execution by sending HTTP requests or establishing WebSocket connections. For a web scenario, we recommend that you select Use Custom Runtime to create a function.
Image Configurations: Configure an image for the function.
Parameter
Description
Image Selection Mode
Select Use Sample Images or use your own image to create the function.
Use Sample Image: Select a built-in sample image of Function Compute.
Use Container Registry Images: Use a Container Registry image. Click Select a Container Registry image below Container Image. In the Select Container Image panel, configure the Container Image Instance and Container Registry Repository parameters. Find the image that you want to use and click Select in the Actions column.
NoteYou cannot use public images that belong to another account from Container Registry to create a function.
For Container Registry Enterprise Edition, Function Compute uses the tags of the image you select to create function instances.
If you use an image of Container Registry Enterprise Edition (Advanced Edition or Standard Edition), we recommend that you turn on Immutable for Image Version. Otherwise, the image tag may be updated elsewhere and Function Compute pulls the updated image data to start function instances. For more information, see Configure a repository to be immutable.
If you use an image of Container Registry Enterprise Edition (Basic Edition) or Container Registry Personal Edition, you must update the function based on the image information in the Function Compute console after you update the image tag with the same name in the image repository. Then, Function Compute can start function instances with the new image.
Startup Command
Specify the startup command of the container. If you do not configure this parameter, the Entrypoint or CMD instruction in the image is used.
Listening Port
Specify the port on which the HTTP server that is implemented in the container image listens. The default port is 9000.
Advanced Settings: Configure advanced settings, such as the instance-related information and execution timeout period.
Parameter
Description
GPU Acceleration
Specify whether to use GPU-accelerated instances based on your business requirements. By default, CPU instances, instead of GPU-accelerated instances, are used. For more information, see Instance types. For information about the billing of each instance type, see Billing overview. Valid values:
Enable GPU
Disable GPU
Specifications
Enable GPU:
Select a value from the GPU Type drop-down list and specify GPU Memory Size based on your business requirements. Function Compute auto populates the vCPU Capacity and Memory Capacity fields based on the GPU memory size you select. Only specific vCPU and memory capacities are allowed for each GPU size.
Disable GPU:
Configure vCPU Capacity and Memory Capacity based on your business requirements.
For information about the billing of resources, see Billing overview.
NoteThe ratio of vCPU specification to memory capacity (in GB) must be from 1:1 to 1:4.
Size of Temporary Disk
Select the disk size based on your business requirements. Function Compute provides a free disk capacity of 512 MB. For more information, see Billing overview.
Instance Concurrency
Specify the instance concurrency. For more information, see Configure instance concurrency.
Execution Timeout Period
Specify the timeout period for function executions. The default value is 60 seconds. The maximum value is 86400 seconds. We recommend that you set this parameter to 600 seconds.
Time Zone
Specify the time zone of the function. After you select the time zone, the environment variable TZ is automatically added to the function. The value of the variable is the time zone that you select.
Environment Variables: Configure the environment variables in the runtime of the function. For more information, see Environment variables.
Trigger Configurations: Configure a trigger for function invocations. For more information, see Manage triggers.
After the function is created, you can view and update the function in the function list of the service.
When you update a function, you can change the specified listening port, but you cannot delete the listening port or add extra ones. If a listening port is specified when you create the function, the listening port is retained when you update the function if you do not specify another listening port.
Create a function by using Serverless Devs
You can use Serverless Devs to build and push container images and deploy functions with a few clicks.
Run the following command to initialize a project. You must enter the address of the image repository that you created in Container Registry as prompted.
sudo s init start-fc-custom-container-event-nodejs14
The following output is returned:
Serverless Awesome: https://github.com/Serverless-Devs/package-awesome Please input your project name (init dir) start-fc-custom-container-event-nodejs14 file decompression completed ____ _ _ ___ _ _ _ _____ ____ / _ \/ \ / \\ \/// \ /\/ \ /| / // _\ | / \|| | | | \ / | | ||| |\ || | __\| / | |-||| |_/\| | / / | \_/|| | \|| | | | \__ \_/ \|\____/\_//_/ \____/\_/ \| \_/ \____/ please select credential alias default Welcome to the Aliyun FC start application This application requires to open these services: FC : https://fc.console.aliyun.com/ ACR: https://cr.console.aliyun.com/ * Note: The actions information is declared in the s.yaml file: Run the sudo s build --use-docker --dockerfile ./code/Dockerfile command before the project is deployed. If you do not need to build the project, you can comment out this part. > Note: Before project deployment, replace the value of the image parameter in the s.yaml file with the address of the image repository in Container Registry. * The project is initialized. You can go to the project directory and run the s deploy command to deploy the project. Thanks for using Serverless-Devs You could [cd /test/test1/start-fc-custom-container-event-nodejs14] and enjoy your serverless journey! If you need help for this example, you can use [s -h] after you enter folder. Document Star: https://github.com/Serverless-Devs/Serverless-Devs Do you want to deploy the project immediately? No
Run the following command to go to the directory of the project:
cd start-fc-custom-container-event-nodejs14
Edit the
s.yaml
file. For more information about the parameters in the .yaml file, see YAML syntax and permission management.Run the following command to deploy the project:
sudo s deploy
The following output is returned:
[2021-12-15 07:54:30] [INFO] [S-CLI] - Start ... [2021-12-15 07:54:30] [INFO] [S-CLI] - Start the pre-action [2021-12-15 07:54:30] [INFO] [S-CLI] - Action: s build --use-docker --dockerfile ./code/Dockerfile [2021-12-15 07:54:31] [INFO] [S-CLI] - Start ... [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Build artifact start... [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Use docker for building. [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Building image... Sending build context to Docker daemon 5.12kB Step 1/7 : FROM node:14.5.0-alpine3.11 ---> 5d97b3d11dc1 ...... Step 7/7 : ENTRYPOINT [ "node", "server.js" ] ---> Using cache ---> a5ef1c015e7e Successfully built a5ef1c015e7e Successfully tagged registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories. Build image(registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx) successfully [2021-12-15 07:54:33] [INFO] [FC-BUILD] - Build artifact successfully. Tips for next step ====================== * Invoke Event Function: s local invoke * Invoke Http Function: s local start * Deploy Resources: s deploy End of method: build [2021-12-15 07:54:33] [INFO] [S-CLI] - End the pre-action [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using region: cn-hangzhou [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using access alias: default [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeyID: yourAccessKeyID [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeySecret: yourAccessKeySecret [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists [2021-12-15 07:54:35] [INFO] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole [2021-12-15 07:54:35] [INFO] [RAM] - Checking Role AliyunFCDefaultRole exists [2021-12-15 07:54:35] [INFO] [RAM] - Updating role: AliyunFCDefaultRole [2021-12-15 07:54:35] [INFO] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists [2021-12-15 07:54:35] [INFO] [FC-DEPLOY] - Checking Function nodejs14-event-function exists [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Using image registry: registry.cn-hangzhou.aliyuncs.com [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Try to use a temporary token for docker login Login to registry: registry.cn-hangzhou.aliyuncs.com with user: cr_temp_user Pushing docker image: registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx... The push refers to repository [registry.cn-hangzhou.aliyuncs.com/fc-example/test] cdf38e7753b7: Layer already exists 43128f71725b: Layer already exists 0fb36a16ab83: Layer already exists dd966b9fd474: Layer already exists a1915d7a1111: Layer already exists c4491b3ee709: Layer already exists 9fb10d900487: Layer already exists 3e207b409db3: Layer already exists nginx: digest: sha256:02b69157def85ceb72f32cb1c5845d00e1d8df19caf6eaf720a9bc77bb57db76 size: 1991 √ Make service hello-world-service success. √ Make function hello-world-service/nodejs14-event-function success. [2021-12-15 07:54:39] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists [2021-12-15 07:54:39] [INFO] [FC-DEPLOY] - Checking Function nodejs14-event-function exists There is auto config in the service: hello-world-service Tips for next step ====================== * Display information of the deployed resource: s info * Display metrics: s metrics * Display logs: s logs * Invoke remote function: s invoke * Remove Service: s remove service * Remove Function: s remove function * Remove Trigger: s remove trigger * Remove CustomDomain: s remove domain helloworld: region: cn-hangzhou service: name: hello-world-service function: name: nodejs14-event-function runtime: custom-container handler: not-used memorySize: 256 timeout: 60
Run the following command to debug the function:
sudo s invoke -e "{\"key\":\"val\"}"
The following output is returned:
[2021-12-15 08:00:17] [INFO] [S-CLI] - Start ... ========= FC invoke Logs begin ========= FC Invoke Start RequestId: 768945c8-f92d-428e-89c2-ecd50883**** {"key":"val"} FC Invoke End RequestId: 768945c8-f92d-428e-89c2-ecd50883**** Duration: 3.05 ms, Billed Duration: 4 ms, Memory Size: 256 MB, Max Memory Used: 10.77 MB ========= FC invoke Logs end ========= FC Invoke Result: OK End of method: invoke
References
If you want to use GPU-accelerated instances, you must create Custom Container functions. For more information about GPU-accelerated instances, see Instance types and usage modes.
Compared to code packages, container images often require more time for downloading and decompression due to their inclusion of necessary runtime environments. For information on how to mitigate this latency, see Best practices for cold start mitigation.
You can also call the Function Compute API to create a function. For more information, see CreateFunction.
For information about the common scenarios of and differences among built-in runtimes, custom runtimes, and Custom Container runtimes, see Function runtime selection.