本模版示例支持为单/多个(最多3个)SLB添加所有创建的ECS做多个后端服务器
简述:
模版中的ECS实例支持指定不同的实例名和主机名,并且可以选择网络类型为VPC或CLASSIC,当选择不创建VPC时,需要更换ECS实例规格类型,原因是能创建ECS的实例规格,与VPC网络类型的实例规格不一样;默认是新创建一个SLB实例,如果传入SLB实例的ID,那么需要更改下SLB的数量,原因是根据模版中SLB的数量值和SLB的ID来确认添加后端服务器的SLB实例数量,需要两者保持一致(e.g. 用两个已经创建的SLB的ID来做参数,那么SLB数量值胥更改为2)。
本模版目前最多支持往3个SLB实例附加后端服务器;
涉及资源:
ALIYUN::ECS::VPC (虚拟专有网络)
ALIYUN::ECS::VSwitch (虚拟交换机)
ALIYUN::ECS::SecurityGroup (安全组)
ALIYUN::ECS::InstanceGroup (云服务器组)
ALIYUN::SLB::LoadBalancer (负载均衡)
ALIYUN::SLB::BackendServerAttachment (为负载均衡添加后端服务器)
1.创建ECS InstanceGroup
下面是创建VPC类型的ECS Instance代码示例,实例中批量创建ECS指定不同的实例名和主机名。
e.g.
HostName[1,4]Test
HostName: 是ECS InstanceGroup主机命名规则的前缀;
[1,4]: 1为起始数字,4为数字的位数;
Test: 为命名规则的尾缀;
结果创建的ECS的主机名分别为:HostName0001Test, HostName002Test, HostName0003Test, .....
{
"Type": "ALIYUN::ECS::InstanceGroup",
"Condition": "IsCreateVpc",
"Properties": {
"IoOptimized": "optimized",
"ImageId": {
"Ref": "EcsGroupImageId"
},
"SecurityGroupId": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"SG",
"SecurityGroupId"
]
},
{
"Ref": "ALIYUN::NoValue"
}
]
},
"Password": {
"Ref": "EcsPassword"
},
"MinAmount": {
"Ref": "MaxAmount"
},
"SystemDiskCategory": {
"Ref": "EcsSystemDiskCategory"
},
"MaxAmount": {
"Ref": "MaxAmount"
},
"VSwitchId": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"VSwitch",
"VSwitchId"
]
},
{
"Ref": "ALIYUN::NoValue"
}
]
},
"VpcId": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"Vpc",
"VpcId"
]
},
{
"Ref": "ALIYUN::NoValue"
}
]
},
"InstanceType": {
"Ref": "EcsGroupInstanceType"
},
"InstanceName": {
"Fn::Join": [
"",
[
{
"Ref": "InstanceNamePrefix"
},
"[",
{
"Ref": "InstanceNameBeginNumber"
},
",4]",
{
"Ref": "InstanceNameSuffix"
}
]
]
},
"HostName": {
"Fn::Join": [
"",
[
"HostNameTest",
"[1,4]",
{
"Ref": "InstanceNameSuffix"
}
]
]
},
"AllocatePublicIP": {
"Fn::If": [
"IsCreateVpc",
false,
true
]
},
"UserData": {
"Fn::Join": [
"",
[
"#!/bin/sh\n",
"echo \"hello , create ecs group test\" > /tmp/create_test.log\n"
]
]
}
}
}
2.为SLB实例添加后端服务器
下面示例代码创建的附加后端服务器功能,用到的一些ROS的函数功能,以及自己定义的一些逻辑判断,具体可根据方案模版查阅;
因为考虑到要为多个SLB实例添加后端服务器的情况,本方案用了Conditions及函数的功能配合完成了是为单/多个SLB添加后端服务器。
{
"Type": "ALIYUN::SLB::BackendServerAttachment",
"Condition": "IsAttachment1",
"Properties": {
"LoadBalancerId": {
"Fn::If": [
"IsCreateSlb",
{
"Fn::GetAtt": [
"LoadBalancer",
"LoadBalancerId"
]
},
{
"Fn::Select": [
"0",
{
"Fn::Split": [
",",
{
"Ref": "LoadBalancerIds"
}
]
}
]
}
]
},
"BackendServerList": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InstanceIds"
]
}
]
},
"BackendServerWeightList": {
"Fn::Split": [
",",
{
"Ref": "BackendServerWeightList"
}
]
}
}
}
3. 条件Code示例
请结合模版中的Paramters。
{
"IsAttachment1": {
"Fn::And": [
{
"Fn::Equals": [
1,
{
"Ref": "SlbCount"
}
]
},
{
"Fn::Or": [
"IsCreateSlb",
{
"Fn::Not": "IsCreateSlb"
}
]
}
]
},
"IsAttachment2": {
"Fn::And": [
{
"Fn::Equals": [
2,
{
"Ref": "SlbCount"
}
]
},
{
"Fn::Not": "IsCreateSlb"
}
]
},
"IsAttachment3": {
"Fn::And": [
{
"Fn::Equals": [
3,
{
"Ref": "SlbCount"
}
]
},
{
"Fn::Not": "IsCreateSlb"
}
]
},
"IsCreateSlb": {
"Fn::Equals": [
"None",
{
"Ref": "LoadBalancerIds"
}
]
},
"IsCreateVpc": {
"Fn::Equals": [
"Yes",
{
"Ref": "IsCreateVpc"
}
]
},
"NotUseVpc": {
"Fn::Equals": [
"No",
{
"Ref": "IsCreateVpc"
}
]
}
}
方案模版如下:
{
"ROSTemplateFormatVersion": "2015-09-01",
"Conditions": {
"IsAttachment1": {
"Fn::And": [
{
"Fn::Equals": [
1,
{
"Ref": "SlbCount"
}
]
},
{
"Fn::Or": [
"IsCreateSlb",
{
"Fn::Not": "IsCreateSlb"
}
]
}
]
},
"IsAttachment2": {
"Fn::And": [
{
"Fn::Equals": [
2,
{
"Ref": "SlbCount"
}
]
},
{
"Fn::Not": "IsCreateSlb"
}
]
},
"IsAttachment3": {
"Fn::And": [
{
"Fn::Equals": [
3,
{
"Ref": "SlbCount"
}
]
},
{
"Fn::Not": "IsCreateSlb"
}
]
},
"IsCreateSlb": {
"Fn::Equals": [
"None",
{
"Ref": "LoadBalancerIds"
}
]
},
"IsCreateVpc": {
"Fn::Equals": [
"Yes",
{
"Ref": "IsCreateVpc"
}
]
},
"NotUseVpc": {
"Fn::Equals": [
"No",
{
"Ref": "IsCreateVpc"
}
]
}
},
"Parameters": {
"SlbCount": {
"Type": "Number",
"Label": "Slb的数量(确认SLB数量)",
"Description": "需要添加的Slb实例的数量, 根据数量填写《Slb实例Id》。",
"Default": 1,
"AllowedValues": [
1,
2,
3
]
},
"BackendServerWeightList": {
"Type": "String",
"Label": "Slb后端服务器权重",
"Description": "Slb后端服务器权重,允许单个或多个;如指定一个, 所有的Ecs权重都是此值;多个用','(英文逗号)隔开, 多个数量需对应《需要添加到Slb中的Ecs后端服务器》, 序号顺序一一对应。 e.g. 20,30,100 ",
"Default": "100"
},
"LoadBalancerIds": {
"Type": "String",
"Label": "Slb实例Id",
"Description": "最多提供三个slb,多个slb需要存在于一个region内, 允许单个或多个, 多个用','(英文逗号)隔开, \ne.g. lb-bp183j4sfd02j02csd,lb-bid839fjn2j78bl,lb-sl39f8fvp6cbd4;\n如使用默认值(None)不指定,默认创建一个Slb实例。",
"Default": "None"
},
"IsCreateVpc": {
"Type": "String",
"Description": "是否创建Vpc,默认不创建。",
"Label": "是否创建Vpc",
"Default": "Yes",
"AllowedValues": [
"No",
"Yes"
]
},
"EcsGroupInstanceType": {
"Type": "String",
"Description": "创建ECS实例的规格,默认为ecs.n1.large, 网络为classic类型可选用ecs.n1.large 或 ecs.n1.xlarge, 网络为Vpc类型可选用ecs.c5.large 或 ecs.g5.large",
"Label": "ECS实例规格",
"Default": "ecs.c5.large",
"AllowedValues": [
"ecs.n1.large",
"ecs.n1.xlarge",
"ecs.c5.large",
"ecs.g5.large"
]
},
"MaxAmount": {
"Type": "Number",
"Description": "一次需要创建ECS实例数量,与《ECS实例名称的开始数字》可用值对应。",
"Label": "创建ECS实例数量",
"Default": 2
},
"EcsGroupImageId": {
"Type": "String",
"Description": "用于启动ECS实例的镜像ID。",
"Label": "ECS实例镜像",
"Default": "centos_7_06_64_20G_alibase_20190218.vhd",
"AssociationProperty": "ALIYUN::ECS::Instance:ImageId"
},
"EcsPassword": {
"Type": "String",
"Description": "登陆ECS实例的密码。",
"ConstraintDescription": "8-30个字符,字符,包括3种类型的大写字母、小写字母、数字或特殊字符。",
"Label": "ECS实例密码",
"AllowedPattern": "[a-zA-Z0-9-\\(\\)\\`\\~\\!@\\#\\$%\\^&\\*-+=\\|\\{\\}\\[\\]\\:\\;\\‘\\,\\.\\?\\/]*",
"MaxLength": 30,
"MinLength": 8,
"NoEcho": true,
"Confirm": true
},
"EcsSystemDiskCategory": {
"Type": "String",
"Description": "系统磁盘类型: 高效云盘(cloud_efficiency) or 普通云盘(cloud_ssd)",
"Label": "ECS实例系统磁盘类型",
"Default": "cloud_ssd",
"AllowedValues": [
"cloud_efficiency",
"cloud_ssd"
]
},
"InstanceNamePrefix": {
"Type": "String",
"Description": "ECS实例名称前缀, 必须要指定, 不能为空。最长128个字符,可包含英文、中文、数字、(_)、(.)、(-)。",
"Label": "ECS实例名称前缀",
"Default": "EcsGroup"
},
"InstanceNameBeginNumber": {
"Type": "String",
"Description": "ECS实例名称的开始数字,默认为0000,可用值分别是0000、0005、0010、0015、0020, 数字位数已经固定最长为4位。",
"Label": "ECS实例名称的开始数字",
"Default": "0"
},
"InstanceNameSuffix": {
"Type": "String",
"Description": "指定ECS实例名称后缀(可选),默认为空。",
"Label": "ECS实例名称后缀",
"Default": "Test"
}
},
"Resources": {
"Vpc": {
"Type": "ALIYUN::ECS::VPC",
"Condition": "IsCreateVpc",
"Properties": {
"VpcName": {
"Fn::Join": [
"-",
[
{
"Ref": "ALIYUN::StackName"
},
"Vpc"
]
]
},
"CidrBlock": "192.168.0.0/16",
"Description": {
"Fn::Join": [
" ",
[
"Vpc of the",
{
"Ref": "ALIYUN::StackName"
}
]
]
}
}
},
"VSwitch": {
"Type": "ALIYUN::ECS::VSwitch",
"Condition": "IsCreateVpc",
"Properties": {
"VSwitchName": {
"Fn::Join": [
"-",
[
{
"Ref": "ALIYUN::StackName"
},
"VSwitch"
]
]
},
"VpcId": {
"Fn::GetAtt": [
"Vpc",
"VpcId"
]
},
"CidrBlock": "192.168.0.0/16",
"Description": {
"Fn::Join": [
" ",
[
"VSwitch of the",
{
"Ref": "ALIYUN::StackName"
}
]
]
},
"ZoneId": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": {
"Ref": "ALIYUN::Region"
}
}
]
}
}
},
"SG": {
"Type": "ALIYUN::ECS::SecurityGroup",
"Condition": "IsCreateVpc",
"Properties": {
"VpcId": {
"Fn::GetAtt": [
"Vpc",
"VpcId"
]
},
"SecurityGroupName": {
"Fn::Join": [
"-",
[
{
"Ref": "ALIYUN::StackName"
},
"SG"
]
]
},
"SecurityGroupIngress": [
{
"PortRange": "-1/-1",
"Priority": 1,
"SourceCidrIp": "0.0.0.0/0",
"IpProtocol": "all",
"NicType": "intranet"
}
],
"SecurityGroupEgress": [
{
"PortRange": "-1/-1",
"Priority": 1,
"IpProtocol": "all",
"DestCidrIp": "0.0.0.0/0",
"NicType": "intranet"
}
]
}
},
"EcsInstanceGroupWithVpc": {
"Type": "ALIYUN::ECS::InstanceGroup",
"Condition": "IsCreateVpc",
"Properties": {
"IoOptimized": "optimized",
"ImageId": {
"Ref": "EcsGroupImageId"
},
"SecurityGroupId": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"SG",
"SecurityGroupId"
]
},
{
"Ref": "ALIYUN::NoValue"
}
]
},
"Password": {
"Ref": "EcsPassword"
},
"MinAmount": {
"Ref": "MaxAmount"
},
"SystemDiskCategory": {
"Ref": "EcsSystemDiskCategory"
},
"MaxAmount": {
"Ref": "MaxAmount"
},
"VSwitchId": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"VSwitch",
"VSwitchId"
]
},
{
"Ref": "ALIYUN::NoValue"
}
]
},
"VpcId": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"Vpc",
"VpcId"
]
},
{
"Ref": "ALIYUN::NoValue"
}
]
},
"InstanceType": {
"Ref": "EcsGroupInstanceType"
},
"InstanceName": {
"Fn::Join": [
"",
[
{
"Ref": "InstanceNamePrefix"
},
"[",
{
"Ref": "InstanceNameBeginNumber"
},
",4]",
{
"Ref": "InstanceNameSuffix"
}
]
]
},
"HostName": {
"Fn::Join": [
"",
[
"HostNameTest",
"[1,4]",
{
"Ref": "InstanceNameSuffix"
}
]
]
},
"AllocatePublicIP": {
"Fn::If": [
"IsCreateVpc",
false,
true
]
},
"UserData": {
"Fn::Join": [
"",
[
"#!/bin/sh\n",
"echo \"hello , create ecs group test\" > /tmp/create_test.log\n"
]
]
}
}
},
"EcsInstanceGroupNoVpc": {
"Type": "ALIYUN::ECS::InstanceGroup",
"Condition": "NotUseVpc",
"Properties": {
"IoOptimized": "optimized",
"ImageId": {
"Ref": "EcsGroupImageId"
},
"Password": {
"Ref": "EcsPassword"
},
"MinAmount": {
"Ref": "MaxAmount"
},
"SystemDiskCategory": {
"Ref": "EcsSystemDiskCategory"
},
"MaxAmount": {
"Ref": "MaxAmount"
},
"InstanceType": {
"Ref": "EcsGroupInstanceType"
},
"InstanceName": {
"Fn::Join": [
"",
[
{
"Ref": "InstanceNamePrefix"
},
"[",
{
"Ref": "InstanceNameBeginNumber"
},
",",
"4",
"]",
{
"Ref": "InstanceNameSuffix"
}
]
]
}
}
},
"LoadBalancer": {
"Condition": "IsCreateSlb",
"Type": "ALIYUN::SLB::LoadBalancer",
"Properties": {
"AddressType": "internet"
}
},
"BackendServerAttachment1": {
"Type": "ALIYUN::SLB::BackendServerAttachment",
"Condition": "IsAttachment1",
"Properties": {
"LoadBalancerId": {
"Fn::If": [
"IsCreateSlb",
{
"Fn::GetAtt": [
"LoadBalancer",
"LoadBalancerId"
]
},
{
"Fn::Select": [
"0",
{
"Fn::Split": [
",",
{
"Ref": "LoadBalancerIds"
}
]
}
]
}
]
},
"BackendServerList": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InstanceIds"
]
}
]
},
"BackendServerWeightList": {
"Fn::Split": [
",",
{
"Ref": "BackendServerWeightList"
}
]
}
}
},
"BackendServerAttachment2-1": {
"Type": "ALIYUN::SLB::BackendServerAttachment",
"Condition": "IsAttachment2",
"Properties": {
"LoadBalancerId": {
"Fn::Select": [
"0",
{
"Fn::Split": [
",",
{
"Ref": "LoadBalancerIds"
}
]
}
]
},
"BackendServerList": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InstanceIds"
]
}
]
},
"BackendServerWeightList": {
"Fn::Split": [
",",
{
"Ref": "BackendServerWeightList"
}
]
}
}
},
"BackendServerAttachment2-2": {
"Type": "ALIYUN::SLB::BackendServerAttachment",
"Condition": "IsAttachment2",
"Properties": {
"LoadBalancerId": {
"Fn::Select": [
"1",
{
"Fn::Split": [
",",
{
"Ref": "LoadBalancerIds"
}
]
}
]
},
"BackendServerList": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InstanceIds"
]
}
]
},
"BackendServerWeightList": {
"Fn::Split": [
",",
{
"Ref": "BackendServerWeightList"
}
]
}
}
},
"BackendServerAttachment3-1": {
"Type": "ALIYUN::SLB::BackendServerAttachment",
"Condition": "IsAttachment3",
"Properties": {
"LoadBalancerId": {
"Fn::Select": [
"0",
{
"Fn::Split": [
",",
{
"Ref": "LoadBalancerIds"
}
]
}
]
},
"BackendServerList": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InstanceIds"
]
}
]
},
"BackendServerWeightList": {
"Fn::Split": [
",",
{
"Ref": "BackendServerWeightList"
}
]
}
}
},
"BackendServerAttachment3-2": {
"Type": "ALIYUN::SLB::BackendServerAttachment",
"Condition": "IsAttachment3",
"Properties": {
"LoadBalancerId": {
"Fn::Select": [
"1",
{
"Fn::Split": [
",",
{
"Ref": "LoadBalancerIds"
}
]
}
]
},
"BackendServerList": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InstanceIds"
]
}
]
},
"BackendServerWeightList": {
"Fn::Split": [
",",
{
"Ref": "BackendServerWeightList"
}
]
}
}
},
"BackendServerAttachment3-3": {
"Type": "ALIYUN::SLB::BackendServerAttachment",
"Condition": "IsAttachment3",
"Properties": {
"LoadBalancerId": {
"Fn::Select": [
"2",
{
"Fn::Split": [
",",
{
"Ref": "LoadBalancerIds"
}
]
}
]
},
"BackendServerList": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InstanceIds"
]
}
]
},
"BackendServerWeightList": {
"Fn::Split": [
",",
{
"Ref": "BackendServerWeightList"
}
]
}
}
}
},
"Outputs": {
"EcsInstanceIds": {
"Description": "所有ECS实例的ID。",
"Value": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InstanceIds"
]
}
]
}
},
"LoadBalancerIp": {
"Condition": "IsCreateSlb",
"Description": "SLB实例的IP。",
"Value": {
"Fn::GetAtt": [
"LoadBalancer",
"IpAddress"
]
}
},
"EcsPublicIps": {
"DependsOn": "NotUseVpc",
"Description": "所有ECS实例的公网IP列表",
"Value": {
"Fn::If": [
"IsCreateVpc",
"Vpc类型的ECS实例没有公网IP",
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"PublicIps"
]
}
]
}
},
"EcsPrivateIps": {
"Description": "所有ECS实例的私网IP列表。",
"Value": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"PrivateIps"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"InnerIps"
]
}
]
}
},
"EcsHostNames": {
"Description": "所有ECS实例的主机名称列表。",
"Value": {
"Fn::If": [
"IsCreateVpc",
{
"Fn::GetAtt": [
"EcsInstanceGroupWithVpc",
"HostNames"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroupNoVpc",
"HostNames"
]
}
]
}
}
}
}