文本介绍使用SDK创建Custom Container Runtime函数。

SDK示例

本文以Go语言为例,可以通过以下代码示例创建Custom Container Runtime函数。

import (
    "fmt"

    fc "github.com/aliyun/fc-go-sdk"
)

const (
    AK_ID     = "your ak id"
    AK_SECRET = "your ak secret"
    ROLE      = "your role arn" // role with AliyunContainerRegistryReadOnlyAccess/AliyunContainerRegistryFullAccess, for example: acs:ram::123456:role/service-role
    IMAGE     = "your ACR image" // for example: registry-vpc.cn-shanghai.aliyuncs.com/fc-test/hello-world:v1
)

func main() {
    serviceName := "fc-demo-ccr"
    funcName := "test"
    client, _ := fc.NewClient("http://123456.cn-shanghai.fc.aliyuncs.com", "2016-08-15", AK_ID, AK_SECRET)

    _, err := client.CreateService(fc.NewCreateServiceInput().
        WithServiceName(serviceName).
        WithRole(ROLE).
        WithDescription("this is a test service for docker image"))
    if err != nil {
        panic(err)
    }

    // CreateFunction
    createFunctionInput := fc.NewCreateFunctionInput(serviceName).WithFunctionName(funcName).
        WithDescription("test function").
        WithHandler("just-a-string").WithRuntime("custom-container").
        WithCustomContainerConfig(fc.NewCustomContainerConfig().WithImage(IMAGE)).
        WithTimeout(30)

    _, err = client.CreateFunction(createFunctionInput)
    if err != nil {
        panic(err)
    }

    invokeInput := fc.NewInvokeFunctionInput(serviceName, funcName).WithPayload([]byte("hello world"))
    invokeOutput, err := client.InvokeFunction(invokeInput)
    if err != nil {
        panic(err)
    } else {
        fmt.Printf("InvokeFunction response: %s \n", string(invokeOutput.Payload))
    }
}