All Products
Search
Document Center

Function Compute (2.0):CLI reference (2021-04-16)

Last Updated:Dec 21, 2023

Alibaba Cloud CLI is a tool based on Alibaba Cloud OpenAPI. You can use Alibaba Cloud CLI to call APIs and manage services. This topic describes how to use Alibaba Cloud CLI to call Function Compute API operations and provides examples.

Prerequisites

  • You have installed Alibaba Cloud CLI. For more information, see Installation Guide.

  • You have configured Alibaba Cloud CLI. For more information, see Configure Alibaba Cloud CLI.

  • You have learned the command structure of using Alibaba Cloud CLI to call a RESTful API, which is the architecture style of the Function Compute API. For more information, see Call RPC API and RESTful API.

Getting started

You can perform the following steps to complete the full process from creating a service to invoking a function by using Alibaba Cloud CLI:

  1. Create a service named testServiceName.

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

    Sample response:

    {
            "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. Create a function.

    Note
    • You cannot directly upload a code package by using Alibaba Cloud CLI. You can compress the code package into a .zip package in an on-premises environment and upload the .zip package to an Object Storage Service (OSS) bucket. Then, you can specify to use the OSS bucket and object when you create a function in Alibaba Cloud CLI.

    • By default, OSS resources created by using Alibaba Cloud CLI reside in the same region as Function Compute resources.

    1. Create an OSS bucket and set the access control list (ACL) for the bucket to private.

      aliyun oss mb oss://test-bucket-name-1 --acl private
    2. Package the code that you want to deploy to Function Compute as a .zip package and upload the package to an OSS bucket.

      aliyun oss cp code.zip oss://test-bucket-name-1/
    3. In the testServiceName service, create a function named 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\"}}"

      Sample response:

      {
              "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

    Sample response:

    hello world
Note

You can configure the --body parameter by using one of the following methods:

  • Perform operations in the Get started section and write the content to --body after escaping.

  • Use a JSON file that contains the content that you want to specify.

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

    Example:

    {
        "serviceName": "testServiceName"
    }

Sample commands for managing services

// 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

Sample commands for managing functions

// 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

Sample commands for managing versions and aliases

// 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

Sample commands for provisioned instances

// 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

Sample commands for on-demand instances

// 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

Sample commands for managing triggers

// 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

Sample commands for asynchronous invocation configurations

// 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

References

For more information about the command parameters, see API reference.

References

For more information about Alibaba Cloud CLI, see What is Alibaba Cloud CLI?