本文以創建ECS實例組并掛載到傳統型負載均衡(CLB)實例為例,由簡入難的向您介紹如何編輯ROS模板。
前提條件
請您提前了解模板語法和結構。更多信息,請參見模板快速入門。
場景示例
在阿里云專有網絡中創建ECS實例組,并在ECS實例組中部署Nginx服務,然后將創建的ECS實例組掛載到CLB實例上。
使用須知
您可以訪問對應的資源類型查看屬性詳情。具體操作,請參見查看資源類型。
資源類型為每個屬性定義了類型、是否必須、是否允許更新等信息。如果為必須,則要求必須在模板Resources的Properties中聲明該屬性;反之,則為非必須。如果為允許更新,則可以在新模板中修改該屬性,然后使用修改后的模板更新資源棧以達到更新云資源屬性的目的;反之,則不允許更新。
編輯模板
您可以通過資源類型索引文檔查找所需的資源類型。更多信息,請參見資源類型索引。
例如:當前場景中需要創建專有網絡(ALIYUN::ECS::VPC)、ECS實例組(ALIYUN::ECS::InstanceGroup)、負載均衡實例(ALIYUN::SLB::LoadBalancer)、負載均衡監聽(ALIYUN::SLB::Listener)、彈性公網IP(ALIYUN::VPC::EIP),交換機(ALIYUN::ECS::VSwitch)、安全組(ALIYUN::ECS::InstanceGroup)。在創建ECS實例組時,需要使用ALIYUN::ECS::InstanceGroup中的UserData參數執行數據初始化命令,并使用等待信號的資源(ALIYUN::ROS::WaitCondition)和接受信號的資源(ALIYUN::ROS::WaitConditionHandle)控制ECS實例組內部的執行流程。
根據以上信息,您可以在模板中定義需要創建的資源(Resources)。
定義模板資源及其依賴關系
定義基礎網絡資源
您可以通過模板定義基礎網絡資源Vpc
、VSwitch
、EcsSecurityGroup
。
使用Ref與偽參數ALIYUN::StackName獲取資源棧名稱作為資源屬性的屬性值。例如:
Vpc
中的VpcName
和VSwitch
中的VSwitchName
。更多信息,請參見函數(Functions)和ALIYUN::StackName。使用Ref獲取返回指定參數的值。例如:
VSwitch
中的Ref: Vpc,獲取的是Vpc
資源的返回值VpcId
。一般返回指定參數的值都為資源ID。更多信息,請參見函數(Functions)。
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
Properties:
CidrBlock: 192.168.0.0/16
VpcName:
Ref: ALIYUN::StackName
VSwitch:
Type: ALIYUN::ECS::VSwitch
Properties:
VSwitchName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
ZoneId:
Ref: ZoneId
CidrBlock: 192.168.0.0/24
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
SecurityGroupIngress:
- PortRange: 80/80
Priority: 1
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: internet
SecurityGroupEgress:
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: internet
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: intranet
定義負載均衡實例和彈性公網資源
您可以通過模板定義負載均衡實例Slb
、負載均衡監聽SlbListener
和彈性公網IP資源EipSlbAddress
。
使用Fn::Sub與偽參數ALIYUN::StackName獲取資源棧名稱進行拼接成新的字符串作為資源屬性的屬性值。更多信息,請參見ALIYUN::StackName。
Resources:
Slb:
Type: ALIYUN::SLB::LoadBalancer
Properties:
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
LoadBalancerName:
Fn::Sub: slb-${ALIYUN::StackName}
PayType:
Ref: PayType
PricingCycle:
Ref: PayPeriodUnit
Duration:
Ref: PayPeriod
AddressType: intranet
LoadBalancerSpec:
Ref: LoadBalancerSpec
AutoPay: true
SlbListener:
DependsOn:
- Slb
Type: ALIYUN::SLB::Listener
Properties:
Persistence:
CookieTimeout: 60
StickySession: 'on'
PersistenceTimeout: 180
XForwardedFor: 'off'
StickySessionType: insert
ListenerPort: 80
Bandwidth: -1
HealthCheck:
HttpCode: http_2xx,http_3xx,http_4xx,http_5xx
HealthCheckType: tcp
UnhealthyThreshold: 3
Timeout: 5
HealthyThreshold: 3
Port: 80
URI: /
Interval: 2
LoadBalancerId:
Ref: Slb
BackendServerPort: 80
Protocol: tcp
EipSlbAddress:
Type: ALIYUN::VPC::EIP
Properties:
Name:
Ref: ALIYUN::StackName
InternetChargeType: PayByTraffic
Bandwidth:
Ref: Bandwidth
定義ECS實例組、等待信號資源和接收信號資源
您可以通過模板定義ECS實例組EcsInstanceGroup
、等待信號資源WaitCondition
和接收信號資源WaitConditionHandle
。
使用Fn::GetAtt函數獲取返回模板中資源的屬性值。例如:
WaitConditionHandle
的返回值CurlCli
。更多信息,請參見函數(Functions)。使用Fn::Sub函數用于拼接命令操作字符串及將輸入字符串中的變量替換為您指定的值。例如:
EcsInstanceGroup
中的UserData,使用CurlCli替換${CurlCli}。更多信息,請參見函數(Functions)。控制ECS實例內部初始化的資源組合如下。
創建一個等待信號資源(ALIYUN::ROS::WaitCondition)。
創建一個接收信號資源(ALIYUN::ROS::WaitConditionHandle)。
在ECS內部UserData中執行腳本并發送信息。例如:
EcsInstanceGroup
中的UserData內容,UserData中可以傳入ECS的初始化腳本,最后執行成功回調WaitConditionHandle
資源并結束WaitCondition
的等待。
Resources:
WaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Count: 1
Handle:
Ref: WaitConditionHandle
Timeout: 300
WaitConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
EcsInstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
Properties:
InstanceChargeType:
Ref: PayType
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
IoOptimized: optimized
SystemDiskCategory:
Ref: SystemDiskCategory
SystemDiskSize:
Ref: SystemDiskSize
DiskMappings:
- Category:
Ref: DataDiskCategory
Size:
Ref: DataDiskSize
VpcId:
Ref: Vpc
SecurityGroupId:
Ref: EcsSecurityGroup
VSwitchId:
Ref: VSwitch
MaxAmount: 2
ImageId: centos_7
InstanceType:
Ref: EcsInstanceType
Password:
Ref: InstancePassword
AllocatePublicIP: false
UserData:
Fn::Sub:
- |
#!/bin/bash
# 掛盤到/disk1
cat >> /root/InitDataDisk.sh << EOF
#!/bin/bash
echo "p
n
p
w
" | fdisk -u /dev/vdb
EOF
/bin/bash /root/InitDataDisk.sh
rm -f /root/InitDataDisk.sh
rm -f InitDataDisk.sh
mkfs -t ext4 /dev/vdb1
cp /etc/fstab /etc/fstab.bak
mkdir /disk1
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\\\"http://g'` /disk1 ext4 defaults 0 0 >> /etc/fstab
mount -a
# 這里配置安裝腳本
yum install -y nginx
# 配置啟動腳本
systemctl start nginx.service
# 執行成功回調WaitConditionHandle,結束WaitCondition的等待。
${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
- CurlCli:
Fn::GetAtt:
- WaitConditionHandle
- CurlCli
定義ECS實例組、負載均衡實例和添加后端服務器依賴關系
您可以通過模板定義ECS實例組EcsInstanceGroup
、負載均衡Slb
和SlbBackendServerAttachment
。
通過
SlbBackendServerAttachment
將ECS實例組掛載到CLB實例上。更多信息,請參見ALIYUN::SLB::BackendServerAttachment。在ALIYUN::SLB::BackendServerAttachment中,如果不指定
BackendServerWeightList
的值,則BackendServerList
中所有ECS實例權重都是100;當BackendServerWeightList
長度小于BackendServerList
時,則使用BackendServerWeightList
中的最后一個值配置BackendServerList
中剩余ECS實例的權重。設置DependsOn屬性可以指定資源緊跟在另一個資源后創建。為某個資源添加DependsOn屬性后,該資源僅在DependsOn屬性中指定的資源后創建。更多信息,請參見DependsOn。
使用Fn::GetAtt函數獲取資源輸出屬性值。例如:
EcsInstanceGroup
中的InstanceIds
。更多信息,請參見函數(Functions)。
Resources:
EcsInstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
Properties:
InstanceChargeType:
Ref: PayType
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
IoOptimized: optimized
SystemDiskCategory:
Ref: SystemDiskCategory
SystemDiskSize:
Ref: SystemDiskSize
DiskMappings:
- Category:
Ref: DataDiskCategory
Size:
Ref: DataDiskSize
VpcId:
Ref: Vpc
SecurityGroupId:
Ref: EcsSecurityGroup
VSwitchId:
Ref: VSwitch
MaxAmount: 2
ImageId: centos_7
InstanceType:
Ref: EcsInstanceType
Password:
Ref: InstancePassword
AllocatePublicIP: false
Slb:
Type: ALIYUN::SLB::LoadBalancer
Properties:
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
LoadBalancerName:
Fn::Sub: slb-${ALIYUN::StackName}
PayType:
Ref: PayType
PricingCycle:
Ref: PayPeriodUnit
Duration:
Ref: PayPeriod
AddressType: intranet
LoadBalancerSpec:
Ref: LoadBalancerSpec
AutoPay: true
SlbBackendServerAttachment:
DependsOn:
- EcsInstanceGroup
- Slb
Type: ALIYUN::SLB::BackendServerAttachment
Properties:
BackendServerList:
Fn::GetAtt:
- EcsInstanceGroup
- InstanceIds
LoadBalancerId:
Ref: Slb
BackendServerWeightList:
- 100
- 50
完整模板示例
ROSTemplateFormatVersion: '2015-09-01'
Description:
en: Create new VPC and vSWitch, create one CLB, two ECS instances, and bind all ECS instances to the CLB.
zh-cn: 新建VPC、vSWitch,創建1個CLB、2個ECS實例,并將所有ECS實例綁定到CLB上。
Parameters:
ZoneId:
Type: String
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
Label:
en: vSwitch Availability Zone
zh-cn: 交換機可用區
VpcCidrBlock:
Default: 192.168.0.0/16
Label:
zh-cn: 專有網絡網段
en: VPC CIDR Block
Type: String
Description:
zh-cn: 新建專有網絡IP地址段范圍,推薦使用以下的IP地址段<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
en: New proprietary network IP address segment range, recommended use of the following IP address segments<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
VSwitchCidrBlock:
Default: 192.168.0.0/24
Type: String
Description:
zh-cn: 必須是所屬專有網絡的子網段,并且沒有被其他交換機占用。
en: Must be a sub-network segment of the proprietary network and is not occupied by other vSwitches.
Label:
zh-cn: 交換機網段
en: vSwitch CIDR Block
EcsInstanceType:
Type: String
Label:
en: Instance Type
zh-cn: 實例類型
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceChargeType: ${InstanceChargeType}
SystemDiskCategory:
Type: String
Description:
en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD Cloud Disk</font>]'
zh-cn: '<font color=''blue''><b>可選值:</b></font><br>[cloud_efficiency: <font color=''green''>高效云盤</font>]<br>[cloud_ssd: <font color=''green''>SSD云盤</font>]<br>[cloud_essd: <font color=''green''>ESSD云盤</font>]<br>[cloud: <font color=''green''>普通云盤</font>]<br>[ephemeral_ssd: <font color=''green''>本地SSD盤</font>]'
AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceType: ${EcsInstanceType}
Label:
en: System Disk Type
zh-cn: 系統盤類型
SystemDiskSize:
Default: 40
Type: Number
Description:
zh-cn: 系統盤大小, 取值范圍:[40, 500], 單位:GB。
Label:
zh-cn: 系統盤空間
en: System Disk Space
DataDiskCategory:
AssociationProperty: ALIYUN::ECS::Disk::DataDiskCategory
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceType: ${EcsInstanceType}
Type: String
Description:
zh-cn: '<font color=''blue''><b>可選值:</b></font><br>[cloud_efficiency: <font color=''green''>高效云盤</font>]<br>[cloud_ssd: <font color=''green''>SSD云盤</font>]<br>[cloud_essd: <font color=''green''>ESSD云盤</font>]<br>[cloud: <font color=''green''>普通云盤</font>]'
en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]'
Label:
zh-cn: 數據盤類型
en: Data Disk Type
DataDiskSize:
Description:
zh-cn: ECS實例數據盤大小,單位為GiB。取值范圍:20~32768。
Default: 100
MaxValue: 32768
MinValue: 20
Label:
zh-cn: 數據盤空間
en: Data Disk Space
Type: Number
InstancePassword:
NoEcho: true
Type: String
Description:
en: Server login password, Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).。
zh-cn: 服務器登錄密碼,長度8~30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)。
AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
Label:
en: Instance Password
zh-cn: 實例密碼
ConstraintDescription:
en: Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
zh-cn: 長度8~30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)。
MinLength: 8
MaxLength: 30
AssociationProperty: ALIYUN::ECS::Instance::Password
PayType:
Type: String
Label:
en: ECS Instance Charge Type
zh-cn: 付費類型
Default: PostPaid
AllowedValues:
- PostPaid
- PrePaid
AssociationProperty: ChargeType
AssociationPropertyMetadata:
LocaleKey: InstanceChargeType
PayPeriodUnit:
Type: String
Description:
en: The long cycle of purchasing resources. <br>Week is a Week and Month is a Month. <br> The default value is month.<br><b><font color='red'> When ECS instance types are PrePaid valid.</b></font>
zh-cn: 購買資源時長的周期。<br>Week為周,Month為月<br>默認值為月<br><b><font color='red'>當ECS實例類型為PrePaid有效。</b></font>
Label:
en: Pay Period Unit
zh-cn: 購買資源時長周期
Default: Month
AllowedValues:
- Month
- Year
AssociationProperty: PayPeriodUnit
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${PayType}
- PostPaid
PayPeriod:
Type: Number
Label:
en: Period
zh-cn: 購買資源時長
Default: 1
AllowedValues:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
AssociationProperty: PayPeriod
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${PayType}
- PostPaid
LoadBalancerSpec:
Type: String
AssociationProperty: ALIYUN::SLB::Instance::InstanceType
Label:
en: LoadBalancer Specifications
zh-cn: 負載均衡實例規格
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
Bandwidth:
Description:
zh-cn: 取值范圍:[0, 1000],單位:Mbps。
en: 'Value range: [0, 1000], Unit: Mbps.'
Default: 10
MaxValue: 1000
Label:
zh-cn: 負載均衡的彈性公網IP帶寬
en: CLB Elastic IP Bandwidth
MinValue: 1
Type: Number
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
Properties:
CidrBlock:
Ref: VpcCidrBlock
VpcName:
Ref: ALIYUN::StackName
VSwitch:
Type: ALIYUN::ECS::VSwitch
Properties:
VSwitchName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
ZoneId:
Ref: ZoneId
CidrBlock:
Ref: VSwitchCidrBlock
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
SecurityGroupIngress:
- PortRange: 80/80
Priority: 1
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: internet
SecurityGroupEgress:
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: internet
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: intranet
WaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Count: 1
Handle:
Ref: WaitConditionHandle
Timeout: 300
WaitConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
EcsInstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
Properties:
InstanceChargeType:
Ref: PayType
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
IoOptimized: optimized
SystemDiskCategory:
Ref: SystemDiskCategory
SystemDiskSize:
Ref: SystemDiskSize
DiskMappings:
- Category:
Ref: DataDiskCategory
Size:
Ref: DataDiskSize
VpcId:
Ref: Vpc
SecurityGroupId:
Ref: EcsSecurityGroup
VSwitchId:
Ref: VSwitch
MaxAmount: 2
ImageId: centos_7
InstanceType:
Ref: EcsInstanceType
Password:
Ref: InstancePassword
AllocatePublicIP: false
UserData:
Fn::Sub:
- |
#!/bin/bash
# 掛盤到/disk1
cat >> /root/InitDataDisk.sh << EOF
#!/bin/bash
echo "p
n
p
w
" | fdisk -u /dev/vdb
EOF
/bin/bash /root/InitDataDisk.sh
rm -f /root/InitDataDisk.sh
rm -f InitDataDisk.sh
mkfs -t ext4 /dev/vdb1
cp /etc/fstab /etc/fstab.bak
mkdir /disk1
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\\\"http://g'` /disk1 ext4 defaults 0 0 >> /etc/fstab
mount -a
# 這里配置安裝腳本
yum install -y nginx
# 配置啟動腳本
systemctl start nginx.service
# 執行成功回調WaitConditionHandle,結束WaitCondition的等待。
${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
- CurlCli:
Fn::GetAtt:
- WaitConditionHandle
- CurlCli
Slb:
Type: ALIYUN::SLB::LoadBalancer
Properties:
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
LoadBalancerName:
Fn::Sub: slb-${ALIYUN::StackName}
PayType:
Ref: PayType
PricingCycle:
Ref: PayPeriodUnit
Duration:
Ref: PayPeriod
AddressType: intranet
LoadBalancerSpec:
Ref: LoadBalancerSpec
AutoPay: true
EipSlbAddress:
Type: ALIYUN::VPC::EIP
Properties:
Name:
Ref: ALIYUN::StackName
InternetChargeType: PayByTraffic
Bandwidth:
Ref: Bandwidth
EipSlbAddressAssociation:
Type: ALIYUN::VPC::EIPAssociation
Properties:
InstanceId:
Ref: Slb
AllocationId:
Ref: EipSlbAddress
SlbBackendServerAttachment:
DependsOn:
- EcsInstanceGroup
Type: ALIYUN::SLB::BackendServerAttachment
Properties:
BackendServerList:
Fn::GetAtt:
- EcsInstanceGroup
- InstanceIds
LoadBalancerId:
Ref: Slb
BackendServerWeightList:
- 100
- 100
SlbListener:
DependsOn:
- Slb
Type: ALIYUN::SLB::Listener
Properties:
Persistence:
CookieTimeout: 60
StickySession: 'on'
PersistenceTimeout: 180
XForwardedFor: 'off'
StickySessionType: insert
ListenerPort: 80
Bandwidth: -1
HealthCheck:
HttpCode: http_2xx,http_3xx,http_4xx,http_5xx
HealthCheckType: tcp
UnhealthyThreshold: 3
Timeout: 5
HealthyThreshold: 3
Port: 80
URI: /
Interval: 2
LoadBalancerId:
Ref: Slb
BackendServerPort: 80
Protocol: tcp
Outputs:
Endpoint:
Description:
zh-cn: 對外暴露的公網IP地址。
en: Public IP Addresses.
Value:
Fn::Sub:
- http://${ServerAddress}
- ServerAddress:
Fn::GetAtt:
- EipSlbAddress
- EipAddress
添加模板參數分組與動態獲取參數配置
在以上模板中您完成了對多種資源及其依賴關系的定義,此時模板中EcsInstanceGroup
資源的InstanceType
、SystemDiskCategory
屬性值與Slb
資源的LoadBalancerSpec
屬性值為引用參數。當您在不同地域創建資源棧時,需要多次調整模板變更資源屬性值以達到部署資源棧的目的。
您可以對模板添加參數Parameters,從而提高模板的靈活性和可復用性。
添加模板參數分組
您可以在模板中使用元數據(Metadata)對Parameters中定義的參數進行分組,并定義參數分組標簽。
當您完成對模板資源和參數的定義后,可以按照不同資源和資源對應的參數進行分組。
以當前模板為例,您可以將資源按如下結果劃分。
資源參數分類 | 資源名稱 | 參數名稱 |
基礎網絡配置 |
|
|
負載均衡配置 |
|
|
云服務器ECS配置 |
|
|
動態獲取參數配置
以ECSInstanceType
參數為例,當你需要在ROS控制臺上對參數設置篩選條件并動態選擇參數配置時,可以根據參數對應的資源類型(ALIYUN::ECS::InstanceGroup)在AssociationProperty和AssociationPropertyMetadata文檔中查詢到該參數支持的AssociationProperty取值(ALIYUN::ECS::Instance::InstanceType),然后對篩選到的AssociationProperty查詢過濾條件為ZoneId
和InstanceChargeType
的AssociationPropertyMetadata取值。更多信息,請參見AssociationProperty和AssociationPropertyMetadata。
完整模板示例
ROSTemplateFormatVersion: '2015-09-01'
Description:
en: Create new VPC and vSWitch,, create one CLB, two ECS instances, and bind all ECS instances to the CLB.
zh-cn: 新建已有VPC、vSWitch,創建1個CLB、2個ECS實例,并將所有ECS實例綁定到CLB上。
Parameters:
ZoneId:
Type: String
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
Label:
en: vSwitch Availability Zone
zh-cn: 交換機可用區
VpcCidrBlock:
Default: 192.168.0.0/16
Label:
zh-cn: 專有網絡網段
en: VPC CIDR Block
Type: String
Description:
zh-cn: 新建專有網絡IP地址段范圍,推薦使用以下的IP地址段<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
en: New proprietary network IP address segment range, recommended use of the following IP address segments<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
VSwitchCidrBlock:
Default: 192.168.0.0/24
Type: String
Description:
zh-cn: 必須是所屬專有網絡的子網段,并且沒有被其他交換機占用。
en: Must be a sub-network segment of the proprietary network and is not occupied by other vSwitches.
Label:
zh-cn: 交換機網段
en: vSwitch CIDR Block
EcsInstanceType:
Type: String
Label:
en: Instance Type
zh-cn: 實例類型
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceChargeType: ${InstanceChargeType}
SystemDiskCategory:
Type: String
Description:
en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD Cloud Disk</font>]'
zh-cn: '<font color=''blue''><b>可選值:</b></font><br>[cloud_efficiency: <font color=''green''>高效云盤</font>]<br>[cloud_ssd: <font color=''green''>SSD云盤</font>]<br>[cloud_essd: <font color=''green''>ESSD云盤</font>]<br>[cloud: <font color=''green''>普通云盤</font>]<br>[ephemeral_ssd: <font color=''green''>本地SSD盤</font>]'
AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceType: ${EcsInstanceType}
Label:
en: System Disk Type
zh-cn: 系統盤類型
SystemDiskSize:
Default: 40
Type: Number
Description:
zh-cn: 系統盤大小, 取值范圍:[40, 500], 單位:GB。
en: 'System disk size, range of values: 40-500, units: GB.'
Label:
zh-cn: 系統盤空間
en: System Disk Space
DataDiskCategory:
AssociationProperty: ALIYUN::ECS::Disk::DataDiskCategory
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceType: ${EcsInstanceType}
Type: String
Description:
zh-cn: '<font color=''blue''><b>可選值:</b></font><br>[cloud_efficiency: <font color=''green''>高效云盤</font>]<br>[cloud_ssd: <font color=''green''>SSD云盤</font>]<br>[cloud_essd: <font color=''green''>ESSD云盤</font>]<br>[cloud: <font color=''green''>普通云盤</font>]'
en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]'
Label:
zh-cn: 數據盤盤類型
en: Data Disk Type
DataDiskSize:
Description:
zh-cn: ECS實例數據盤大小,單位為GiB。取值范圍:20~32768。
en: 'ECS Instance disk size, range of values: 20-32768, units: GB.'
Default: 100
MaxValue: 32768
MinValue: 20
Label:
zh-cn: 數據盤空間
en: Data Disk Space
Type: Number
InstancePassword:
NoEcho: true
Type: String
Description:
en: Server login password, Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
zh-cn: 服務器登錄密碼,長度8~30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)。
AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
Label:
en: Instance Password
zh-cn: 實例密碼
ConstraintDescription:
en: Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
zh-cn: 長度8~30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)。
MinLength: 8
MaxLength: 30
AssociationProperty: ALIYUN::ECS::Instance::Password
PayType:
Type: String
Label:
en: ECS Instance Charge Type
zh-cn: 付費類型
Default: PostPaid
AllowedValues:
- PostPaid
- PrePaid
AssociationProperty: ChargeType
AssociationPropertyMetadata:
LocaleKey: InstanceChargeType
PayPeriodUnit:
Type: String
Description:
en: The long cycle of purchasing resources. <br>Week is a Week and Month is a Month. <br> The default value is month.<br><b><font color='red'> When ECS instance types are PrePaid valid.</b></font>
zh-cn: 購買資源時長的周期。<br>Week為周,Month為月<br>默認值為月<br><b><font color='red'>當ECS實例類型為PrePaid有效。</b></font>
Label:
en: Pay Period Unit
zh-cn: 購買資源時長周期
Default: Month
AllowedValues:
- Month
- Year
AssociationProperty: PayPeriodUnit
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${PayType}
- PostPaid
PayPeriod:
Type: Number
Label:
en: Period
zh-cn: 購買資源時長
Default: 1
AllowedValues:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
AssociationProperty: PayPeriod
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${PayType}
- PostPaid
LoadBalancerSpec:
Type: String
AssociationProperty: ALIYUN::SLB::Instance::InstanceType
Label:
en: LoadBalancer Specifications
zh-cn: 負載均衡實例規格
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
Bandwidth:
Description:
zh-cn: 取值范圍:[0, 1000],單位:Mbps。
en: 'Value range: [0, 1000], Unit: Mbps.'
Default: 10
MaxValue: 1000
Label:
zh-cn: 負載均衡的彈性公網IP帶寬
en: CLB Elastic IP Bandwidth
MinValue: 1
Type: Number
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
Properties:
CidrBlock:
Ref: VpcCidrBlock
VpcName:
Ref: ALIYUN::StackName
VSwitch:
Type: ALIYUN::ECS::VSwitch
Properties:
VSwitchName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
ZoneId:
Ref: ZoneId
CidrBlock:
Ref: VSwitchCidrBlock
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
SecurityGroupIngress:
- PortRange: 80/80
Priority: 1
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: internet
SecurityGroupEgress:
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: internet
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: intranet
WaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Count: 1
Handle:
Ref: WaitConditionHandle
Timeout: 300
WaitConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
EcsInstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
Properties:
InstanceChargeType:
Ref: PayType
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
IoOptimized: optimized
SystemDiskCategory:
Ref: SystemDiskCategory
SystemDiskSize:
Ref: SystemDiskSize
DiskMappings:
- Category:
Ref: DataDiskCategory
Size:
Ref: DataDiskSize
VpcId:
Ref: Vpc
SecurityGroupId:
Ref: EcsSecurityGroup
VSwitchId:
Ref: VSwitch
MaxAmount: 2
ImageId: centos_7
InstanceType:
Ref: EcsInstanceType
Password:
Ref: InstancePassword
AllocatePublicIP: false
UserData:
Fn::Sub:
- |
#!/bin/bash
# 掛盤到/disk1
cat >> /root/InitDataDisk.sh << EOF
#!/bin/bash
echo "p
n
p
w
" | fdisk -u /dev/vdb
EOF
/bin/bash /root/InitDataDisk.sh
rm -f /root/InitDataDisk.sh
rm -f InitDataDisk.sh
mkfs -t ext4 /dev/vdb1
cp /etc/fstab /etc/fstab.bak
mkdir /disk1
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\\\"http://g'` /disk1 ext4 defaults 0 0 >> /etc/fstab
mount -a
# 這里配置安裝腳本
yum install -y nginx
# 配置啟動腳本
systemctl start nginx.service
# 執行成功回調WaitConditionHandle,結束WaitCondition的等待。
${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
- CurlCli:
Fn::GetAtt:
- WaitConditionHandle
- CurlCli
Slb:
Type: ALIYUN::SLB::LoadBalancer
Properties:
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
LoadBalancerName:
Fn::Sub: slb-${ALIYUN::StackName}
PayType:
Ref: PayType
PricingCycle:
Ref: PayPeriodUnit
Duration:
Ref: PayPeriod
AddressType: intranet
LoadBalancerSpec:
Ref: LoadBalancerSpec
AutoPay: true
EipSlbAddress:
Type: ALIYUN::VPC::EIP
Properties:
Name:
Ref: ALIYUN::StackName
InternetChargeType: PayByTraffic
Bandwidth:
Ref: Bandwidth
EipSlbAddressAssociation:
Type: ALIYUN::VPC::EIPAssociation
Properties:
InstanceId:
Ref: Slb
AllocationId:
Ref: EipSlbAddress
SlbBackendServerAttachment:
DependsOn:
- EcsInstanceGroup
Type: ALIYUN::SLB::BackendServerAttachment
Properties:
BackendServerList:
Fn::GetAtt:
- EcsInstanceGroup
- InstanceIds
LoadBalancerId:
Ref: Slb
BackendServerWeightList:
- 100
- 50
SlbListener:
DependsOn:
- Slb
Type: ALIYUN::SLB::Listener
Properties:
Persistence:
CookieTimeout: 60
StickySession: 'on'
PersistenceTimeout: 180
XForwardedFor: 'off'
StickySessionType: insert
ListenerPort: 80
Bandwidth: -1
HealthCheck:
HttpCode: http_2xx,http_3xx,http_4xx,http_5xx
HealthCheckType: tcp
UnhealthyThreshold: 3
Timeout: 5
HealthyThreshold: 3
Port: 80
URI: /
Interval: 2
LoadBalancerId:
Ref: Slb
BackendServerPort: 80
Protocol: tcp
Outputs:
Endpoint:
Description:
zh-cn: 對外暴露的公網IP地址
en: Public IP Addresses
Value:
Fn::Sub:
- http://${ServerAddress}
- ServerAddress:
Fn::GetAtt:
- EipSlbAddress
- EipAddress
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- ZoneId
- VpcCidrBlock
- VSwitchCidrBlock
Label:
default:
zh-cn: 基礎網絡配置
en: Basic Network Configuration
- Parameters:
- PayType
- PayPeriodUnit
- PayPeriod
Label:
default:
en: PayType Configuration
zh-cn: 付費類型
- Parameters:
- EcsInstanceType
- SystemDiskCategory
- SystemDiskSize
- DataDiskCategory
- DataDiskSize
- InstancePassword
Label:
default:
en: Instance Configure
zh-cn: ECS實例配置
- Parameters:
- LoadBalancerSpec
- Bandwidth
Label:
default:
en: CLB Configure
zh-cn: 負載均衡配置