This topic describes how to deploy an application to Function Compute by using the commands provided by the FC component. In this topic, the demo application that contains a function whose output is Hello World is created and deployed.

Prerequisites

Before you create an application, make sure that the following operations are complete:

Procedure

  1. Initialize a project.
    Run the following command in the project directory to initialize a project:
    s init node.js12-http -d fc-hello-world-demo
    Parameters:
    • node.js12-http: specifies that an HTTP function is to be created and the runtime environment is Node.js 12.
    • -d: the project to be initialized. In this example, the project named fc-hello-world-demo is to be initialized.
    Execution result:
    √ File decompression completed
    ? Do you want to install dependencies? No // Dependencies are not installed during the execution. 
    ? please select credential alias default // Select a key alias. 
    
     Thanks for using Serverless-Devs
     You could [cd C:\Users\ty\Desktop\ddd\jy\fc-hello-world-demo] and enjoy your serverless journey!
     If you need help for this example, you can use [s -h] after you enter folder.
     Document Star: https://github.com/Serverless-Devs/Serverless-Devs
    After the project is initialized, a file named fc-hello-world-demo is generated in the current directory. fc-hello-world-demo contains the following files:
    • s.yaml: the YAML file that defines Function Compute resources. For more information about the YAML format, see YAML syntax.
    • index.js: the file that stores the code logic of functions.
    • package.json: the file that lists dependencies. The file defines the dependent modules and the configurations of the project.
  2. Run the following command to go to the project directory:
    cd fc-hello-world-demo
  3. Build an application.
    Run the following command in the project directory to build an application:
    s build
    Execution result:
    [2021-07-28T17:42:41.309] [INFO ] [S-CLI] - Start ...
    [2021-07-28T17:42:53.916] [INFO ] [FC-BUILD] - Build artifact start...
    builder begin to build, runtime is: nodejs10, sourceDir:  C:\Users\ty\Desktop\ddd\jy\fc-hello-world-demo\
    running task: flow NpmTaskFlow
    running task: CopySource
    running task: NpmInstall
    npm WARN fc-deploy-test@1.0.0 No description
    npm WARN fc-deploy-test@1.0.0 No repository field.
    added 1 package in 0.437s
    [2021-07-28T17:42:58.630] [INFO ] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
    If the execution is successful, a file named .s is generated in the current directory. The file stores the build products that are returned and is used for application debugging and deployment on your computer. .s contains the node_modules file, which stores dependencies.
  4. Optional:Debug the application on your computer.
    After you build the application, you may want to debug the application on your computer to check whether it can work as expected. Run one of the following commands based on the function type:
    • HTTP function
      s local start
      Execution result:
      [2021-06-04T15:50:26.113] [INFO ] [S-CLI] - Start ...
      [2021-06-04T15:50:26.116] [INFO ] [S-CLI] - It is detected that your project has the following projects < fc-deploy-test > to be execute
      [2021-06-04T15:50:26.116] [INFO ] [S-CLI] - Start executing project fc-deploy-test
      √ File decompression completed
      [2021-06-04T15:50:58.600] [INFO ] [FC-LOCAL-INVOKE] - Using build codeUri: /Users/zqf/Documents/demo/s/hello-world/fc-hello-world-demo/.s/build/artifacts/fc-deploy-service/http-trigger-function.
      [2021-06-04T15:50:58.621] [INFO ] [FC-LOCAL-INVOKE] - Trigger for start is:
      name: httpTrigger
      type: http
      config:
        authType: anonymous
        methods:
          - GET
      
      [2021-06-04T15:50:58.665] [INFO ] [FC-LOCAL-INVOKE] - HttpTrigger httpTrigger of fc-deploy-service/http-trigger-function was registered
          url: http://localhost:8000/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
          methods: GET
          authType: anonymous
      [2021-06-04T15:50:58.682] [INFO ] [S-CLI] - Project fc-deploy-test successfully to execute
      
      fc-deploy-test:
        status: succeed
      
      function compute app listening on port 8000!
    • Event function
      s local invoke
    The execution result varies with the function type.
    • HTTP function
      If you debug an HTTP function, a URL is returned in the result after you run the debugging command. You need to use one of the following methods to continue the debugging process:
      • Run the cURL command to trigger the HTTP function
        curl http://localhost:8000/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
      • Use a browser to trigger the HTTP function
        http://localhost:8000/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
    • Event function

      If you debug an event function, the debugging result is directly returned after you run the debugging command.

  5. Deploy the application.
    Run the following command to deploy the application to Function Compute:
    s deploy
    Execution result:
    [2021-06-04T16:18:32.365] [INFO ] [S-CLI] - Start ...
    [2021-06-04T16:18:32.368] [INFO ] [S-CLI] - It is detected that your project has the following projects < fc-deploy-test > to be execute
    [2021-06-04T16:18:32.368] [INFO ] [S-CLI] - Start executing project fc-deploy-test
    √ File decompression completed
    [2021-06-04T16:19:01.962] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-06-04T16:19:01.963] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-06-04T16:19:01.963] [INFO ] [FC-DEPLOY] - Using accountId: 1879860868****
    [2021-06-04T16:19:01.963] [INFO ] [FC-DEPLOY] - Using accessKeyId: LTAI4G4cwJkK4Rza6xd9****
    [2021-06-04T16:19:08.684] [INFO ] [FC-DEPLOY] - service: fc-deploy-service dose not exist online, fc will use local config from now on.
    [2021-06-04T16:19:08.688] [INFO ] [FC-DEPLOY] - Fc detects that you have run build command for function: http-trigger-function, use build codeUri: /Users/zqf/Documents/demo/s/hello-world/fc-hello-world-demo/.s/build/artifacts/fc-deploy-service/http-trigger-function instead of your codeUri: ./
    
      detail:
      added:
        LD_LIBRARY_PATH: >-
          /code/.s/root/usr/local/lib:/code/.s/root/usr/lib:/code/.s/root/usr/lib/x86_64-linux-gnu:/code/.s/root/usr/lib64:/code/.s/root/lib:/code/.s/root/lib/x86_64-linux-gnu:/code/.s/root/python/lib/python2.7/site-packages:/code/.s/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib
        PATH: >-
          /code/.s/root/usr/local/bin:/code/.s/root/usr/local/sbin:/code/.s/root/usr/bin:/code/.s/root/usr/sbin:/code/.s/root/sbin:/code/.s/root/bin:/code:/code/node_modules/.bin:/code/.s/python/bin:/code/.s/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin
        NODE_PATH: /code/node_modules:/usr/local/lib/node_modules
        PYTHONUSERBASE: /code/.s/python
      deleted: null
      updated: null
    
    // Specify whether to allow FC to add the environment variables that are used to read the installed dependencies. You can also set the --assume parameter when you run the deployment command. A value of yes indicates that the environment variables are to be added. 
    ? Fc add/append some content to your origin environment variables for finding dependencies generated by build command.
     Are you sure to continue? yes
    [2021-06-04T16:19:21.260] [INFO ] [FC-DEPLOY] - function: http-trigger-function dose not exist online, fc will use local config from now on.
    [2021-06-04T16:19:21.462] [INFO ] [FC-DEPLOY] - trigger: httpTrigger dose not exist online, fc will use local config from now on.
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-06-04T16:19:25.971] [INFO ] [FC-DEPLOY] - Waiting for service fc-deploy-service to be deployed
    [2021-06-04T16:19:25.971] [INFO ] [FC-DEPLOY] - Waiting for function http-trigger-function to be deployed
    [2021-06-04T16:19:25.971] [INFO ] [FC-DEPLOY] - Waiting for triggers httpTrigger to be deployed
    √ Make service fc-deploy-service success.
    √ Make function fc-deploy-service/http-trigger-function success.
    √ Make trigger fc-deploy-service/http-trigger-function/httpTrigger success.
    [2021-06-04T16:19:26.414] [INFO ] [FC-DEPLOY] - Deployed:
    Service: fc-deploy-service
    Function: http-trigger-function
    Triggers httpTrigger
    [2021-06-04T16:19:26.418] [INFO ] [S-CLI] - Project fc-deploy-test successfully to execute
    
    fc-deploy-test:
      region: cn-hangzhou
      service:
        name: fc-deploy-service
        description: demo for fc-deploy component
        internetAccess: true
      function:
        name: http-trigger-function
        description: this is a test
        handler: index.handler
        memorySize: 128
        timeout: 60
        instanceConcurrency: 1
        instanceType: e1
        runtime: nodejs10
        initializer: index.initializer
        initializationTimeout: 60
        environmentVariables:
          testEnv: true
          LD_LIBRARY_PATH: >-
            /code/.s/root/usr/local/lib:/code/.s/root/usr/lib:/code/.s/root/usr/lib/x86_64-linux-gnu:/code/.s/root/usr/lib64:/code/.s/root/lib:/code/.s/root/lib/x86_64-linux-gnu:/code/.s/root/python/lib/python2.7/site-packages:/code/.s/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib
          PATH: >-
            /code/.s/root/usr/local/bin:/code/.s/root/usr/local/sbin:/code/.s/root/usr/bin:/code/.s/root/usr/sbin:/code/.s/root/sbin:/code/.s/root/bin:/code:/code/node_modules/.bin:/code/.s/python/bin:/code/.s/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin
          NODE_PATH: /code/node_modules:/usr/local/lib/node_modules
          PYTHONUSERBASE: /code/.s/python
        codeUri: >-
          /Users/zqf/Documents/demo/s/hello-world/fc-hello-world-demo/.s/build/artifacts/fc-deploy-service/http-trigger-function
      systemDomain: >-
        https://xxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
      triggers:
        - name: httpTrigger
          type: http
          config:
            authType: anonymous
            methods:
              - GET
    If the execution is successful, the application is deployed to Function Compute.
  6. Optional:Remotely debug the application.
    Run the following command to remotely debug the application:
    s invoke
    Execution result:
    [2021-06-04T16:27:32.431] [INFO ] [S-CLI] - Start ...
    [2021-06-04T16:27:32.433] [INFO ] [S-CLI] - It is detected that your project has the following projects < fc-deploy-test > to be execute
    [2021-06-04T16:27:32.434] [INFO ] [S-CLI] - Start executing project fc-deploy-test
    √ File decompression completed
    https://xxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
    
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 595612e7-cba7-4a5c-a250-77186791a6af
    2021-06-04T08:27:55.951Z 595612e7-cba7-4a5c-a250-77186791a6af [verbose] hello world
    FC Invoke End RequestId: 595612e7-cba7-4a5c-a250-77186791a6af
    
    Duration: 5.24 ms, Billed Duration: 6 ms, Memory Size: 128 MB, Max Memory Used: 49.67 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {
        "message": "Hello World",
        "path": "/",
        "queries": {},
        "headers": {
            "accept": "application/json",
            "authorization": "FC xxx:8VNp0ONqfb58OUs0PYnonoeXnBdH4hyGQzrUujGzDfg=",
            "date": "Fri, 04 Jun 2021 08:27:55 GMT",
            "host": "xxx.cn-hangzhou.fc.aliyuncs.com",
            "user-agent": "Node.js(v12.18.3) OS(darwin/x64) SDK(@alicloud/fc2@v2.2.2)",
            "x-forwarded-proto": "http"
        },
        "method": "GET",
        "requestURI": "/2016-08-15/proxy/fc-deploy-service/http-trigger-function/",
        "clientIP": "42.120.75.254",
        "uuid": "b8d5d109-9418-4fdb-9720-c92e10300213",
        "body": ""
    }
    
    
    [2021-06-04T16:27:55.979] [INFO ] [S-CLI] - Project fc-deploy-test successfully to execute
    
    End of method: invoke
    The deployed function is an HTTP function. Therefore, a URL is returned after you run the remote debugging command. You can run the following cURL command to use the URL to trigger the HTTP function in Function Compute:
    curl https://xxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
  7. Optional:Delete the application.
    Run the following command to delete the application:
    s remove service
    Execution result:
    [2021-06-04T16:33:02.094] [INFO ] [S-CLI] - Start ...
    [2021-06-04T16:33:02.096] [INFO ] [S-CLI] - It is detected that your project has the following projects < fc-deploy-test > to be execute
    [2021-06-04T16:33:02.097] [INFO ] [S-CLI] - Start executing project fc-deploy-test
    [2021-06-04T16:33:08.056] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-06-04T16:33:08.056] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-06-04T16:33:08.056] [INFO ] [FC-DEPLOY] - Using accountId: 1879860868****
    [2021-06-04T16:33:08.056] [INFO ] [FC-DEPLOY] - Using accessKeyId: LTAI4G4cwJkK4Rza6xd9****
    [2021-06-04T16:33:10.354] [INFO ] [FC-DEPLOY] - service: fc-deploy-service exists online.
    [2021-06-04T16:33:12.314] [INFO ] [FC-DEPLOY] - function: http-trigger-function exists online.
    [2021-06-04T16:33:14.468] [INFO ] [FC-DEPLOY] - trigger: httpTrigger exists online.
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    √ Delete trigger fc-deploy-service/http-trigger-function/httpTrigger success.
    √ Delete function fc-deploy-service/http-trigger-function success.
    √ Delete service fc-deploy-service success.
    [2021-06-04T16:33:18.880] [INFO ] [S-CLI] - Project fc-deploy-test successfully to execute
    
    End of method: remove
                            
    If the execution is successful, the application is deleted.