This topic describes how to use Funcraft to create a function in Function Compute. For example, we can create a helloworld function.

Background information

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

Step 1: Configure Funcraft

  1. Run the following command to initialize Funcraft and open the account information configuration page.
    fun config
  2. Sequentially specify AccountID (the ID of the Alibaba Cloud account), AccessKey ID, AccessKey Secret, and Default Region Name for Funcraft as prompted.
    If you are using a Resource Access Management (RAM) user, set AccountID to the Account ID of the corresponding Alibaba Cloud account and set AccessKey ID and AccessKey Secret to the AccessKey pair of the RAM user.
    Note You can query the Account ID on the Account Management page and AccessKey pair of the current account on the User Management page.
    After the configuration is completed, Funcraft saves the configurations to the .fcli/config.yaml file in the user 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.
    • Templates prefixed with event- are common event functions.
    • Templates prefixed with http-trigger automatically create HTTP triggers. An HTTP trigger uses request and response as input parameters, helping you quickly create a web application.

    In this example, we select the event-nodejs10 template.

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

    • The index.js file contains sample code of the function.
    • The template.yml file describes how Funcraft creates a function resource.
      • In this example, the demo service and the demo function are created for you.
      • For more configuration items supported by the template.yml file, see Serverless Application Model.

(Optional) Step 3: Locally debug the function

To locally debug the function, you must install Docker locally. For more information, see Install Docker. If you cannot install Docker locally, skip this step and debug the function in the cloud.

Run the following command locally to debug the function.

cd demo
fun local invoke demo
Note It takes a relatively long time to debug the function for the first time because the image of the execution environment is pulled locally.

Step 4: Deploy the function to the cloud

  1. Run the following command to deploy the function to the cloud.
    fun deploy
  2. During deployment, enter Y to confirm the resources you want to create.
    After the creation is completed, a message "service demo deploy success" appears, indicating that your resources are deployed.

Test the function in the cloud

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 left-side navigation pane, click Service/Function.
  3. On the page that appears, click the demo service. On the Functions tab, click the demo function.
  4. 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 6: View execution 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.

Advanced tutorial

After you complete the preceding tutorials, you can further learn about the following topics based on your actual scenarios: