This topic describes how to create a custom container function in the Function Compute console or by using Serverless Devs.

Prerequisites

Create a function in the console

  1. Push your function image to the image repository for the default instance.

    In this example, the sample project is in the /tmp directory, Function Compute is activated in the China (Shenzhen) region, and the name of the image repository is nodejs-express.

    1. Run the following command to go to the /tmp directory:
      cd /tmp
    2. Run the following command in the /tmp directory to clone the sample project:
      git clone https://github.com/awesome-fc/custom-container-docs.git
    3. Run the following command to go to the project directory:
      cd custom-container-docs/nodejs-express
    4. Run the following command to specify the image repository. Specify the name of your image in the command. In the following sample command, the image name is registry.cn-shenzhen.aliyuncs.com/fc-demo/nodejs-express:v0.2.
      export IMAGE_NAME="registry.cn-shenzhen.aliyuncs.com/fc-demo/nodejs-express:v0.2" 
    5. Run the following command to package the image:
      docker build -t $IMAGE_NAME .
    6. Run the following command to push the image:
      docker push $IMAGE_NAME
  2. Create a service and grant the service permissions.
    1. In the Function Compute console, create a service. For more information, see Create a Service.
    2. 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 the temporary account for the default instance in Container Registry. Then, Function Compute uses the temporary account to push the image from your private image repository.
  3. Create a function.
    1. Log on to the Function Compute console.
    2. In the left-side navigation pane, click Services and Functions.
    3. In the top navigation bar, select the region where your Kubernetes cluster is deployed.
    4. On the Services page, click the target service.
    5. On the Functions page, click Create Function.
    6. On the Create Function page, select Use Container Image, set parameters in the Basic Settings section, and then click Create.
      create-custom-container
      Parameter Required Description Example
      Function Name No Enter a name for the function. The name can be up to 64 characters in length and can contain digits, letters, underscores (_), and hyphens (-). The function name is case-sensitive and must start with a letter.
      Note Function Compute automatically creates a name for your function if you leave this field empty.
      Function
      Container Image Yes Click Select a Container Registry image. In the Select Container Image dialog box that appears, configure Container Image Instance and Container Registry Repository. Among the images that appear, find the desired image and click Select in the Actions column. test-registry-vpc.cn-chengdu.cr.aliyuncs.com/test/registry:latest
      Listening Port Yes Specify the port on which the HTTP server that is implemented by using the container image listens. The default port is 9000. 9000
      Command No Configure the startup command of the container in the ["python","server.py"] format. If you do not configure this parameter, the ENTRYPOINT or CMD command in the image is used. ["/code/myserver"]
      Args No Configure the startup parameters of the container in the ["--port","9000"] format. If you do not configure this parameter, the CMD command in the image is used. ["-arg1", "value1"]
      Function Trigger Mode Yes Select a trigger mode for the function.
      • Trigger by Events: If you select this option, the function is triggered by a time trigger or a trigger of another Alibaba Cloud service.
      • Trigger by HTTP Requests: If you select this option, the function is triggered by HTTP requests. This mode is used for scenarios in which you want to develop web services in an efficient manner.
      Trigger by Events
      Instance Category Yes Select an instance category.
      • Elastic Instance
      • Performance Instance
      For more information, see Instance types and instance modes. For more information about the billing for different instance types, see Overview.
      Elastic Instance
      Memory Capacity Yes Specify the size of the memory that is used to execute the function by using one of the following methods:
      • Select a value: Select a value from the drop-down list.
      • Input a value: Click Enter Memory Size and enter a value for the memory size. Valid values:
        • Elastic Instance: [128, 3072]. Unit: MB.
        • Performance Instances: [4, 32]. Unit: GB.
        Note The value must be a multiple of 64 MB.
      512 MB
      After the function is created, the function is displayed in the function list of the service.

Use Serverless Devs to create a function

You can use Serverless Devs to build and push container images and deploy functions with a few clicks.

  1. Run the following command to initialize a project:
    s init start-fc-custom-container-event-nodejs14
    Sample command output:
     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: Actions are declared in the s.yaml file:
            Run the following command before project deployment: s build --use-docker --dockerfile ./code/Dockerfile.
           If you do not need to build the project, you can use annotations to skip the preceding command. 
           > Before the deployment, replace the value of the image parameter in the s.yaml file with the address of your image 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
  2. Run the following command to go to the project directory:
    cd start-fc-custom-container-event-nodejs14
  3. Edit the s.yaml file.
    Replace the value of the image parameter with the address of your image in Container Registry.
  4. Run the following command to deploy the project:
    s deploy
    Sample command output:
    [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: LTAI4G4cwJkK4Rza6xd9****
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
    [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
  5. Run the following command to debug the function:
    s invoke -e "{\"key\":\"val\"}"
    Sample command output:
    [2021-12-15 08:00:17] [INFO] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 768945c8-f92d-428e-89c2-ecd50883****
    null
    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