All Products
Document Center

Use fun deploy to deploy applications

Last Updated: Jun 22, 2020


You can use fun deploy or Resource Orchestration Service (ROS) in Fun to deploy an application.

ROS adopts the Transform macro to transform the template syntax of Fun to the syntax supported by ROS. Resources supported by ROS such as Resource Access Management (RAM) and Function Flow can also be declared in Fun template files.

The ROS deployment provides solutions for the following issues:

  1. Incomplete resource status management mechanism: Original resources deployed by Fun can only be created and updated. You can use ROS within Fun to detect new, updated, deleted, and fixed resources to deploy applications as scheduled.
  2. Lack of a deployment failure rollback mechanism: A deployment often involves a lot of highly dependent resources. A failure to deploy one service may cause the entire deployment to fail. If a deployment fails, ROS ensures the application can roll back to the previous available state to ensure service availability.
  3. Applications deployed are not consistent with template definitions: The template.yml file is used to describe a complete application. ROS allows you to deploy applications as described in template files.
  4. Outdated template definitions: If the template of an application has not been synchronized for a long time, it may not be consistent with the online application. This may lead to failure or incompatibility when the new application is deployed from the template.
  5. Limited resource types: Function Compute provides multiple resources for you and ROS provides cloud resources for more scenarios.
  6. Unable to quickly create multiple development environments: The concept of resource sets does not exist in Fun. A template deploys the same exact resource even if it is deployed multiple times. This means that if you want to create multiple development environments, you must modify resource identifiers to unique values.

Use fun deploy

The following example uses fun deploy to deploy an HTTP trigger function. Create an index.js file in the root directory of the project and enter the following code:

  1. var getRawBody = require('raw-body')
  2. module.exports.handler = function (request, response, context) {
  3. // get request body
  4. getRawBody(request, function (err, body) {
  5. var respBody = {
  6. headers: request.headers,
  7. url: request.url,
  8. path: request.path,
  9. queries: request.queries,
  10. method: request.method,
  11. clientIP: request.clientIP,
  12. body: body.toString()
  13. };
  14. response.setStatusCode(200);
  15. response.setHeader('content-type', 'application/json');
  16. response.send(JSON.stringify(respBody, null, 4));
  17. });
  18. };

To configure related services, create a template.yml file in the root directory of the project and enter the following code:

  1. ROSTemplateFormatVersion: '2015-09-01'
  2. Transform: 'Aliyun::Serverless-2018-04-03'
  3. Resources:
  4. local-http-test:
  5. Type: 'Aliyun::Serverless::Service'
  6. Properties:
  7. Description: 'local invoke demo'
  8. nodejs8:
  9. Type: 'Aliyun::Serverless::Function'
  10. Properties:
  11. Handler: index.handler
  12. CodeUri: './'
  13. Description: 'http trigger demo with nodejs8!'
  14. Runtime: nodejs8
  15. Events:
  16. http-test:
  17. Type: HTTP
  18. Properties:
  19. AuthType: ANONYMOUS
  20. Methods: ['GET', 'POST', 'PUT']

You can then use the fun deploy command to deploy the application online.

  1. $fun deploy
  2. using region: cn-shanghai
  3. using accountId: ***********8320
  4. using accessKeyId: ***********1EXB
  5. using timeout: 10
  6. Waiting for service local-http-test to be deployed...
  7. Waiting for function nodejs8 to be deployed...
  8. Waiting for packaging function nodejs8 code...
  9. package function nodejs8 code done
  10. Waiting for HTTP trigger http-test to be deployed...
  11. methods: GET
  12. url:
  13. function http-test deploy success
  14. function nodejs8 deploy success
  15. service local-http-test deploy success

Syntax of the ROS deployment

You can add the -h option after the fun deploy command to view the help information:

  1. $fun deploy -h
  2. Usage: fun deploy [options] [resource]
  3. Deploy a serverless application.
  4. use 'fun deploy' to deploy all resources
  5. use 'fun deploy serviceName' to deploy all functions under a service
  6. use 'fun deploy functionName' to deploy only a function resource
  7. with '--only-config' parameter, will only update resource config without updating the function code
  8. Options:
  9. -t, --template [template] The path of fun template file.
  10. -c, --only-config Update only configuration flags
  11. --use-ros Deploy resources using ROS
  12. --stack-name <stackName> The name of the ROS stack
  13. -h, --help output usage information

To deploy ROS-related resources, you must add --use-ros and --stack-name after the fun deploy command. Add —use-ros to indicate an ROS deployment, and —stack-name to specify the name of the stack used.

A stack is the IDs of a set of applications (including cloud resources) deployed by template files. 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 will be compared with the specified stack before the online resources are created, deleted, or updated.

ROS deployment example

1. Initialize a function

Run the following command to initialize an example function:

  1. fun init -n RosDemo event-nodejs8

2. Package resources

All resources managed by ROS must be on the cloud and managed via Fun. Therefore, you must use the following fun package command to upload resources to OSS:

  1. fun package --oss-bucket bucket-name

3. Deploy resources

Use the following command to deploy resources through ROS:

  1. fun deploy --use-ros --stack-name bucket-name

ROS usage scenarios

1. Create multiple application deployment environments

You can specify different values for stackName to deploy applications to different environments:

  1. fun deploy --use-ros --stack-name test
  2. fun deploy --use-ros --stack-name staging
  3. fun deploy --use-ros --stack-name prod
  4. ... ...

2. Perform tests and delete test resources

If you want to delete used resources after tests, you can find the stacks that you want to delete in the ROS console and delete them. All the test resources will then be deleted.

3. Automatically rolls back to the previous available state when a deployment fails

The whole stack will roll back whenever a resource fails during deployment. This mechanism ensures service availability during deployment.

4. Describe more resources in template files

You can declare Fun resources as well as ROS-supported resources in template files of Fun. For example, you can use the following code to declare an OSS bucket:

  1. Bucket:
  2. Type: 'ALIYUN::OSS::Bucket'
  3. Properties:
  4. BucketName: 'oss-unzip-demo-bucket'