全部产品
Search
文档中心

资源编排:使用云服务器ECS连接云数据库RDS完成数据初始化

更新时间:Apr 23, 2023

本文以使用云服务器ECS连接云数据库RDS完成数据初始化为例,由简入难地向您介绍如何编辑ROS模板。

前提条件

请您提前了解模板语法和结构。更多信息,请参见模板快速入门

场景示例

在阿里云专有网络中创建ECS实例和RDS实例,并在云服务器ECS中获取数据库的连接信息,从而完成数据初始化。2023-03-24_17-08-11

使用须知

您可以访问对应的资源类型查看属性详情。具体操作,请参见查看资源类型

资源类型为每个属性定义了类型、是否必须、是否允许更新等信息。如果为必须,则要求必须在模板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)。

定义模板资源及其依赖关系

定义基础网络资源

您可以通过模板定义基础网络资源VpcVSwitchEcsSecurityGroup

  • 使用Ref与伪参数ALIYUN::StackName获取资源栈名称作为资源属性的属性值,例如Vpc中的VpcNameVSwitch中的VSwitchName。更多信息,请参见RefALIYUN::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

定义数据库资源

您可以通过模板定义数据库资源DBInstanceDBAccount

使用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资源EcsInstanceInstanceRunCommand

使用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资源的InstanceTypeSystemDiskCategory属性值与DBInstance资源的DBInstanceClass属性值为固定值,当您在不同地域创建资源栈时,需要多次调整模板结构和变更资源属性以达到部署资源栈的目的。

您可以对模板添加参数Parameters,从而提高模板的灵活性和可复用性。

添加模板参数分组

您可以在模板中使用元数据(Metadata)对Parameters中定义的参数进行分组,并定义参数分组标签。

您可以根据不同资源以及资源对应的参数进行分组。以当前模板为例,您可以将资源按照如下结果划分。

资源参数分类

资源名称

参数名称

基础网络配置

VpcVSwitchEcsSecurityGroup

VSwitchZoneIdVpcCidrBlockVSwitchCidrBlock

数据库配置

DBInstanceDBAccount

DBInstanceClassDBInstanceStorageDBNameDBUsernameDBPassword

ECS云服务器配置

EcsInstanceInstanceRunCommand

ECSInstanceTypeECSDiskSizeECSDiskCategoryEcsInstancePassword

动态获取参数配置

ECSInstanceType参数为例,当您需要在控制台上对参数设置筛选条件并动态选择参数配置时,可以按照参数对应的资源类型(ALIYUN::ECS::Instance)在AssociationProperty和AssociationPropertyMetadata文档中查询到该参数支持的AssociationProperty取值(ALIYUN::ECS::Instance::InstanceType),然后查看对筛选到的AssociationProperty设置过滤条件为ZoneIdAssociationPropertyMetadata取值。更多信息,请参见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: 数据库配置