This topic describes the command format and procedure for running different functions by using fun local start related commands.

Notice The content described in this article will no longer be maintained later. If your Function service resources are managed by Funcraft, we recommend that you migrate the resources to Serverless Devs.

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 Overview.

For more information about how to use Serverless Devs to debug functions, see Debug Functions and Remote debug Using s Local Related Commands.

We apologize for any inconvenience caused.

Command syntax

The command format for running the local HTTP function is as follows. option can be omitted.
fun local start [option]

Run an HTTP trigger function through a browser

After you run the following command, Funcraft starts an HTTP server to provide HTTP services: Then, scan the template.yml file for all functions configured with HTTP triggers and register the functions with HTTP Server.
fun local start
Expected output: http_trigger
Note You can view information about all HTTP triggers in the expected output. Open any URL in a browser to trigger the HTTP trigger function.
The following is the operation flow of an access Node.js HTTP trigger, which is consistent with the running environment of other language type. fun_local_start_eg

After the HTTP trigger function is successfully run, you can perform related operations to debug the function. For more information, see Debug a local HTTP trigger function by using VSCode.

Run event functions through APIs

After you start running the function by using the API, you can debug the function by using the Python SDK. For more information about how to start and run functions by using the API, see Run event functions through APIs.

After the fun local start command is executed, Funcraft starts an HTTP server to provide HTTP services, scans all functions in the template.yml file, and registers all functions without HTTP triggers with the HTTP server. After the registration is successful, you can call it through the InvokeFunction or SDK. For more information, see InvokeFunction and Supported SDKs.
Note You must sign all API requests to ensure security. Alibaba Cloud uses the request signature to verify the identity of the API caller. For more information, see Configure signature authentication. If you do not want to manually calculate signature authentication, you can use the SDK for authentication. The SDK has implemented the signature algorithm and signed the API. For more information, see Supported SDKs.
The execution fun local start runs the service. The execution log is as follows.
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 the API

The following section uses Python as an example to describe how to debug event functions by using the Python SDK.

  1. Select the following operations as needed to install dependencies.
    • Run the following command to install dependencies:
      pip install aliyun-fc2
    • Log on to GitHub to download dependencies. For more information, see Python.
  2. Create a. py file and then write the code. This example uses the file invoke.py as an example.
    import fc2
    
    client = fc2.Client(endpoint='http://localhost:8000', accessKeyID='<your access key id>', accessKeySecret='your access key secret')
    
    resp = client.invoke_function('localdemo', 'php72')
    
    print resp.headers
    print resp.data
    Note The Alibaba Cloud account (primary account) and AccessKey Secret configured in the SDK code must be the same as those configured for Funcraft. Otherwise, signature authentication will fail during the call.
  3. Run the following command to execute the code file and debug the function:
    python invoke.py
    Sample command output:apivscodefc
Note Hot loading is supported when running and debugging HTTP trigger functions locally. After the local service is started through the fun local start, even if the code is modified, there is no need to restart the local service. You can directly refresh the web page or re-trigger the function to run the debugging changed function.