资源编排服务ROS(Resource Orchestration Service)支持Count功能,用于批量创建资源。
背景信息
ALIYUN::VPC::EIP类型用于申请弹性公网IP(EIP)。如果需要申请多个弹性公网IP,则需要在模板中写多个ALIYUN::VPC::EIP资源,这样模板会变得冗长。此时您可以使用Count功能批量创建资源。Count功能详情,请参见Count。
本文将使用Count功能,批量创建ECS实例和EIP,并为ECS实例绑定EIP。本示例将会创建以下资源:
- 1个VPC(专有网络)
- 1个VSwitch(交换机)
- 1个SecurityGroup(安全组)
- 2个ECS(按量付费的ECS)
- 2个EIP(弹性公网IP)
操作步骤
执行结果
资源栈创建后,您可以在资源栈详情页面单击资源页签,查看经过ROS预处理后的资源信息。
您也可以在资源栈详情页面单击模板页签,查看经过ROS预处理后的模板信息。
{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"Count": {
"Default": 2,
"Type": "Number"
},
"Password": {
"Default": "Abc12345",
"NoEcho": true,
"Type": "String"
},
"InstanceType": {
"Default": "ecs.c6.large",
"Type": "String"
},
"ZoneId": {
"Type": "String"
}
},
"Resources": {
"Vpc": {
"Type": "ALIYUN::ECS::VPC",
"Properties": {
"VpcName": "test-resource-count",
"CidrBlock": "10.0.0.0/8"
}
},
"VSwitch": {
"Type": "ALIYUN::ECS::VSwitch",
"Properties": {
"VpcId": {
"Ref": "Vpc"
},
"CidrBlock": "10.0.10.0/24",
"ZoneId": {
"Ref": "ZoneId"
}
}
},
"SecurityGroup": {
"Type": "ALIYUN::ECS::SecurityGroup",
"Properties": {
"VpcId": {
"Ref": "Vpc"
},
"SecurityGroupName": "test-resource-count"
}
},
"Eip[0]": {
"Type": "ALIYUN::VPC::EIP",
"Properties": {
"Bandwidth": 5
}
},
"Eip[1]": {
"Type": "ALIYUN::VPC::EIP",
"Properties": {
"Bandwidth": 5
}
},
"Servers": {
"Type": "ALIYUN::ECS::InstanceGroup",
"Properties": {
"VpcId": {
"Ref": "Vpc"
},
"MinAmount": {
"Ref": "Count"
},
"SecurityGroupId": {
"Ref": "SecurityGroup"
},
"ImageId": "centos_7",
"AllocatePublicIP": false,
"VSwitchId": {
"Ref": "VSwitch"
},
"Password": {
"Ref": "Password"
},
"InstanceType": {
"Ref": "InstanceType"
},
"MaxAmount": {
"Ref": "Count"
}
}
},
"EipBind[0]": {
"Type": "ALIYUN::VPC::EIPAssociation",
"Properties": {
"InstanceId": {
"Fn::Select": [
0,
{
"Fn::GetAtt": [
"Servers",
"InstanceIds"
]
}
]
},
"AllocationId": {
"Ref": "Eip[0]"
}
}
},
"EipBind[1]": {
"Type": "ALIYUN::VPC::EIPAssociation",
"Properties": {
"InstanceId": {
"Fn::Select": [
1,
{
"Fn::GetAtt": [
"Servers",
"InstanceIds"
]
}
]
},
"AllocationId": {
"Ref": "Eip[1]"
}
}
}
},
"Outputs": {
"AllocationIds": {
"Value": [
{
"Ref": "Eip[0]"
},
{
"Ref": "Eip[1]"
}
]
},
"InstanceIds": {
"Value": {
"Fn::GetAtt": [
"Servers",
"InstanceIds"
]
}
},
"EipAddresses": {
"Value": [
{
"Fn::GetAtt": [
"Eip[0]",
"EipAddress"
]
},
{
"Fn::GetAtt": [
"Eip[1]",
"EipAddress"
]
}
]
}
}
}