You can call Function Compute API operations by running commands in Alibaba Cloud CLI. This topic describes how to use Alibaba Cloud CLI to call Function Compute APIs 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 are familiar with the command syntax for calling RESTful API operations in Alibaba Cloud CLI. Function Compute provides RESTful API operations. 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:
Create a service named testServiceName.
aliyun fc-open POST /2021-04-06/services --body "{\"serviceName\":\"testServiceName\",\"description\":\"testServiceName\"}"
The following sample response is returned:
{ "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": "" } }
Create a function.
NoteYou 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 the OSS bucket and object to use when you create the function in Alibaba Cloud CLI.
By default, the OSS bucket created by using Alibaba Cloud CLI resides in the same region as Function Compute resources.
Create an OSS bucket and set the access control list (ACL) of the bucket to public-read-write.
aliyun oss mb oss://test-bucket-name-1 --acl public-read-write
Compress the code package that you want to deploy to Function Compute into a .zip package in an on-premises environment. Then, upload the .zip package to the OSS bucket that you create.
aliyun oss cp code.zip oss://test-bucket-name-1/
Create a function named testFunctionName in the testServiceName service.
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\"}}"
The following sample response is returned:
{ "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 }
Invoke the function.
aliyun fc-open POST /2021-04-06/services/testServiceName/functions/testFunctionName/invocations
The following sample response is returned:
hello world
The --body parameter can be set in the following ways:
Write the content that you want to specify in an escaped format after --body, as shown in the Get started section.
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 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 services.
aliyun fc-open GET /2021-04-06/services
// Delete a service.
aliyun fc-open DELETE /2021-04-06/services/testServiceName
Sample commands for 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 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 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 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 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 the provisioned instances for a function.
aliyun fc-open GET /2021-04-06/services/testServiceName/functions/testFunctionName/provision-config\?qualifier=testAliasName
// Query 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 on-demand instances.
aliyun fc-open GET /2021-04-06/on-demand-configs
// Delete on-demand instances for a function.
aliyun fc-open DELETE /2021-04-06/services/testServiceName/functions/testFunctionName/on-demand-config\?qualifier=testAliasName
Sample commands for 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 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 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.