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

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 Funcraft command-line tool and 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 three programming languages: Node.js, Python, and PHP.
  • Pull services and functions from Function Compute and execute functions in Function Compute.
  • Deploy services and functions to Function Compute and update related configurations.

Step 1: Bind an Alibaba Cloud account to the extension

  1. Open VS Code, and click icon_Aliyun_Serverless_VSCode_Extension in the left-side navigation pane. 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 under the 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 in different regions.vscode_switch_region

Step 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 locally initialize a Function Compute project.
    vscode_create_function
  3. Specify the service name, function name, function runtime, and function type in sequence.
    After you specify these parameters, Aliyun Serverless VSCode Extension automatically creates a function and displays the created local service and function in the LOCAL RESOURCES pane.vscode_create_function_result
You can also click the FC: Create Function icon next to a service in the LOCAL RESOURCES panel to create functions for this service. You must specify the function name, function runtime, and function type in sequence.vscode_create_function_underservice

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 click the debugging link in the Handler file to locally debug the function.
    vscode_local_debug
  2. Insert a breakpoint into the code and start debugging. The debugging information will appear.
    vscode_local_debug_result
    Aliyun Serverless VSCode Extension creates an event.dat file in the directory that stores the function handler file. You can modify the event.dat file to configure events that trigger the function when 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 click the execution link in the Handler file to locally call the function.vscode_local_invoke
The logs and execution results of the function are output to the TERMINAL tab.vscode_local_invoke_result
Aliyun Serverless VSCode Extension creates an event.dat file in the directory that stores the function handler file. You can modify the event.dat file to configure events that trigger the function when the function is called.vscode_local_invoke_event

Step 5: Deploy the service and its function

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

Step 6: Execute the function in Function Compute

Click the Invoke icon on the right of the function name in the REMOTE RESOURCES pane to execute the function in Function Compute.vscode_remote_invoke
The logs and execution results of the function are output to 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 trigger the function when the function is called in Function Compute.

Step 7: Test the function in Function Compute

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 top navigation bar, select a region.
  3. In the left-side navigation pane, click Service/Function.
  4. In the Function Compute console, find and click the demo service. On the Functions tab, click the testFunc01 function.
  5. 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 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.