This topic describes how to create a function in Function Compute by using Funcraft. In this topic, the demo function whose output is hello world is created.


Background information

Funcraft is an application deployment tool provided by Function Compute. The tool allows you to conveniently manage resources of Function Compute, API Gateway, and Log Service to deploy applications with efficiency. For more information, see Funcraft overview.

Try Funcraft in Cloud Shell

Step 1: Configure Funcraft

  1. Run the following command to initialize Funcraft and configure the account information:
    fun config
  2. Set the Aliyun Account ID, Aliyun Access Key ID, Aliyun Access Key Secret, and Default region name parameters in sequence as prompted.
    If you use a RAM user, set the Aliyun Account ID parameter to the ID of the corresponding Alibaba Cloud account and set the Aliyun Access Key ID and Aliyun Access Key Secret parameters to the AccessKey pair of the RAM user.
    Note You can obtain your Alibaba Cloud account ID in the Account Management console and the AccessKey ID and AccessKey secret of the RAM user that you use in the User Management console.
    After you configure the account information, Funcraft saves the configuration to the .fcli/config.yaml file in your directory.

    For more information about how to configure Funcraft, see Configure Funcraft.

Step 2: Create an initialization template

  1. Run the following command to initialize the project template:
    fun init -n demo
  2. Select a project template as prompted.
    The following types of project templates are available:
    • Templates prefixed with event-, which are used to create common event functions.
    • Templates prefixed with http-trigger, which automatically create HTTP triggers. An HTTP trigger uses request and response as input parameters to help you create a web application.

    In this example, the event-nodejs10 template is selected.

    Funcraft creates a demo folder in the directory where you run the command. The demo folder contains the index.js and template.yml files.

    • The index.js file contains the sample code of the function.
    • The template.yml file contains the information about function resources.
      • In this example, the demo service and the demo function are created.
      • For more information about the configuration items that are supported by the template.yml file, see Serverless Application Model.

Step 3: Debug the function on premises (Optional)

To debug the function on premises, you must install Docker on your on-premises device. For more information, see Install Docker (Optional). If you cannot install Docker on your on-premises device, skip this step and debug the function in the Function Compute console.

  1. Run the following command to enter the demo function:
    cd demo
  2. Run the following command on your on-premises device to debug the function:
    fun local invoke demo
    Note A relatively long time is required to debug the function for the first time because the image of the execution environment is pulled on premises.

Step 4: Deploy the function to the Function Compute console

  1. Run the following command to deploy the function to the Function Compute console:
    fun deploy
  2. During deployment, enter Y to confirm the resources that you want to create.
    After the resources are created, the service demo deploy success message appears. The message indicates that your resources are deployed.

Step 5: Test the function in the Function Compute console

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. Click the demo service. On the Functions tab, click the demo function.
  5. Click the Code tab. On the Code tab, click Invoke to execute the function in the Function Compute console.

Step 6: View the execution logs of the function

You can view the execution log for each execution on the Code tab. To view historical execution logs, you must configure a Logstore for the function first. Then, you can view historical execution logs on the Logs tab. For more information, see Configure and view function logs.

What to do next

After you complete the preceding steps, you can read the following topics based on your business scenarios to perform more advanced operations: