This topic describes how to use the fun local start command to run different functions.
For more information about how to migrate Function Compute-related resources from Funcraft to Serverless Devs, see Migrate resources from Funcraft to Serverless Devs.
For more information about Serverless Devs, see What is Serverless Devs?.
For more information about how to use Serverless Devs to debug functions, see Run the s local command to debug functions and Cloud-terminal joint debugging.
We apologize for any inconvenience caused.
Syntax
option
can be omitted. fun local start [option]
Run an HTTP trigger function in a browser
fun local start
The following figure shows the expected output. After you fire the HTTP trigger, you can perform operations to debug the function. For more information, see Debug a local HTTP trigger function by using VSCode.
Run an event function by using an API operation
After you start and run the function by using an API operation, you can use the Python SDK to debug the function. For more information about how to use an API operation to start and run functions, see Run an event function by using an API operation.
api localdemo/php72 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/php72/invocations/
api localdemo/python27 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/python27/invocations/
api localdemo/python3 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/python3/invocations/
api localdemo/nodejs6 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/nodejs6/invocations/
api localdemo/nodejs8 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/nodejs8/invocations/
api localdemo/java8 was registered
url: http://localhost:8000/2016-08-15/services/localdemo/functions/java8/invocations/
function compute app listening on port 8000!
Debug event functions by using an SDK
This section describes how to use an SDK to debug an event function. The Python SDK is used as an example.
- Install dependencies based on your business requirements.
- Run the following command to install dependencies:
pip install aliyun-fc2
- Log on to GitHub to download dependencies. For more information, visit the Python official website.
- Run the following command to install dependencies:
- Create a .py file and write code. In this example, the file invoke.py is created.
import fc2 import os # 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 in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources under 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 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 = fc2.Client(endpoint='http://localhost:8000', accessKeyID, accessKeySecret) resp = client.invoke_function('localdemo', 'php72') print resp.headers print resp.data
Note The Alibaba Cloud account and AccessKey secret configured in the SDK code must be the same as those configured in Funcraft. Otherwise, signature verification fails when you call the SDK. - Run the following command to execute the code file and debug the function:
Expected output:python invoke.py