This topic describes how to use Funcraft to create a function in Function Compute. In this topic, a demo function whose output is "Hello World" is created.

Prerequisites

Background information

Funcraft is an application deployment tool provided by Function Compute. You can use Funcraft to manage resources of services such as Function Compute, API Gateway, and Log Service to deploy applications. For more information about Funcraft, see Features.

Try Funcraft in Cloud Shell

Procedure

  1. Create a template for initialization.
    1. Run the following command to initialize the project template:
      fun init -n demo
    2. Select a project template as prompted.
      ? Select a template to init (Use arrow keys or type to search)
      > event-nodejs12
        event-nodejs10
        event-nodejs8
        event-nodejs6
        event-python3
        event-python2.7
        event-java8
        event-php7.2
        event-dotnetcore2.1
        http-trigger-nodejs12
        http-trigger-nodejs10
        http-trigger-nodejs8
        http-trigger-nodejs6
        http-trigger-python3
        http-trigger-python2.7
        http-trigger-java8
      (Move up and down to reveal more choices)
      The following types of project templates are available:
      • 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 to help you create a web application.

      In this example, event-nodejs10 is used.

      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.
  2. Optional:Debug the function on your computer.

    To debug the function on your computer, you must first install Docker on your computer. For more information, see Install Docker. If you cannot install Docker on your computer, skip this step and debug the function in the Function Compute console.

    1. Run the following command to access the demo function:
      cd demo
    2. Run the following command on your computer to debug the function:
      fun local invoke demo
      local_test
      Note The first time you debug the function, the image of the invocation environment is pulled to your computer. This takes a long time to complete.
  3. Deploy the function to Function Compute.
    1. Run the following command to deploy the function to Function Compute:
      fun deploy
    2. During the deployment process, enter Y to confirm the resources that you want to create.
      deploy
      After the resources are created, the service demo deploy success message appears. The message indicates that your resources are deployed.
  4. Test the result in the Function Compute console.

    You can log on to the Function Compute console to check whether the resources are deployed.

    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 Services and Functions.
    4. Click the demo service. On the Functions tab, click the demo function.
      look_for_fuction
    5. Click the Code tab. On the Code tab, click Invoke to invoke the function in the Function Compute console.
      execute_function
  5. View logs.

    After you invoke the function, you can view the invocation log on the Code tab. To view historical invocation logs, you must first configure a Logstore for the function. Then, you can view historical invocation logs on the Logs tab. For more information, see Configure Log Service resources and view function execution 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: