本文以使用Elastic Compute Service串連雲資料庫RDS完成資料初始化為例,由簡入難地向您介紹如何編輯ROS模板。
前提條件
請您提前瞭解模板文法和結構。更多資訊,請參見模板快速入門。
情境樣本
在阿里雲專用網路中建立ECS執行個體和RDS執行個體,並在Elastic Compute Service中擷取資料庫的串連資訊,從而完成資料初始化。
使用須知
您可以訪問對應的資源類型查看屬性詳情。具體操作,請參見查看資源類型。
資源類型為每個屬性定義了類型、是否必須、是否允許更新等資訊。如果為必須,則要求必須在模板Resources的Properties中聲明該屬性;反之,則為非必須。如果為允許更新,則可以在新模板中修改該屬性,然後使用修改後的模板更新資源棧以達到更新雲資源屬性的目的;反之,則不允許更新。
編輯模板
您可以通過資源類型索引文檔尋找所需的資源類型。更多資訊,請參見資源類型索引。
例如:當前情境中需要建立專用網路(ALIYUN::ECS::VPC)、ECS執行個體(ALIYUN::ECS::Instance)、RDS執行個體(ALIYUN::RDS::DBInstance),還需要建立ECS執行個體所使用的交換器(ALIYUN::ECS::VSwitch)、安全性群組(ALIYUN::ECS::SecurityGroup)和執行資料初始化命令的ECS雲助手(ALIYUN::ECS::RunCommand)。
根據以上資訊,您可以在模板中定義需要建立的資源(Resources)。
定義模板資源及其依賴關係
定義基礎網路資源
您可以通過模板定義基礎網路資源Vpc
、VSwitch
和EcsSecurityGroup
。
使用Ref與偽參數ALIYUN::StackName擷取資源棧名稱作為資源屬性的屬性值,例如
Vpc
中的VpcName
和VSwitch
中的VSwitchName
。更多資訊,請參見Ref和ALIYUN::StackName。使用Fn::Select與Fn::GetAZs函數結合偽參數ALIYUN::Region擷取資源棧所在地區的第一個可用性區域ID,例如
VSwitch
中的ZoneId
。更多資訊,請參見函數(Functions)和ALIYUN::Region。
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:
Fn::Select:
- '0'
- Fn::GetAZs:
Ref: ALIYUN::Region
CidrBlock: 192.168.0.0/24
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
SecurityGroupEgress:
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: intranet
定義資料庫資源
您可以通過模板定義資料庫資源DBInstance
和DBAccount
。
使用Fn::GetAtt函數擷取資源輸出屬性值,例如DBAccount
中的DBInstanceId
。更多資訊,請參見Fn::GetAtt。
Resources:
DBInstance:
Type: ALIYUN::RDS::DBInstance
Properties:
ZoneId:
Fn::Select:
- '0'
- Fn::GetAZs:
Ref: ALIYUN::Region
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
Engine: MySQL
EngineVersion: '8.0'
DBInstanceClass: mysql.n4.medium.2c
DBInstanceStorage: 10
MultiAZ: true
DBInstanceNetType: Intranet
DBMappings:
- CharacterSetName: utf8
DBName: employees
SecurityIPList: 0.0.0.0/0
DBAccount:
Type: ALIYUN::RDS::Account
DependsOn:
- DBInstance
Properties:
DBInstanceId:
Fn::GetAtt:
- DBInstance
- DBInstanceId
AccountPassword:
Ref: DBPassword
AccountType: Super
AccountName: rdsuser
定義雲端服務器ECS資源
您可以通過模板定義Elastic Compute Service資源EcsInstance
和InstanceRunCommand
。
使用Fn::Sub函數拼接命令操作字串,例如InstanceRunCommand
中的CommandContent
。更多資訊,請參見Fn::Sub。
此處使用的初始化資料為MySQL官方提供的測試資料,為了保證測試資料網路下載的穩定性,請提前將資料存放至OSS Bucket中。
Resources:
EcsInstance:
Type: ALIYUN::ECS::Instance
Properties:
VpcId:
Ref: Vpc
SecurityGroupId:
Ref: EcsSecurityGroup
VSwitchId:
Ref: VSwitch
ImageId: centos_7
AllocatePublicIP: false
InstanceType: ecs.c5.large
SystemDiskSize: 40
SystemDiskCategory: cloud_essd
Password:
Ref: EcsInstancePassword
InstanceRunCommand:
Type: ALIYUN::ECS::RunCommand
Properties:
CommandContent:
Fn::Sub:
- |
#!/bin/bash
yum -y install holland-mysqldump.noarch unzip
wget -P /tmp https://ros-userdata-resources.oss-cn-beijing.aliyuncs.com/MySQL/test_db-master.zip
unzip /tmp/test_db-master.zip -d /tmp/
mysql -h${DBConnectString} -p3306 -urdsuser -p${DBPassword} < /tmp/test_db-master/employees.sql
- DBConnectString:
Fn::GetAtt:
- DBInstance
- InnerConnectionString
DBPassword:
Ref: DBPassword
Type: RunShellScript
InstanceIds:
- Fn::GetAtt:
- EcsInstance
- InstanceId
Timeout: '300'
完整模板樣本
ROSTemplateFormatVersion: '2015-09-01'
Description: { }
Parameters:
EcsInstancePassword:
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
DBPassword:
NoEcho: true
Type: String
Label:
en: DB Password
zh-cn: 資料庫使用者訪問密碼
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:
Fn::Select:
- '0'
- Fn::GetAZs:
Ref: ALIYUN::Region
CidrBlock: 192.168.0.0/24
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
SecurityGroupEgress:
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: intranet
DBInstance:
Type: ALIYUN::RDS::DBInstance
Properties:
ZoneId:
Fn::Select:
- '0'
- Fn::GetAZs:
Ref: ALIYUN::Region
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
Engine: MySQL
EngineVersion: '8.0'
DBInstanceClass: mysql.n4.medium.2c
DBInstanceStorage: 10
MultiAZ: true
DBInstanceNetType: Intranet
DBMappings:
- CharacterSetName: utf8
DBName: employees
SecurityIPList: 0.0.0.0/0
EcsInstance:
Type: ALIYUN::ECS::Instance
Properties:
VpcId:
Ref: Vpc
SecurityGroupId:
Ref: EcsSecurityGroup
VSwitchId:
Ref: VSwitch
ImageId: centos_7
AllocatePublicIP: false
InstanceType: ecs.c5.large
SystemDiskSize: 40
SystemDiskCategory: cloud_essd
Password:
Ref: EcsInstancePassword
Account:
Type: ALIYUN::RDS::Account
DependsOn:
- DBInstance
Properties:
DBInstanceId:
Fn::GetAtt:
- DBInstance
- DBInstanceId
AccountPassword:
Ref: DBPassword
AccountType: Super
AccountName: rdsuser
InstanceRunCommand:
Type: ALIYUN::ECS::RunCommand
DependsOn:
- Account
Properties:
CommandContent:
Fn::Sub:
- |
#!/bin/bash
yum -y install holland-mysqldump.noarch unzip
wget -P /tmp https://ros-userdata-resources.oss-cn-beijing.aliyuncs.com/MySQL/test_db-master.zip
unzip /tmp/test_db-master.zip -d /tmp/
mysql -h${DBConnectString} -p3306 -urdsuser -p${DBPassword} < /tmp/test_db-master/employees.sql
- DBConnectString:
Fn::GetAtt:
- DBInstance
- InnerConnectionString
DBPassword:
Ref: DBPassword
Type: RunShellScript
InstanceIds:
- Fn::GetAtt:
- EcsInstance
- InstanceId
Timeout: '300'
添加模板參數分組與動態擷取參數配置
在以上模板中完成了對多種資源及其依賴關係的定義,其中EcsInstance
資源的InstanceType
、SystemDiskCategory
屬性值與DBInstance
資源的DBInstanceClass
屬性值為固定值,當您在不同地區建立資源棧時,需要多次調整模板結構和變更資源屬性以達到部署資源棧的目的。
您可以對模板添加參數Parameters,從而提高模板的靈活性和可複用性。
添加模板參數分組
您可以在模板中使用中繼資料(Metadata)對Parameters中定義的參數進行分組,並定義參數分組標籤。
您可以根據不同資源以及資源對應的參數進行分組。以當前模板為例,您可以將資源按照如下結果劃分。
資源參數分類 | 資源名稱 | 參數名稱 |
基礎網路設定 |
|
|
資料庫配置 |
|
|
ECS雲端服務器配置 |
|
|
動態擷取參數配置
以ECSInstanceType
參數為例,當您需要在控制台上對參數設定篩選條件並動態選擇參數配置時,可以按照參數對應的資源類型(ALIYUN::ECS::Instance)在AssociationProperty和AssociationPropertyMetadata文檔中查詢到該參數支援的AssociationProperty
取值(ALIYUN::ECS::Instance::InstanceType),然後查看對篩選到的AssociationProperty
設定過濾條件為ZoneId
的AssociationPropertyMetadata
取值。更多資訊,請參見AssociationProperty和AssociationPropertyMetadata。
完整模板樣本
ROSTemplateFormatVersion: '2015-09-01'
Description: { }
Parameters:
VSwitchZoneId:
Type: String
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
Description:
en: Availability ID for existing switches
zh-cn: 現有交換器的可用性區域ID
Label:
en: VSwitch Zone ID
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.XX.XX/8]</font><br><font color='green'>[172.16.XX.XX/12]</font><br><font color='green'>[192.168.XX.XX/16]</font>
en: New proprietary network IP address segment range, recommended use of the following IP address segments<br><font color='green'>[10.0.XX.XX/8]</font><br><font color='green'>[172.16.XX.XX/12]</font><br><font color='green'>[192.168.XX.XX/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
Description:
en: <font color='blue'><b>1.Before selecting the model please confirm that the current available zone under the model is in stock, some models need to be reported in advance</b></font>]<br><font color='blue'><b>2.List of optional models</font>]<br></b></font>[ecs.c5.large <font color='green'>2vCPU 4GiB Intranet bandwidth1Gbps In-grid sending and receiving packages30MillionPPSS</font>]<br></b>[ecs.c5.xlarge <font color='green'>4vCPU 8GiB Intranet bandwidth1.5Gbps In-grid sending and receiving packages50MillionPPS</font>]<br></b>[ecs.c5.2xlarge <font color='green'>8vCPU 16GiB Intranet bandwidth2.5Gbps In-grid sending and receiving packages80MillionPPS</font>]
zh-cn: <font color='blue'><b>1.選擇機型前請先確認當前可用性區域下該機型是否有貨,部分機型需要提前報備</b></font><br><font color='blue'><b>2.可選機型列表</font><br></b></font>[ecs.c5.large <font color='green'>2vCPU 4GiB 內網頻寬1Gbps 內網收發包30萬PPS</font>]<br></b>[ecs.c5.xlarge <font color='green'>4vCPU 8GiB 內網頻寬1.5Gbps 內網收發包50萬PPS</font>]<br></b>[ecs.c5.2xlarge <font color='green'>8vCPU 16GiB 內網頻寬2.5Gbps 內網收發包80萬PPS</font>]
Label:
en: Instance Type
zh-cn: 執行個體規格
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ${VSwitchZoneId}
ECSDiskSize:
Type: Number
Description:
en: 'The size of the instance system disk, in GiB. Value range: 40 to 500'
zh-cn: 執行個體系統硬碟大小,單位為GiB,取值範圍:40~500
Label:
en: System Disk Space
zh-cn: 系統硬碟空間
MinValue: 40
MaxValue: 500
Default: 40
ECSDiskCategory:
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: ${VSwitchZoneId}
InstanceType: ${ECSInstanceType}
Label:
en: System Disk Type
zh-cn: 系統硬碟類型
EcsInstancePassword:
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
DBInstanceClass:
Label:
zh-cn: 執行個體規格
en: DB Instance Class
AssociationProperty: ALIYUN::RDS::Instance::InstanceType
AssociationPropertyMetadata:
Engine: MySQL
ZoneId: ${VSwitchZoneId}
Type: String
Description:
zh-cn: 根據資料庫引擎的類型和可用的地區支援選擇執行個體規格;<br>請參見詳細資料:<a href='https://www.alibabacloud.com/help/doc-detail/26312.html' target='_blank'><b><font color='blue'>執行個體規格表</font></b></a>
en: 'Select the instance specification based on the type of database engine and the available area support;<br>see detail: <a href=''https://www.alibabacloud.com/help/doc-detail/26312.html'' target=''_blank''><b><font color=''blue''>Instance specification sheet</font></b></a>'
DBInstanceStorage:
Label:
zh-cn: 執行個體儲存
en: Storage
Type: Number
Description:
zh-cn: RDS執行個體大小範圍為20~2000,每5個增量,單位為GB
en: The size range of RDS instances is 20 - 2000, Incrementing in every 5, unit GB
MinValue: 20
MaxValue: 2000
ConstraintDescription:
zh-cn: RDS執行個體大小範圍為20~2000,每5個增量,單位為GB
en: The size range of RDS instances is 20 - 2000, Incrementing in every 5, unit GB
Default: 200
DBName:
Type: String
Label:
en: DB Name
zh-cn: 資料庫名稱
ConstraintDescription:
zh-cn: 必須以字母開頭並且只包含字母數字字元。
en: Must begin with a letter and contain only alphanumeric characters.
MinLength: 1
MaxLength: 64
Default: employees
DBUsername:
Type: String
Description:
en: Primary account name of the database instance.
zh-cn: 資料庫執行個體的主帳號名稱。
ConstraintDescription:
en: Consist of 2 to 16 characters of lowercase letters, underline. Must begin with a letter and be end with an alphanumeric character
zh-cn: 由 2 到 16 個小寫字母組成,底線。必須以字母開頭,以字母數字字元結尾
Label:
zh-cn: 資料庫帳號名稱
en: DB Username
Default: rdsuser
MaxLength: 16
MinLength: 2
DBPassword:
NoEcho: true
Type: String
Label:
en: DB Password
zh-cn: 資料庫使用者訪問密碼
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: VSwitchZoneId
CidrBlock:
Ref: VSwitchCidrBlock
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
VpcId:
Ref: Vpc
SecurityGroupIngress:
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
NicType: intranet
SourceCidrIp: '0.0.0.0/0'
DBInstance:
Type: ALIYUN::RDS::DBInstance
Properties:
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
Engine: MySQL
EngineVersion: '8.0'
DBInstanceClass:
Ref: DBInstanceClass
DBInstanceStorage:
Ref: DBInstanceStorage
DBInstanceNetType: Intranet
DBMappings:
- CharacterSetName: utf8
DBName:
Ref: DBName
SecurityIPList: 0.0.0.0/0
DBAccount:
Type: ALIYUN::RDS::Account
DependsOn:
- DBInstance
Properties:
DBInstanceId:
Fn::GetAtt:
- DBInstance
- DBInstanceId
AccountPassword:
Ref: DBPassword
AccountType: Super
AccountName:
Ref: DBUsername
EcsInstance:
Type: ALIYUN::ECS::Instance
Properties:
VpcId:
Ref: Vpc
SecurityGroupId:
Ref: EcsSecurityGroup
VSwitchId:
Ref: VSwitch
ImageId: centos_7
AllocatePublicIP: true
InstanceType:
Ref: ECSInstanceType
SystemDiskSize:
Ref: ECSDiskSize
SystemDiskCategory:
Ref: ECSDiskCategory
Password:
Ref: EcsInstancePassword
InstanceRunCommand:
Type: ALIYUN::ECS::RunCommand
DependsOn:
- DBAccount
Properties:
CommandContent:
Fn::Sub:
- |
#!/bin/bash
yum -y install holland-mysqldump.noarch unzip
wget -P /tmp https://ros-userdata-resources.oss-cn-beijing.aliyuncs.com/MySQL/test_db-master.zip
unzip /tmp/test_db-master.zip -d /tmp/
mysql -h${DBConnectString} -p3306 -u${DBUsername} -p${DBPassword} < /tmp/test_db-master/employees.sql
- DBConnectString:
Fn::GetAtt:
- DBInstance
- InnerConnectionString
DBUsername:
Ref: DBUsername
DBPassword:
Ref: DBPassword
Type: RunShellScript
InstanceIds:
- Fn::GetAtt:
- EcsInstance
- InstanceId
Timeout: '500'
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- VSwitchZoneId
- VpcCidrBlock
- VSwitchCidrBlock
Label:
default:
zh-cn: 基礎網路設定
en: Basic Network Configuration
- Parameters:
- ECSInstanceType
- ECSDiskSize
- ECSDiskCategory
- EcsInstancePassword
Label:
default:
en: Instance
zh-cn: ECS執行個體配置
- Parameters:
- DBInstanceClass
- DBInstanceStorage
- DBName
- DBUsername
- DBPassword
Label:
default:
en: Database
zh-cn: 資料庫配置