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

Function Compute:CLIリファレンス (2021-04-16)

最終更新日:Sep 11, 2024

Alibaba Cloud CLIは、Alibaba Cloud OpenAPIに基づくツールです。 Alibaba Cloud CLIを使用してAPIを呼び出し、サービスを管理できます。 このトピックでは、Alibaba Cloud CLIを使用してFunction Compute API操作を呼び出す方法と例について説明します。

前提条件

  • Alibaba Cloud CLIをインストールしました。 詳細については、「インストールガイド」をご参照ください。

  • Alibaba Cloud CLIを設定済みです。 詳細については、「Alibaba Cloud CLIの設定」をご参照ください。

  • Alibaba Cloud CLIを使用してRESTful APIを呼び出すコマンド構造を学習しました。これは、Function Compute APIのアーキテクチャスタイルです。 詳細については、「RPC APIおよびRESTful APIの呼び出し」をご参照ください。

入門ガイド

次の手順を実行して、Alibaba Cloud CLIを使用してサービスの作成から関数の呼び出しまでの全プロセスを完了します。

  1. testServiceNameという名前のサービスを作成します。

    aliyun fc-open POST /2021-04-06/services  --body "{\"serviceName\":\"testServiceName\",\"description\":\"testServiceName\"}"

    レスポンス例:

    {
            "createdTime": "2022-11-15T07:53:04Z",
            "description": "test1",
            "internetAccess": true,
            "lastModifiedTime": "2022-11-15T07:53:04Z",
            "logConfig": {
                    "enableInstanceMetrics": false,
                    "enableRequestMetrics": false,
                    "logBeginRule": null,
                    "logstore": "",
                    "project": ""
            },
            "nasConfig": {
                    "groupId": 0,
                    "mountPoints": [],
                    "userId": 0
            },
            "ossMountConfig": {
                    "mountPoints": []
            },
            "role": "",
            "serviceId": "c98c7a92-48bc-49a4-ba4b-7c74cf38****",
            "serviceName": "testServiceName",
            "tracingConfig": {
                    "jaegerConfig": null,
                    "params": null,
                    "type": null
            },
            "vendorConfig": null,
            "vpcConfig": {
                    "anytunnelViaENI": null,
                    "role": "",
                    "securityGroupId": "",
                    "vSwitchIds": [],
                    "vpcId": ""
            }
    }
  2. 関数を作成します。

    説明
    • Alibaba Cloud CLIを使用してコードパッケージを直接アップロードすることはできません。 コードパッケージをに圧縮できます。zipパッケージをオンプレミス環境でアップロードします。zipパッケージをObject Storage Service (OSS) バケットに追加します。 次に、Alibaba Cloud CLIで関数を作成するときにOSSバケットとオブジェクトを使用するように指定できます。

    • デフォルトでは、Alibaba Cloud CLIを使用して作成されたOSSリソースは、Function Computeリソースと同じリージョンにあります。

    1. OSSバケットを作成し、バケットのアクセス制御リスト (ACL) をprivateに設定します。

      aliyun oss mb oss://test-bucket-name-1 --acl private
    2. デプロイするコードをパッケージ化するFunction Computeとして。zipパッケージを作成し、OSSバケットにアップロードします。

      aliyun oss cp code.zip oss://test-bucket-name-1/
    3. testServiceNameサービスで、testFunctionNameという名前の関数を作成します。

      aliyun fc-open POST /2021-04-06/services/testServiceName/functions --body "{\"functionName\": \"testFunctionName\",\"runtime\": \"python3\",\"handler\": \"index.handler\",\"code\": {\"ossBucketName\": \"test-bucket-name-1\",\"ossObjectName\": \"code.zip\"}}"

      レスポンス例:

      {
              "caPort": null,
              "codeChecksum": "10564808724700284365",
              "codeSize": 409818,
              "createdTime": "2022-11-15T08:23:30Z",
              "customContainerConfig": null,
              "customDNS": null,
              "customHealthCheckConfig": null,
              "customRuntimeConfig": null,
              "description": "",
              "environmentVariables": {},
              "functionId": "c0b8ed1b-b5cb-42c0-ac88-22d98681****",
              "functionName": "testFunctionName",
              "gpuMemorySize": null,
              "handler": "index.handler",
              "initializationTimeout": 3,
              "initializer": null,
              "instanceConcurrency": 1,
              "instanceLifecycleConfig": null,
              "instanceSoftConcurrency": null,
              "instanceType": "e1",
              "lastModifiedTime": "2022-11-15T08:23:30Z",
              "layers": null,
              "layersArnV2": null,
              "memorySize": 128,
              "runtime": "python3",
              "timeout": 3
      }
  3. 関数を呼び出します。Invoke the function.

    aliyun fc-open POST /2021-04-06/services/testServiceName/functions/testFunctionName/invocations

    レスポンス例:

    hello world
説明

次のいずれかの方法を使用して、-- bodyパラメーターを設定できます。

  • [開始] セクションで操作を実行し、エスケープ後にコンテンツを -bodyに書き込みます。

  • 指定するコンテンツを含むJSONファイルを使用します。

    aliyun fc-open POST /2021-04-06/services --body "$(cat createService.json)"

    例:

    {
        "serviceName": "testServiceName"
    }

サービスを管理するためのサンプルコマンド

// serviceName: testServiceName

// Create a service.
aliyun fc-open POST /2021-04-06/services --body "{\"serviceName\":\"testServiceName\",\"description\":\"testServiceName\"}"

// Query a service.
aliyun fc-open GET /2021-04-06/services/testServiceName

// Update the description of a service.
aliyun fc-open PUT /2021-04-06/services/testServiceName --body "{\"description\":\"update service\"}"

// Query a list of services.
aliyun fc-open GET /2021-04-06/services

// Delete a service.
aliyun fc-open DELETE /2021-04-06/services/testServiceName

関数を管理するためのサンプルコマンド

// serviceName: testServiceName
// functionName: testFunctionName

// Create a function. You must compress the code package into a .zip package, upload the .zip package to an OSS bucket, and then specify the OSS bucket and object in the --body parameter.
aliyun fc-open POST /2021-04-06/services/testServiceName/functions --body "{\"functionName\":\"testFunctionName\",\"runtime\":\"python3\",\"handler\":\"index.handler\",\"code\":{\"ossBucketName\":\"testBucket\",\"ossObjectName\":\"code.zip\"}}"

// Query a function.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName

// Obtain the code package of a function.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName/code

// Update a function.
aliyun fc-open PUT /2021-04-06/services/testServiceName/functions/testFunctionName --body "{\"description\":\"update function\"}"

// Synchronously invoke a function.
aliyun fc-open POST /2021-04-06/services/testServiceName/functions/testFunctionName/invocations

// Asynchronously invoke a function.
aliyun fc-open POST /2021-04-06/services/testServiceName/functions/testFunctionName/invocations --header x-fc-invocation-type=Async

// Query a list of functions.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions

// Delete a function.
aliyun fc-open DELETE /2021-04-06/services/testServiceName/functions/testFunctionName

バージョンとエイリアスを管理するためのサンプルコマンド

// serviceName: testServiceName
// aliasName: testAliasName

// Publish a version.
aliyun fc-open POST /2021-04-06/services/testServiceName/versions --body "{\"description\":\"publish new version\"}"

// Query a list of versions.
aliyun fc-open GET /2021-04-06/services/testServiceName/versions

// Delete a version.
aliyun fc-open DELETE /2021-04-06/services/testServiceName/versions/1

//Create an alias.
aliyun fc-open POST /2021-04-06/services/testServiceName/aliases --body "{\"aliasName\":\"testAliasName\",\"versionId\":\"1\"}"

// Query an alias.
aliyun fc-open GET /2021-04-06/services/testServiceName/aliases/testAliasName

// Query a list of aliases.
aliyun fc-open GET /2021-04-06/services/testServiceName/aliases

// Update an alias.
aliyun fc-open PUT /2021-04-06/services/testServiceName/aliases/testAliasName --body "{\"description\":\"update alias\"}"

// Delete an alias.
aliyun fc-open DELETE /2021-04-06/services/testServiceName/aliases/testAliasName

プロビジョニングされたインスタンスのサンプルコマンド

// serviceName: testServiceName
// functionName: testFunctionName
// aliasName: testAliasName

// Configure provisioned instances for a function.
aliyun fc-open PUT /2021-04-06/services/testServiceName/functions/testFunctionName/provision-config\?qualifier=testAliasName --body "{\"target\":1}"

// Query provisioned instance configurations for a function.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName/provision-config\?qualifier=testAliasName

// Query a list of provisioned instances.
aliyun fc-open GET /2021-04-06/provision-configs

オンデマンドインスタンスのサンプルコマンド

// serviceName: testServiceName
// functionName: testFunctionName
// aliasName: testAliasName

// Configure on-demand instances for a function.
aliyun fc-open PUT /2021-04-06/services/testServiceName/functions/testFunctionName/on-demand-config\?qualifier=testAliasName --body "{\"maximumInstanceCount\":10}"

// Query on-demand instances for a function.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName/on-demand-config\?qualifier=testAliasName

// Query a list of on-demand instances.
aliyun fc-open GET /2021-04-06/on-demand-configs

// Delete on-demand configurations for a function.
aliyun fc-open DELETE /2021-04-06/services/testServiceName/functions/testFunctionName/on-demand-config\?qualifier=testAliasName

トリガーを管理するためのサンプルコマンド

// serviceName: testServiceName
// functionName: testFunctionName
// triggerName: testTriggerName

// Create a trigger. In the example, a time trigger is created.
aliyun fc-open POST /2021-04-06/services/testServiceName/functions/testFunctionName/triggers   --body "{\"triggerConfig\": \"{\\\"cronExpression\\\":\\\"@every 1m\\\"}\", \"triggerName\": \"TestTriggerName\", \"triggerType\": \"timer\" }"

// Query a trigger.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName/triggers/testTriggerName

// Update a trigger.
aliyun fc-open PUT /2021-04-06/services/testServiceName/functions/testFunctionName/triggers/testTriggerName   --body "{\"triggerConfig\": \"{\\\"cronExpression\\\":\\\"@every 1m\\\"}\", \"triggerName\": \"TestTriggerName\", \"triggerType\": \"timer\" }"

// Query a list of triggers.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName/triggers

// Delete a trigger.
aliyun fc-open DELETE /2021-04-06/services/testServiceName/functions/testFunctionName/triggers/testTriggerName

非同期呼び出し構成のサンプルコマンド

// serviceName: testServiceName
// functionName: testFunctionName

// Configure asynchronous invocation for a function.
aliyun fc-open PUT /2021-04-06/services/testServiceName/functions/testFunctionName/async-invoke-config --body "{\"maxAsyncRetryAttempts\":2}"

// Query asynchronous invocation configurations of a function.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName/async-invoke-configs

// Query an asynchronous invocation configuration of a function.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName/async-invoke-config

// Delete an asynchronous invocation configuration.
aliyun fc-open DELETE /2021-04-06/services/testServiceName/functions/testFunctionName/async-invoke-config

// Query a specified asynchronous task.
aliyun fc-open  GET /2021-04-06/services/testServiceName/functions/testFunctionName/stateful-async-invocations/invokeID

// Query a list of asynchronous tasks.
aliyun fc-open  GET /2021-04-06/services/testServiceName/functions/testFunctionName/stateful-async-invocations

関連ドキュメント

コマンドパラメーターの詳細については、「APIリファレンス」をご参照ください。

関連ドキュメント

Alibaba Cloud CLIの詳細については、Alibaba Cloud CLIとは