This topic describes how to use the fun local start command to run different functions.

Important This topic is no longer maintained. If you use Funcraft to manage your Function Compute resources, we recommend that you migrate your resources to Serverless Devs and use Serverless Devs to manage your resources.

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

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

Run an HTTP trigger function in a browser

After you run the following command, Funcraft starts an HTTP server to provide HTTP services: After that, Funcraft scans all functions that are configured with HTTP triggers in the template.yml file and register the functions on the HTTP server.
fun local start
The following figure shows the expected output. http_trigger
Note You can view information about all HTTP triggers in the output. Open a URL in a browser to fire the HTTP trigger.
The following figure shows the procedure to access a Node.js HTTP trigger. The procedures used in other runtimes are similar. fun_local_start_eg

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.

After you run the fun local start command, Funcraft starts an HTTP server to provide HTTP services, scans all functions in the template.yml file, and registers all functions that are not configured with HTTP triggers on the HTTP server. After the registration, you can call the InvokeFunction operation or use SDKs to invoke the functions. For more information, see InvokeFunction and SDKs.
Note Alibaba Cloud uses the request signature to verify the identity of the API caller. This ensures data security when you call API operations. For more information, see Signature authentication. If you do not want to manually perform the signature verification, you can use the SDK to verify the signature. A signature algorithm is implemented in the SDK to sign API operations. For more information, see SDKs.
Run the fun local start command to run the service. The following code shows the logs.
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.

  1. 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.
  2. 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.
  3. Run the following command to execute the code file and debug the function:
    python invoke.py
    Expected output:apivscodefc
Note Hot loading is supported when you run and debug an local HTTP trigger functions. After you start a local service by using the fun local start command, you do not need to restart the local service even if you modify the code. You can directly refresh the web page or re-trigger the function to run and debug the changed function.