This topic describes how to create a custom container function.

SDK sample code

The following example demonstrates how to create a custom container function. SDK for Go is used in the example.

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)
}

Image pull acceleration configurations

When you configure image pull acceleration, take note of the following points:

When you call the CreateFunction or UpdateFunction operation to create or update a function, you can specify the AccelerationType parameter (string) in the CustomContainerConfig structure. Valid values for the AccelerationType parameter:

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