すべてのプロダクト
Search
ドキュメントセンター

Function Compute:カスタムコンテナ関数の作成

最終更新日:Sep 11, 2024

このトピックでは、SDKを使用してカスタムコンテナー関数を作成する方法の例を示します。

SDKの例

package main

import (
    "fmt"
    "os"
    "github.com/aliyun/fc-go-sdk"
)
func main() {
    /*
    The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. Using these credentials to perform operations in Function Compute is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    We recommend that you do not save the AccessKey ID and AccessKey secret to your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. 
    In this example, the AccessKey pair is saved to the environment variables for authentication. 
    Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your local environment before you run the sample code. 
    In the runtime environments of Function Compute, the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are automatically configured after you configure the execution permissions. 
   */
    fcClient, err := fc.NewClient(fmt.Sprintf("%s.cn-shanghai.fc.aliyuncs.com", os.Getenv("ACCOUNT_ID")),
        "2016-08-15", os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_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")))
    if err != nil {
        panic(err)
    }
    fmt.Printf("FC CreateFunction response: %+v\n", *respC)

}