Compared with function code packages, container images are easier to be migrated between technology stacks. In addition, container images feature a comprehensive and standard tool chain ecosystem and provide best practices. However, container images contain files that are not used by applications, such as operating systems and command tools. These files inevitably increase the amount of data to be downloaded and decompressed, and prolongs the cold start for functions. It can take quite a few minutes to pull a large image whose size is larger than 1 GB before decompression. This greatly deteriorates the cold start performance of function instances. To resolve this issue, Function Compute has made a series of optimizations to improve the pull speed of container images by two to five times in different scenarios. The time consumed to pull an image is reduced from minutes to seconds. This topic describes the principles, precautions, and configuration method of image pull acceleration.

Principles

After you enable image pull acceleration, Function Compute assumes the RAM role specified for a service each time you perform a CreateFunction or UpdateFunction operation. Function Compute assumes the RAM role to pull images by using a temporary AccessKey secret and transfer the images to the image cache service of Function Compute. After the images are transferred, the pull speed of custom container images is significantly improved for functions.

Precautions

  • If you agree to enable image pull acceleration, you agree to authorize Function Compute to pull images from your repository and transfer them to the image cache service of Function Compute. The image data is encrypted and provided with network isolation and identity authentication to ensure data security. Before you enable this feature, make sure that the operation of transferring images to the image cache service of Function Compute complies with the security regulations and guidelines of your organization.
  • After a function is created or updated, it takes a specific amount of time to transfer the image. Therefore, cache miss may occur before the cached image is available. The cached image is usually available in 5 minutes after the function is created or updated.

Configuration method

To configure image pull acceleration, perform the following operation:

When you create or update a function, set the accelerationType field of the String type in the customContainerConfig structure to one of the following enumerated values:

  • Default: enables image pull acceleration.
  • None: disables image pull acceleration.

The following code provides an example on how to enable or disable image pull acceleration by using Function Compute SDK for Go:

package main

import (
    "fmt"
    "os"

    "github.com/aliyun/fc-go-sdk"
)
func main() {
    fcClient, err := fc.NewClient(fmt.Sprintf("%s.cn-shanghai.fc.aliyuncs.com", os.Getenv("ACCOUNT_ID")),
        "2016-08-15", os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_ID_SECRET"))
    if err ! = nil {
        panic(err)
    }


    // Create a function, with CustomContainerConfig.AccelerationType to be Default,
    // which enables image pull acceleration
    respC, err := fcClient.CreateFunction(fc.NewCreateFunctionInput("demo-service").
        WithFunctionName("demo-function").
        WithHandler("bootstrap").
        WithRuntime("custom-container").
        WithCustomContainerConfig(fc.NewCustomContainerConfig().
            WithImage("registry.cn-shenzhen.aliyuncs.com/fc-demo/nodejs-express:v1").
            WithAccelerationType("Default")))
    if err ! = nil {
        panic(err)
    }
    fmt.Printf("FC CreateFunction response: %+v\n", *respC)

    // Update a function, with CustomContainerConfig.AccelerationType to be None,
    // which disables image pull acceleration
    respU, err := fcClient.UpdateFunction(fc.NewUpdateFunctionInput("demo-service", "demo-function").
        WithCustomContainerConfig(fc.NewCustomContainerConfig().
            WithImage("registry.cn-shenzhen.aliyuncs.com/fc-demo/nodejs-express:v1").
            WithAccelerationType("")))
    if err ! = nil {
        panic(err)
    }
    fmt.Printf("FC UpdateFunction response: %+v\n", *respU)
}