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

Prerequisites

Background information

Provided by Function Compute, Aliyun Serverless VSCode Extension is a VS Code-based development, debugging, and deployment tool that integrates the features of the command-line tool Funcraft and the Function Compute SDK. You can use this extension to perform the following operations:

  • Quickly initialize projects and create functions locally.
  • Run and debug local functions. The debugging feature is supported in programming languages Node.js, Python, and PHP.
  • Pull services and functions from the cloud and run functions in the cloud.
  • Deploy services and functions to the cloud and update related configurations.

Step 1: Bind an Alibaba Cloud account to the VSCode extension

  1. Right-click icon_Aliyun_Serverless_VSCode_Extension in the left-side navigation pane, and select Bind New Account to bind an Alibaba Cloud account to the VSCode extension.
    bind_aliyunaccount
  2. Sequentially enter Account ID, AccessKey ID, AccessKey Secret, and Account Alias (the local account name) of the Alibaba Cloud account.
    input_information
    After the binding is completed, you can view the cloud-side services and functions of the Alibaba Cloud account in the REMOTE RESOURCES pane.function_list
    You can switch the region in the REMOTE RESOURCES pane to view the services and functions in different regions.switch_region

Step 2: Create a function

  1. Open an empty directory file in the VSCode extension.
  2. Click the plus sign (+) in the LOCAL RESOURCES pane to locally initialize a Function Compute project.
    create_function
  3. Sequentially enter or select the service name, function name, function runtime, and function type as instructed.
    input_information
    After you specify these parameters, the extension automatically creates a function and displays the created local service and function in the LOCAL RESOURCES pane.
Alternatively, you can click Create Function icon on the right of a service in the LOCAL RESOURCES pane to create a function for the service. Sequentially enter or select the function name, function runtime, and function type as instructed.create_function_02

Step 3: Locally debug the function

Notice
  • If you want to debug functions that run in Python 2.7 or Python 3 runtimes, you must first install the Python extension.
  • If you want to debug functions that run in PHP runtimes, you must first install the PHP Debug extension.
  1. In the LOCAL RESOURCES pane, click the Debug icon on the right of the function name or the debugging link in the Handler file to locally debug the function.
    debug
  2. Insert a breakpoint into the code and start debugging. The debugging information will be displayed.
    debug_02
    The extension creates the event.dat file under the directory that stores the function handler file. You can modify the event.dat file to configure events to be triggered each time the function is debugged.

Step 4: Locally call the function

In the LOCAL RESOURCES pane, click the Invoke icon on the right of the function name or the execution link in the Handler file to locally call the function.execute_funtion
The logs and execution results of the function will be output to the TERMINAL tab.output
The extension creates the event.dat file under the directory that stores the function handler file. You can modify the event.dat file to configure events to be triggered each time the function is called.modify_event

Step 5: Deploy the service and its functions

  1. Click the Deploy icon in the LOCAL RESOURCES pane to deploy the local service and its functions to the cloud.
    deploy
  2. After the deployment is completed, click the Refresh icon in the REMOTE RESOURCES pane to query the services and functions that are deployed to the cloud.
    refresh

Step 6: Execute the function in the cloud

Click the Invoke icon on the right of the function name in the REMOTE RESOURCES pane to run the function in the cloud.execute_function_02
The logs and execution results of the function will be output to the TERMINAL tab.output_02

The extension creates the event.dat file under the root directory of your project. You can modify the event.dat file to configure events to be triggered each time the function is called in the cloud.

Step 7: Test the function in the cloud

You can log on to the Function Compute console to check whether the preceding deployment is successful.

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Service/Function.
  3. In the Function Compute Console, find and click the demo service. On the Functions tab, click the testFunc01 function.
  4. On the page that appears, click the Code tab. On the Code tab, click Invoke to execute the function in the Function Compute console.

Step 8: View execution logs of the function

You can view the execution log on the current page after each execution. To view historical execution logs, click the Log tab. You must configure a Logstore for the function first. For more information, see Logs.