Introduction
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:
- 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.
- 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.
- 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.
- 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.
- Limited resource types: Function Compute provides multiple resources for you and ROS provides cloud resources for more scenarios.
- 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:
var getRawBody = require('raw-body')
module.exports.handler = function (request, response, context) {
// get 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));
});
};
To configure related services, create a template.yml file in the root directory of the project and enter the following code:
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']
You can then use the fun deploy
command to deploy the application online.
$fun deploy
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://1984152879328320.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
Syntax of the ROS deployment
You can add the -h option after the fun deploy command to view the help information:
$fun deploy -h
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
Options:
-t, --template [template] The path of fun template file.
-c, --only-config Update only configuration flags
--use-ros Deploy resources using ROS
--stack-name <stackName> The name of the ROS stack
-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:
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:
fun package --oss-bucket bucket-name
3. Deploy resources
Use the following command to deploy resources through ROS:
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:
fun deploy --use-ros --stack-name test
fun deploy --use-ros --stack-name staging
fun deploy --use-ros --stack-name prod
... ...
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:
Bucket:
Type: 'ALIYUN::OSS::Bucket'
Properties:
BucketName: 'oss-unzip-demo-bucket'