This topic shows you how to create a function with the extension and highlights its key features.
Usage notes
The VS Code extension described in this topic is based on Funcraft, which is no longer maintained. If you use Funcraft to manage your Function Compute resources, we recommend that you migrate your resources to Serverless Devs.
For more information about how to migrate Function Compute resources from Funcraft to Serverless Devs, see Migrate from Funcraft to Serverless Devs.
For more information about Serverless Devs, see What is Serverless Devs?.
We apologize for any inconvenience this may cause.
Prerequisites
To use all the features of the Aliyun Serverless VSCode Extension, install the following components:
VS Code: You can download and install it from the official Visual Studio Code website.
Docker: Follow the tutorial at aliyun/fun to install and configure Docker.
Background information
Aliyun Serverless VSCode Extension is a VS Code extension provided by Function Compute. It combines the capabilities of the Fun command-line tool and Function Compute SDKs to provide an integrated development, debugging, and deployment experience within VS Code. This extension allows you to:
Quickly initialize projects and create functions locally.
Run, debug, and deploy local services and functions to the cloud.
Pull your remote services and functions, view their configurations, and invoke them.
Get syntax assistance for template files, including autocompletion, schema validation, and hover tips.
Install the extension
Open VS Code and go to the Extensions view.
Search for
Aliyun Serverless, select it, and click Install.Restart VS Code. The Aliyun Serverless VSCode Extension icon appears in the Activity Bar on the left.
Quick start
Bind your Alibaba Cloud account.
In the Activity Bar, click the Aliyun Serverless VSCode Extension
icon, and then click Bind New Account.Enter your Alibaba Cloud account ID, AccessKey ID, AccessKey Secret, and a custom alias for the account.
Find your account ID on the Account Management page and get your AccessKey ID and AccessKey Secret from the User Management console.
NoteThe extension supports signing in as a RAM user. When signing in as a RAM user, use your root account ID with the RAM user's AccessKey ID and AccessKey Secret.
After you bind the account, your remote services and functions appear in the REMOTE RESOURCES section.
In the REMOTE RESOURCES section, click the More icon in the upper-right corner and select Switch Region to view services and functions in other regions.
Create a function.
In VS Code, open an empty folder for your project. In the LOCAL RESOURCES section, click the plus (+) icon to initialize a new Function Compute project.
Follow the prompts to enter or select the service name, function name, function runtime, and function type. The extension then automatically creates the function and displays the new local service and function in the LOCAL RESOURCES area.
Alternatively, you can click the plus (+) icon next to an existing service name in the LOCAL RESOURCES section to add a new function to that service. Then, follow the prompts to enter a function name and select a runtime and a function type.
Deploy the service and function.
In the LOCAL RESOURCES section, click the deploy icon next to the service to deploy it and its functions to the cloud.
After the deployment is complete, click the refresh icon in the REMOTE RESOURCES area to view the services and functions deployed to the cloud. The TERMINAL tab displays the deployment progress and a success message.
Other features
Invoke a function locally
In the LOCAL RESOURCES section, click the invoke icon next to the function name, or click Local Run in the handler file.
The function's logs and results are output in the TERMINAL tab.
FC Invoke Start RequestId: xxx load code for handler:index.handler 2019-07-18T12:25:50.146Z xxx [verbose] [String: '{"key": "value"}'] FC Invoke End RequestId: xxx hello world RequestId: xxx Billed Duration: 115 ms Memory Size: 1998 MB Max Memory Used: 58 MBThe extension creates an event.dat file in the same directory as the function's entry file. You can modify this file to set the event information that triggers the function on each invocation. The default content is in JSON format, for example,
{"key": "value"}.Debug a function locally
ImportantTo debug Python 2.7 or Python 3 functions, first install the Python extension.
To debug PHP 7.2 functions, first install the PHP Debug extension.
In the LOCAL RESOURCES section, click the debug icon next to the function name, or click Local Debug in the handler file.
Set a breakpoint in your code file. After you start debugging, you can inspect the debugging information.
The plugin creates the event.dat file in the same directory as the function entry file. You can modify this file to set the event information that triggers the function during each debugging session.
Invoke a remote function
In the REMOTE RESOURCES section, click the invoke icon next to a function to invoke it.
The function's logs and results are displayed in the TERMINAL tab.
FC Invoke Start RequestId: xxx load code for handler:index.handler 2019-07-18T12:25:50.146Z xxx [verbose] [String: '{"key": "value"}'] FC Invoke End RequestId: xxx hello world RequestId: xxx Billed Duration: 115 ms Memory Size: 1998 MB Max Memory Used: 58 MBThe extension creates an event.dat file in your project's root directory. Modify this file to configure the event payload for each invocation of the remote function.
Navigate to definitions in the template file
Funcraft uses a template file in the YAML format to describe Serverless applications. When you create a function by using the Aliyun Serverless VSCode Extension, the template file is automatically populated with default values. If you want to modify the configuration of a local service or function, you can click the name of the service or function in the LOCAL RESOURCES section to go to its corresponding definition in the template file. The definition block for the selected resource is highlighted, and the highlight then gradually fades.
Template file assistance
Autocompletion
The extension supports autocompletion for all resource configuration properties in the template.yml file. It provides precise suggestions based on the indentation level.
Schema validation
You can validate all resource configuration information in the template file template.yml. The system checks whether the resource configuration information in template.yml complies with the specifications.
Hover tips
Context-sensitive help is provided for all resource configurations in the template.yml template file. In template.yml, when you hover the pointer over the key name of a resource, a tooltip appears and displays information about the configurable fields under that key, such as the field name and field type.
Feedback
If you encounter any issues, contact us or report an issue on GitHub.