This topic describes how to create a function by using Aliyun Serverless VSCode Extension.
- You have downloaded the Visual Studio Code (VS Code) component from the official website of Visual Studio Code and installed the component.
- You have installed Docker.
Operations such as code compilation, dependency installation, and local running and debugging are performed in a Docker image.
- You have installed Aliyun Serverless VSCode Extension.
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
- Open VS Code, and click in the left-side navigation pane. Then click Bind New Account to bind an Alibaba Cloud account.
- Enter the ID, AccessKey ID, AccessKey Secret, and custom alias of the Alibaba Cloud
account in sequence.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.You can switch the region in the REMOTE RESOURCES pane to view the services and functions in different regions.
Step 2: Create a function
- Open an empty directory file in Aliyun Serverless VSCode Extension.
- Click the plus sign (+) in the LOCAL RESOURCES pane and locally initialize a Function
- 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.
Step 3: Locally debug the function
- 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.
- In the LOCAL RESOURCES pane, click the Debug icon on the right of the function name
or click the debugging link in the
Handlerfile to locally debug the function.
- Insert a breakpoint into the code and start debugging. The debugging information will
appear.Aliyun Serverless VSCode Extension creates an
event.datfile 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
Handlerfile to locally call the function.
event.datfile 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.
Step 5: Deploy the service and its function
- Click the Deploy icon in the LOCAL RESOURCES pane to deploy the local service and
its function to the cloud.
- 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.
Step 6: Execute the function in Function Compute
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
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.
- Log on to the Function Compute console.
- In the top navigation bar, select a region.
- In the left-side navigation pane, click Service/Function.
- In the Function Compute console, find and click the demo service. On the Functions tab, click the testFunc01 function.
- 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.