This topic describes how to run the fun deploy command or use Resource Orchestration Service (ROS) to deploy an application to the cloud.

Prerequisites

Before you begin, make sure that the following operations are complete:

Background information

Funcraft allows you to use ROS to deploy applications. The deployment by using ROS provides benefits such as a complete management mechanism for resource status and a rollback mechanism.
Benefit Description
Complete management mechanism for resource status When you use ROS to deploy applications, Funcraft can detect new, updated, deleted, and fixed resources to deploy applications as scheduled.
Rollback mechanism When you deploy an application, a failure to manage resources causes the entire stack to be rolled back. If an application fails to be deployed when you use ROS to deploy the application, the application can roll back to the previous available state to ensure service availability.
Flexible definition of models Generally, the template.yml file is used to describe a complete application. The application is deployed as described in the template.yml file. ROS allows you to deploy applications more flexibly.
Clear deployment differences When you use ROS to deploy applications, you can know whether the current template file is synchronized with the online file. This way, you can efficiently determine whether to directly deploy the on-premises template to the cloud.
Support for more cloud resources ROS allows you to manage more cloud resources, which covers all usage scenarios.
Support for efficiently creating multiple development environments If you need to create multiple development environments, you do not need to modify the unique values of resource identifiers.
ROS allows you to describe more resources in template files. You can declare Funcraft resources as well as ROS-supported resources in template files of Funcraft. For more information, see Resource type index. For example, you can use the following code to declare an Object Storage Service (OSS) bucket:
Bucket:
  Type: 'ALIYUN::OSS::Bucket'
  Properties:
    BucketName: 'oss-unzip-demo-bucket' // Replace oss-unzip-demo-bucket with your bucket name. 

Obtain the help information of the fun deploy command

Run the following command to obtain the help information of the fun deploy command:
fun deploy -h
Expected output:
Usage: fun deploy [options] [resource]


  Deploy a serverless application.

  use 'fun deploy' to deploy all resources
  use 'fun deploy serviceName' to deploy all functions under a service
  use 'fun deploy functionName' to deploy only a function resource

  with '--only-config' parameter, will only update resource config without updating the function code

  use '--parameter-override', A parameter structures that specify input parameters for your stack template.
                              If you're updating a stack and you don't specify a parameter, the command uses the stack's existing value.
                              For new stacks, you must specify parameters that don't have a default value. Syntax: parameterkey=parametervalue.


Options:
  -t, --template [template]             The path of fun template file.
  -c, --only-config                     Update only configuration flags
  -p, --parameter-override <parameter>  A parameter structures that specify input parameters for your stack template.
  -y, --assume-yes                      Automatic yes to prompts. Assume "yes" as answer to all prompts and run non-interactively.
  --use-ros                             Deploy resources using ROS
  --use-nas                             Automatically upload local resources to NAS.
  --stack-name <stackName>              The name of the ROS stack
  --push-registry <pushRegistry>        Modify the image upload path
  -h, --help                            display help for command

Run the fun deploy command to deploy an application

  1. Create an Index.js file in the project directory, copy the following content to the Index.js file, and then save the Index.js file:
    var getRawBody = require('raw-body')
    
    module.exports.handler = function (request, response, context) {    
        // Obtain the request body.
        getRawBody(request, function (err, body) {
            var respBody = {
                headers: request.headers,
                url: request.url,
                path: request.path,
                queries: request.queries,
                method: request.method,
                clientIP: request.clientIP,
                body: body.toString()
            };
    
            response.setStatusCode(200);
            response.setHeader('content-type', 'application/json');
            response.send(JSON.stringify(respBody, null, 4));
        });
    };       
  2. Create a template.yml file in the project directory, copy the following content to the template.yml file, and then save the template.yml file:
    ROSTemplateFormatVersion: '2015-09-01'
    Transform: 'Aliyun::Serverless-2018-04-03'
    Resources:
      local-http-test:
        Type: 'Aliyun::Serverless::Service'
        Properties:
          Description: 'local invoke demo'
        nodejs8:
          Type: 'Aliyun::Serverless::Function'
          Properties:
            Handler: index.handler
            CodeUri: './'
            Description: 'http trigger demo with nodejs8!'
            Runtime: nodejs8
          Events:
            http-test:
              Type: HTTP
              Properties:
                AuthType: ANONYMOUS
                Methods: ['GET', 'POST', 'PUT']       
  3. After you compile the code and edit the template file, run the following command to deploy the application to the cloud:
    fun deploy
    Expected output:
    using region: cn-shanghai
    using accountId: ***********8320
    using accessKeyId: ***********1EXB
    using timeout: 10
    
    Waiting for service local-http-test to be deployed...
            Waiting for function nodejs8 to be deployed...
                    Waiting for packaging function nodejs8 code...
                    package function nodejs8 code done
                    Waiting for HTTP trigger http-test to be deployed...
                    methods: GET
                    url: https://****************.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/local-http-test/nodejs8/
                    function http-test deploy success
            function nodejs8 deploy success
    service local-http-test deploy success

Use ROS to deploy an application

Procedure

  1. Run the following command to initialize a sample project:
    fun init event-nodejs8 -n RosDemo  
    • event-nodejs8: the application template.
    • RosDemo: the custom file name of the deployment project.
  2. In the project directory, run the following command to package the project and upload resources to OSS:
    fun package --oss-bucket <bucketname>      

    Replace <bucketname> with the name of your bucket.

    Note
    • The bucket must reside in the same region as the created function.
    • If you use ROS to deploy applications, you cannot use on-premises code resources and must use resources in the cloud. Therefore, you must first run the fun package command to upload resources to OSS.
  3. Run the following command to deploy the project:
    fun deploy --use-ros --stack-name <stackName>        
    Options in the preceding command:
    • --use-ros: specifies that you use ROS to deploy the application.
    • --stack-name: the name of the stack used when you use ROS to deploy the application. A stack is the IDs of a set of applications that you use template files to deploy. You can use one template file to deploy multiple applications by specifying different stack names. When you use a template file to deploy applications, the resource properties in the template file are compared with the specified stack before the online resources are created, deleted, or updated.
      Note You can customize the --stack-name <stackName> option, leave it unspecified, or set it to the name of a created stack. If you customize this option or leave it unspecified, the system automatically creates a stack.
    After the fun deploy command is run, you can log on to the Function Compute console to view the created service and function.