This topic describes how to use Aliyun Serverless VSCode Extension to create a function.

Prerequisites

Before you create a function, make sure that the following operations are complete:

Background information

Aliyun Serverless VSCode Extension is a Visual Studio Code extension that is provided by Function Compute for development, debugging, and deployment purposes. Aliyun Serverless VSCode Extension integrates the features of the command-line tool Funcraft and Function Compute SDK. You can use Aliyun Serverless VSCode Extension to perform the following operations:
  • Initialize projects and create functions on your computer.
  • Run and debug functions on your computer. The debugging feature is supported in Node.js, Python, and PHP.
  • Pull services and functions from Function Compute and invoke functions in Function Compute.
  • Deploy services and functions to Function Compute and update related configurations.

Procedure

  1. Bind your Alibaba Cloud account.
    1. Open Visual Studio 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 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 pane. vscode_bind_account_result
      You can switch the region in the REMOTE RESOURCES pane to view the services and functions that are deployed in other regions. vscode_switch_region
  2. Create a function.
    1. Open an empty directory file in Aliyun Serverless VSCode Extension.
    2. Click the plus sign (+) in the LOCAL RESOURCES pane and initialize a Function Compute project on your computer.
      vscode_create_function
    3. Enter the service name, function name, function runtime environment, and function type in sequence as instructed.
      After you specify these parameters, Aliyun Serverless VSCode Extension creates a function and displays the created service and function in the LOCAL RESOURCES pane. vscode_create_function_result
    You can also click the Create Function icon next to a service in the LOCAL RESOURCES list to create functions 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
    • If you want to debug a function that runs in the Python 2.7 or Python 3 runtime environment, you must first install the Python extension.
    • If you want to debug a function that runs in the PHP runtime environment, you must first install the PHP Debug extension.
    1. In the LOCAL RESOURCES pane, click the Debug icon next to the function name or click the debugging link in the Handler file to debug the function on your computer.
      vscode_local_debug
    2. Insert a breakpoint into the code and start debugging. Then, the debugging information appears.
      vscode_local_debug_result
    Aliyun Serverless VSCode Extension creates an event.dat file in the directory where the function handler file is stored. You can modify the event.dat file to configure events that are triggered when the function is debugged.
  4. Optional:Invoke the function on your computer.
    In the LOCAL RESOURCES pane, click the Invoke icon next to the function name or click the execution link 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 an event.dat file in the directory where the function handler file is stored. You can modify the event.dat file to configure events that are triggered when the function is invoked. vscode_local_invoke_event
  5. Deploy the service and the function.
    1. Click the Deploy icon in the LOCAL RESOURCES pane to deploy the service and the function from your computer to the cloud.
      deploy
    2. After the deployment is complete, click the Refresh icon in the REMOTE RESOURCES pane to query the service and the function that are deployed to the cloud.
      vscode_deploy_result
  6. Invoke the function in Function Compute.
    Click the Invoke icon next to the function name in the REMOTE RESOURCES pane 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 an event.dat file in the root directory of your project. You can modify the event.dat file to configure events that are triggered when the function is invoked in Function Compute.

  7. Test 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 top navigation bar, select a region.
    3. In the left-side navigation pane, click Services and Functions.
    4. Find and click the demo service. On the Functions tab, click the testFunc01 function.
    5. Click the Code tab. On the Code tab, click Invoke to invoke the function in the Function Compute console.
  8. View logs.
    After you invoke the function, you can view the invocation log 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 Log Service resources and view function execution logs.