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

Function Compute:インスタンスタイプの指定

最終更新日:Sep 11, 2024

このトピックでは、SDK for Goのサンプルコードを使用してインスタンスタイプを指定する方法について説明します。

SDKを使用して、関数を作成または更新するときにエラスティックインスタンスまたはパフォーマンスインスタンスを使用するかどうかを指定できます。 次に、システムはリクエストを指定されたインスタンスタイプにルーティングします。

次の項目は、インスタンスタイプパラメーターの値を示しています。

  • e1: エラスティックインスタンス

  • c1: パフォーマンスインスタンス

インスタンスタイプパラメーターを設定しない場合、デフォルトでエラスティックインスタンスが使用されます。

SDK サンプルコード

//  Go sdk
package main

import (
    "os"
    "github.com/aliyun/fc-go-sdk"
)

func main() {
    // new fc client
    /*
    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 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. 
    */
    accessKeyID := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
    client, _ := fc.NewClient("endpoint", "2016-08-15",
        accessKeyID, accessKeySecret)

    // CreateService
    serviceName := "TestService"
    _, err := client.CreateService(fc.NewCreateServiceInput().
        WithServiceName(serviceName))
    if err != nil {
        panic(err)
    }

    // CreateFunction
    functionName := "TestFunction"
    createFunctionInput := fc.NewCreateFunctionInput(serviceName).
        WithFunctionName(functionName).
        WithHandler("index.handler").
        WithRuntime("python2.7").
        WithCode(fc.NewCode().WithFiles("./code/index.py")).
        WithTimeout(5).
        WithInstanceType("c1") // Specifies that the function uses performance instances. 
    _, err = client.CreateFunction(createFunctionInput)
    if err != nil {
        panic(err)
    }

    return
}