Aliyun Serverless VSCode Extension

Updated at:

The Aliyun Serverless VSCode Extension integrates Function Compute into your editor, so you can develop, debug, and manage resources through a graphical interface. This guide shows you how to create your first function and explore the key features of the extension.

Prerequisites

To use all features of the Aliyun Serverless VSCode Extension, ensure you have the following installed:

Background

The Aliyun Serverless VSCode Extension is a VSCode extension provided by Function Compute. It integrates the capabilities of the Function Compute command-line tool (Fun) and the Function Compute SDK. It provides a complete development, debugging, and deployment workflow directly within your editor. With this extension, you can:

  • Quickly initialize local projects and create functions.

  • Run and debug local functions, and deploy services and functions to the cloud.

  • View a list of your remote services and functions, inspect their configurations, and invoke remote functions.

  • Get syntax assistance for template files, including auto-completion, schema validation, and hover tooltips.

Install the extension

  1. Open VSCode and go to the Extensions view.

  2. Search for Aliyun Serverless, view the details, and click Install.

  3. Restart VSCode. The Aliyun Serverless VSCode Extension icon appears in the left sidebar.

Quickstart

  1. Bind an Alibaba Cloud account.

    1. In the left sidebar, click the Aliyun Serverless VSCode Extension iconicon_Aliyun_Serverless_VSCode_Extension, and then click Bind New Account.

      The Bind New Account button is located under the REMOTE RESOURCES section in the sidebar.

    2. Enter your Alibaba Cloud Account ID, AccessKey ID, AccessKey Secret, and an account alias (local name) in sequence.

      A series of input prompts appear at the top of the VSCode window. Enter the required information at each step and press Enter to confirm, or press Escape to cancel.

      After you bind the account, the extension lists the services and functions from your Alibaba Cloud account.

      You can also click the more options icon in the upper-right corner of the REMOTE RESOURCES panel and select FC: Switch Region from the drop-down menu to view services and functions in a different region.

  2. Create a function.

    1. Open an empty directory in VSCode. In the LOCAL RESOURCES panel, click the Create Function icon to initialize a local Function Compute project.

      The Create Function icon is the + button on the right side of the LOCAL RESOURCES panel title bar.

    2. Follow the prompts to enter or select a service name, function name, runtime, and function type. After you complete the steps, the extension automatically creates the function and lists the new local service and function in the LOCAL RESOURCES panel.

      After creation, the LOCAL RESOURCES panel on the left displays the new service and function. The editor automatically opens the handler file, and action links for Local Run, Local Debug, and Invoke Panel appear above the code.

      You can also click the Create Function icon next to a service name in the LOCAL RESOURCES panel to create a new function for that service. Follow the prompts to enter or select the function name, runtime, and function type.

  3. Deploy a service and function.

    1. Click the Deploy icon in the LOCAL RESOURCES panel to deploy the local service and function to the cloud.

      The Deploy icon is the cloud upload button (an up arrow) in the title bar of the LOCAL RESOURCES panel.

      After the deployment is complete, click the Refresh icon in the REMOTE RESOURCES panel to view the deployed service and function. The TERMINAL panel displays the deployment progress logs. When the deployment succeeds, you will see messages like function testFunc01 deploy success and service demo01 deploy success.

Key features

  • Run functions locally

    In the LOCAL RESOURCES panel, click the run icon next to the function name or click the run link in the file that contains your handler to run the function locally.

    The function logs and results appear in the TERMINAL.

    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 MB

    The extension creates an event.dat file in the same directory as your handler file. You can edit this JSON-formatted file (for example, {"key": "value", "name": "Function Compute"}) to configure the event payload sent with each function invocation. The updated event data is reflected in the TERMINAL output on the next invocation.

  • Debug functions locally

    Important
    • To debug a function that uses the Python 2.7 or Python 3 runtime, you must first install the Python extension.

    • To debug a function that uses the PHP runtime, you must first install the PHP Debug extension.

    In the LOCAL RESOURCES panel, click the debug icon next to the function name or click the debug link in the file that contains your handler to debug the function locally.

    Set a breakpoint in your code file and start debugging. The VARIABLES panel on the left displays the function context, such as the requestId, credentials, and function objects. The code execution pauses at the breakpoint, and the current line is highlighted.

    The extension creates an event.dat file in the same directory as your handler file. You can edit this file to configure the event payload for each debug session.

  • Invoke remote functions

    Click the run icon next to a function in the REMOTE RESOURCES panel to invoke the remote function.

    The function logs and results appear in the TERMINAL.

    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 MB

    The extension creates an event.dat file in the root directory of your project. You can edit this file to configure the event payload sent with each remote invocation.

  • Navigate to the template file definition

    The Function Compute Fun tool uses a YAML-formatted template file to describe serverless applications. When you create a function with the Aliyun Serverless VSCode Extension, it automatically populates this file with default values. To modify the configuration of a local service or function, click its name in the LOCAL RESOURCES panel. VSCode navigates to its definition in the template file, where the corresponding block of code is briefly highlighted.

  • Template file assistance

    • Auto-completion

      Provides auto-completion for all resource configuration properties within the template.yml file. The auto-completion suggestions are context-aware and based on the current indentation level.

    • Schema validation

      Validation is supported for all resource configurations in the template.yml file. The resource configurations in template.yml are checked for compliance with the specifications.

    • Hover tooltips

      Offers contextual help for all resource configurations in the template.yml file. When you hover over a resource key in template.yml, a tooltip appears that describes the configurable fields for that key, including the field name, type, and a link to the documentation.

Feedback

If you encounter any issues, you can contact us or provide feedback on GitHub.