资源编排中的模板,是对一组资源的定义和描述。您可以按照模板语法和资源的属性,编写自己的模板,也可以直接使用阿里云提供的模板样例。目前,支持20多种阿里云资源的编排服务。本文将对阿里云资源和资源编排模板分别进行介绍。
开始编写模板之前,您需要对所需的资源有所了解。
一个资源类型,对应一种阿里云资源,是每种资源的唯一标识。您可以查阅资源类型列表。
例如,阿里云云服务器的资源类型是 ALIYUN::ECS::Instance 。
列出了当前资源支持的所有可配置属性。
如上图所示,云服务器的可配置属性有 SecurityGroupId , InstanceType , ImageId 等。归根结底,编写模板就是编辑资源,您可以通过配置资源的属性来自定义资源。在元数据中,清楚了描述了每个属性的特性。下面介绍几个比较重要的特性:
key: 属性关键字。通过它来标识您要配置当前资源的哪个属性。
type: 表明当前属性值是什么类型。您在给属性赋值的时候,类型一定要匹配。否则在创建模板的时候,语法检查会不通过。
required: 表明此属性是否是必须配置项。如果某项属性被标记为 required 为 true ,那么这个属性是必须要配置的。例如,资源 ALIYUN::ECS::Instance 的 SecurityGroupId , InstanceType 和 ImageId 三个属性都被标记为 required ,那么当您编辑 ALIYUN::ECS::Instance 类型的资源的时候,这三个属性的配置是不能省略的。
constraints: 表明当前属性是否有其他约束。例如, DiskMappings 的constraints如下:
[ { "length": { "max": 4 } } ] 表明DiskMappings的值的长度不能超过4,同时DiskMappings的type为list,那么当您给DiskMappings赋值时,要配置成长度不好过4的数组。
Attributes 枚举了当前资源的所有 可获取属性 ,以及属性的描述信息。
如上图所示,云服务器的可获取属性有 InstanceId , ZoneId 等。可获取属性,可以在 Resources 和 Outputs 两部分中进行获取。例如,下面模板片段定义了一个名为WebServer的 ALIYUN::ECS::Instance 资源,在Outputs部分通过 Fn::GetAtt 方法,获取到WebServer的实例ID。
"Resources" : { "WebServer": { "Type": "ALIYUN::ECS::Instance", "Properties": { "ImageId" : {"Ref": "ImageId"}, "InstanceType": {"Ref": "InstanceType"}, "SecurityGroupId": {"Ref": "SecurityGroupId"} } } }, "Outputs": { "InstanceId": { "Value" : {"Fn::GetAtt": ["WebServer","InstanceId"]} } } 接下来,会对资源编排模板进行详细的介绍。模板是一个 JSON 格式的文本文件。一般而言,模板分为六个部分: ROSTemplateFormatVersion , Description , Parameters , Mappings , Resources 和 Outputs 。 格式如下:
{ "ROSTemplateFormatVersion" : "2015-09-01", "Description" : "模板描述", "Parameters" : { }, "Mappings" : { }, "Resources" : { }, "Outputs" : { } } 其中,只有 ROSTemplateFormatVersion 是 必须指定 的部分。
2015-09-01 。 下面对 Parameters , Mappings , Resources , Outputs 逐一说明。
描述资源栈中每个资源,以及资源之间的关系。一个资源可以被其他资源和 Outputs 所引用。
Resources 语法
"Resources" : { "资源 ID" : { "Type" : "资源类型", "Properties" : { }, "DeletionPolicy" : "Retain", "DependsOn": "资源" } } Properties:
ROS控制台 -> 资源类型 -> 属性数据 来查看。例如,ECS Instance 的Properties 地址。 ImageId 和 'SecurityGroupId'分别引用参数 ImageIdRef 和 SecurityGroupId 参数。有关 Parameters 的详解见下节。 { "ROSTemplateFormatVersion" : "2015-09-01", "Description": "ecs实例,指定imageId", "Parameters" : { "ImageIdRef": { "Type" : "String", "Description": "镜像文件 ID,表示启动实例时选择的镜像资源" }, "SecurityGroupId": { "Type": "String", "Description": "安全组Id" } }, "Resources" : { "WebServer": { "Type": "ALIYUN::ECS::Instance", "Properties": { "ImageId" : {"Ref": "ImageIdRef"}, "InstanceType": "ecs.t1.small", "SecurityGroupId": {"Ref": "SecurityGroupId"} } } } } DeletionPolicy: 配置此属性,可以在资源栈被删除时 保留 该资源
DependsOn: 指定资源的创建顺序。下面是一个简单的例子,DatabaseServer 将在 WebServer 创建成功后才开始创建。
{ "ROSTemplateFormatVersion" : "2015-09-01", "Description": "先创建DBServer, 再创建WebServer", "Parameters" : { "SecurityGroupId": { "Type": "String", "Description": "安全组Id" } }, "Resources" : { "WebServer": { "Type": "ALIYUN::ECS::Instance", "DependsOn": "DatabseServer" }, "DatabseServer": { "Type": "ALIYUN::ECS::Instance", "Properties": { "ImageId" : "m-25l0rcfjo", "InstanceType": "ecs.t1.small", "SecurityGroupId": {"Ref": "SecurityGroupId"} } } } } 把可能会变化的值,放到Parameter中,让模板变得更灵活。详细描述请参考帮助文档。 注意 ,这里仅仅是把易变项抽象为参数而已,在创建资源栈的时候需要给每个参数赋值。
Parameters 语法
通过文档可知 Parameters 可以配置的属性比较多,不过只有 Type 这个属性是必须设置的。 格式如下:
"Parameters" : { "参数 ID" : { "Type" : "参数类型" } } Resources 部分引用。 String , Number , CommaDelimitedList , Json , Boolean 五种数据类型。 Default值 。 String 类型时,可以通过这两个配置项来限定参数值的长度。 Number 类型时,可以通过这两个配置项来限定参数值的大小。 NoEcho: true Parameters 示例
以下 Parameters 部分示例声明两个参数。
String 类型,默认值是 ecs.t1.small ,允许值是 ecs.t1.small 和 ecs.t1.xsmall 两种值。 Number ,可指定的最大值是 100 ,最小值是 1 . "Parameters" : { "InstanceType": { "Type": "String", "Default": "ecs.t1.small", "Description": "实例的资源规格", "AllowedValues": [ "ecs.t1.xsmall", "ecs.t1.small" ] }, "SecurityGroupPriority": { "Default": 1, "Description": "生成的安全组的优先级,优先级可选范围为1-100,默认值为1,即最高优先级。", "Type": "Number", "MinValue": 1, "MaxValue": 100 } } 在 Mappings 部分,您可以配置多个枚举值或者映射表。在模板的 Resources 和 Outputs 部分可以使用 Fn::FindInMap 内部函数,通过给定 key 获取 value 。
Mappings 语法
映像部分由 Key-Value 对组成。其中 Key 和 Value 可以为字符串类型或者数字类型。筛选某个值,搜索深度为3。定义 Mappings ,格式如下:
"Mappings" : { "ValidMapping" : { "TestKey1" : { "key1" : "value1" } } } 获取枚举值的方法如下:
{ "Fn::FindInMap" : [ "ValidMapping", { "Ref" : "TestKey1" }, "key1"]} Mappings 示例
以下示例,展示类型是 ALIYUN::ECS::Instance 的资源 WebServer,其属性 ImageId 根据 region 以及 操作系统位数 进行映射。
{ "ROSTemplateFormatVersion": "2015-09-01", "Parameters": { "regionParam": { "Description": "选择创建ECS的区域", "Type": "String", "AllowedValues": ["hangzhou", "beijing"] }, "sysTypeParam": { "Description": "操作系统位数", "Type": "Number", "AllowedValues": [32, 64] }, "SecurityGroupId": { "Type": "String", "Description": "安全组Id" }de }, "Mappings" : { "RegionMap" : { "hangzhou" : { "32" : "m-25l0rcfjo", "64" : "m-25l0rcfj1" }, "beijing" : { "32" : "m-25l0rcfj2", "64" : "m-25l0rcfj3" } } }, "Resources": { "WebServer": { "Type": "ALIYUN::ECS::Instance", "Properties": { "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "regionParam" }, { "Ref" : "sysTypParam" }]}, "InstanceType": "ecs.t1.small", "SecurityGroupId": {"Ref": "SecurityGroupId"} } } } } 当您调用查询资源栈详情接口的时候,资源编排服务会根据 Outputs 部分返回对应的值,往往用来获取资源的属性值。
Outputs 语法
输出部分由 输出ID 和 输出值表达式 组成。格式如下:
"Outputs" : { "输出 ID" : { "Description" : "输出的描述", "Value" : {输出值表达式} } } 其中,输出值表达式,使用内部函数 Fn::GetAtt 来获取资源的属性值。格式如下:
{"Fn::GetAtt": ["resourceId", "attributeId"]} Outputs 示例
在以下示例中,分别获取资源 WebServer 的 InstanceId 属性和 PublicIp 属性。
"Outputs": { "InstanceId": { "Value" : {"Fn::GetAtt": ["WebServer","InstanceId"]} }, "PublicIp": { "Value" : {"Fn::GetAtt": ["WebServer","PublicIp"]} } } 更多模板示例,请参考ROS 模板样例