本文以使用云服務器ECS連接云數據庫RDS完成數據初始化為例,由簡入難地向您介紹如何編輯ROS模板。
前提條件
請您提前了解模板語法和結構。更多信息,請參見模板快速入門。
場景示例
在阿里云專有網絡中創建ECS實例和RDS實例,并在云服務器ECS中獲取數據庫的連接信息,從而完成數據初始化。
使用須知
您可以訪問對應的資源類型查看屬性詳情。具體操作,請參見查看資源類型。
資源類型為每個屬性定義了類型、是否必須、是否允許更新等信息。如果為必須,則要求必須在模板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資源
您可以通過模板定義云服務器ECS資源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='http://bestwisewords.com/document_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: 數據庫配置