This topic describes how to use Aliyun Serverless VSCode Extension to create, deploy, and debug a function.

Background information

Function Compute provides you with a Visual Studio Code (VS Code) plug-in named Aliyun Serverless VSCode Extension. You can use Aliyun Serverless VSCode Extension to perform the following operations:
  • Initialize projects and create functions on your computer.
  • Execute and debug local functions that are compiled in Node.js, Python, and PHP languages.
  • Pull services and functions from Function Compute to VS Code.
  • Deploy services to Function Compute and update configurations.

Prerequisites

Make sure that the following operations are completed:

Procedure

  1. Bind your Alibaba Cloud account.
    1. Open VS Code. In the left-side navigation pane, click the icon_Aliyun_Serverless_VSCode_Extension icon. Then, click Bind New Account to bind an Alibaba Cloud account.
      vscode_bind_account
    2. Enter the Alibaba Cloud account ID, AccessKey ID, AccessKey secret, and custom alias of the Alibaba Cloud account in sequence.
      input_information
      After the account is bound, you can view the services and functions in Function Compute that belong to your Alibaba Cloud account in the REMOTE RESOURCES section. vscode_bind_account_result
      You can change the region in the REMOTE RESOURCES section to view the services and functions that are deployed in other regions. vscode_switch_region
  2. Create a function.
    1. Open an empty folder in Aliyun Serverless VSCode Extension.
    2. In the LOCAL RESOURCES section, click the plus icon to initialize a Function Compute project on your computer.
      vscode_create_function
    3. Enter or select the service name, function name, function runtime environment, and function type in sequence as instructed.
      After you configure the parameters, Aliyun Serverless VSCode Extension creates a function and displays the created service and function in the LOCAL RESOURCES section. vscode_create_function_result
    You can also click the plus icon next to a service in the LOCAL RESOURCES list to create a function for the service. Enter or select the function name, function runtime environment, and function type in sequence as instructed. vscode_create_function_underservice
  3. Debug the function on your computer.
    Notice
    • Before you debug a function in Python 2.7 or Python 3, you must install the Python plug-in.
    • Before you debug a function in PHP 7.2, you must install the PHP Debug plug-in.
    1. In the LOCAL RESOURCES section, click the debugging icon next to the name of the desired function or Local Debug in the handler file to debug the function on your computer.
      vscode_local_debug
    2. Insert a breakpoint into the file and start debugging. Then, you can view the debugging information.
      vscode_local_debug_result
    Aliyun Serverless VSCode Extension creates the event.dat file in the directory in which the function handler file is stored. You can modify the event.dat file to configure events that are triggered when you debug the function.
  4. Optional: Invoke the function on your computer.
    In the LOCAL RESOURCES section, click the execution icon next to the name of the desired function or Local Run in the handler file to invoke the function on your computer. vscode_local_invokeThe logs and invocation results of the function are displayed on the TERMINAL tab. vscode_local_invoke_result
    Aliyun Serverless VSCode Extension creates the event.dat file in the directory in which the function handler file is stored. You can modify the event.dat file to configure events that are triggered when you debug the function. vscode_local_invoke_event
  5. Deploy the service and the function.
    1. Click the deployment icon in the LOCAL RESOURCES section to deploy the service and the function from your computer to Function Compute.
      deploy
    2. After the deployment is complete, click the refresh icon in the REMOTE RESOURCES section to query the service and the function that are deployed to Function Compute.
      vscode_deploy_result
  6. Invoke the function in Function Compute.
    Click the execution icon next to the function name in the REMOTE RESOURCES section to invoke the function in Function Compute. vscode_remote_invoke
    The logs and invocation results of the function are displayed on the TERMINAL tab. vscode_local_invoke_result

    Aliyun Serverless VSCode Extension creates the event.dat file in the root directory of your project. You can modify the event.dat file to configure events that are triggered when you invoke the function in Function Compute.

  7. Verify the result in Function Compute.
    You can log on to the Function Compute console to check whether the service and the function are deployed.
    1. Log on to the Function Compute console.
    2. In the left-side navigation pane, click Services and Functions.
    3. In the top navigation bar, select the region where the service resides.
    4. On the Services page, click the target service.
    5. On the Functions page, click the name of the function that you want to manage.
    6. On the Code tab of the function details page, click Test Function to invoke the function in the Function Compute console.
  8. View logs.
    After you invoke the function, you can view the invocation logs on the Code tab. To view historical invocation logs, you must first configure a Logstore for the function. Then, you can view historical invocation logs on the Logs tab. For more information, see Configure logging.