This topic describes the procedure for using Fun to develop a serverless application named helloworld. In this procedure, you need to create an application, test the application in the local mock Link IoT Edge environment on your computer, and then deploy the application to Function Compute.
Overview
To develop a serverless application by using Fun, follow these steps:
- Create an application. Run the
fun initcommand to create an application based on a specific template file. - Test the application. Run a specific command, for example,
fun edge invoke, to call and test the application. - Deploy the application. Run the
fun deploycommand to deploy the application to Function Compute. Then you can use the application in the same way as using other applications in Link IoT Edge.
Prerequisites
Fun is installed and configured. For more information, see Install and configure Fun.
Procedure
- Create a serverless application.
Create a project directory in any position on your computer, and then run the
fun initcommand in the directory to create a serverless application.Important Currently, Link IoT Edge can only run serverless applications in Node.js 8 and Python 3.$ mkdir helloworld $ cd helloworld/ $ fun init ? Select a template to init helloworld-nodejs8 Start rendering template... + /private/tmp/helloworld + /private/tmp/helloworld/index.js + /private/tmp/helloworld/template.yml finish rendering template.In the preceding information, the template.yml file defines a serverless application model. Key parameters in the template.yml file are as follows:
ROSTemplateFormatVersion: '2015-09-01' Transform: 'Aliyun::Serverless-2018-04-03' Resources: helloworld: # The service name. Type: 'Aliyun::Serverless::Service' Properties: Description: 'helloworld' helloworld: # The function name. Type: 'Aliyun::Serverless::Function' Properties: Handler: index.handler # The event processing function. Runtime: nodejs8 # The runtime environment of the function. Link IoT Edge only supports Python 3 and Node.js 8. CodeUri: 'index.js' # The source code of the function, which will be deployed to Function Compute.For more information about the template.yml file, see Serverless Application Model.
- Test the application on your computer.
To test the created serverless application, follow these steps:
- Run the following command to start the local mock Link IoT Edge environment on your computer. The local mock Link IoT Edge environment uses the same port as Link IoT Edge that is officially released. Therefore, make sure that Link IoT Edge is not running before you start the local mock Link IoT Edge environment.
fun edge startNote During the startup, enter certificate information about the gateway device as prompted. - Go to the directory where the template.yml file is located and run the following command to call the helloworld function:
fun edge invoke helloworldBy default, the
invokecommand reads event content from the standard input and then calls the specific function. You can directly enter the event content on the command line, press Enter, and then press Ctrl+D to end the input.You can also use the
-eparameter to specify the event file to read. If the function is called successfully, the return value of the function is displayed.$ echo "{}" > event.json $ fun edge invoke -e event.json hellworld/helloworld Using template file at /private/tmp/helloworld/template.yml. Invoking function helloworld (nodejs8). hello world
- Run the following command to start the local mock Link IoT Edge environment on your computer. The local mock Link IoT Edge environment uses the same port as Link IoT Edge that is officially released. Therefore, make sure that Link IoT Edge is not running before you start the local mock Link IoT Edge environment.
- Deploy the application to Function Compute.
If the application passes the test, go to the directory where the template.yml file is located and run the following command to deploy the application to Function Compute:
fun deploy