Connect to an ApsaraDB RDS instance from an ECS instance to initialize data

更新时间:
复制 MD 格式

This topic describes how to edit a Resource Orchestration Service (ROS) template. The example shows how to connect to an ApsaraDB RDS instance from an Elastic Compute Service (ECS) instance to initialize data.

Prerequisites

Familiarize yourself with the template syntax and structure. For more information, see Quick Start.

Scenario

In this scenario, you create an ECS instance and an RDS instance in an Alibaba Cloud virtual private cloud (VPC). Then, you retrieve the database connection information from the ECS instance to initialize the data.2023-03-24_17-08-11

Usage notes

View the property details for each resource type. For more information, see View resource types.

Each resource type defines the type, requirements, and update policy for its properties. Required properties must be declared in the `Properties` section of the `Resources` block. Optional properties do not need to be declared. If a property allows updates, you can modify it in a new template and then update the stack to apply the changes. Otherwise, the property cannot be updated.

Edit the template

You can find the required resource types in the resource type index document. For more information, see Resource type index.

For example, this scenario requires you to create a VPC (ALIYUN::ECS::VPC), an ECS instance (ALIYUN::ECS::Instance), and an RDS instance (ALIYUN::RDS::DBInstance). You also need to create a vSwitch (ALIYUN::ECS::VSwitch) and a security group (ALIYUN::ECS::SecurityGroup) for the ECS instance, and use Cloud Assistant (ALIYUN::ECS::RunCommand) to run the data initialization command.

Based on this information, define the resources to create in the `Resources` section of the template.

Define template resources and their dependencies

Define basic network resources

Define the basic network resources Vpc, VSwitch, and EcsSecurityGroup in the template.

  • Use `Ref` and the `ALIYUN::StackName` pseudo parameter to retrieve the stack name as the value for resource properties, such as VpcName in Vpc and VSwitchName in VSwitch. For more information, see Ref and ALIYUN::StackName.

  • Use the `Fn::Select` and `Fn::GetAZs` functions with the `ALIYUN::Region` pseudo parameter to retrieve the ID of the first zone in the stack's region. For example, for `ZoneId` in `VSwitch`. For more information, see Functions and 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

Define database resources

Define the database resources DBInstance and DBAccount in the template.

Use the `Fn::GetAtt` function to retrieve the value of a resource's output property, such as DBInstanceId in DBAccount. For more information, see 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

Define Elastic Compute Service (ECS) resources

Define the ECS resources EcsInstance and InstanceRunCommand in the template.

Use the `Fn::Sub` function to construct a command string, such as CommandContent in InstanceRunCommand. For more information, see Fn::Sub.

Note

The initialization data used here is the official test data from MySQL. To ensure a stable download, store the data in an Object Storage Service (OSS) bucket in advance.

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'

Complete sample template

ROSTemplateFormatVersion: '2015-09-01'
Description: { }
Parameters:
  EcsInstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: The logon password of the server. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (()`~!@#$%^&*_-+=|{}[]:;'<>,.?/).
      zh-cn: The logon password of the server. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (()`~!@#$%^&*_-+=|{}[]:;'<>,.?/).
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
      zh-cn: Instance Password
    ConstraintDescription:
      en: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (()`~!@#$%^&*_-+=|{}[]:;'<>,.?/).
      zh-cn: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (()`~!@#$%^&*_-+=|{}[]:;'<>,.?/).
    MinLength: 8
    MaxLength: 30
  DBPassword:
    NoEcho: true
    Type: String
    Label:
      en: DB Password
      zh-cn: DB Password
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'

Add template parameter groups and dynamically get parameter configurations

The preceding template defines multiple resources and their dependencies. However, the values for properties such as `InstanceType` and `SystemDiskCategory` of the `EcsInstance` resource and `DBInstanceClass` of the `DBInstance` resource are static. This requires you to adjust the template structure and change resource properties multiple times to deploy stacks in different regions.

You can add parameters to the template to improve its flexibility and reusability.

Add template parameter groups

You can use metadata in the template to group the parameters defined in the `Parameters` section and define group tags for the parameter groups.

You can group resources by type and their corresponding parameters. For example, you can group the resources in the current template as follows:

Resource parameter categorization

Resource name

Parameter name

Basic network configuration

Vpc, VSwitch, EcsSecurityGroup

VSwitchZoneId, VpcCidrBlock, VSwitchCidrBlock

Database configuration

DBInstance, DBAccount

DBInstanceClass, DBInstanceStorage, DBName, DBUsername, DBPassword

ECS instance configuration

EcsInstance, InstanceRunCommand

ECSInstanceType, ECSDiskSize, ECSDiskCategory, EcsInstancePassword

Dynamically get parameter configurations

Take the ECSInstanceType parameter as an example. To set filter conditions and dynamically select parameter configurations in the console, find the supported AssociationProperty value (`ALIYUN::ECS::Instance::InstanceType`) for the parameter in the AssociationProperty and AssociationPropertyMetadata documentation based on the corresponding resource type (`ALIYUN::ECS::Instance`). Then, view the AssociationPropertyMetadata value for the selected AssociationProperty where the filter condition is set to ZoneId. For more information, see AssociationProperty and AssociationPropertyMetadata.

Complete sample template

ROSTemplateFormatVersion: '2015-09-01'
Description: { }
Parameters:
  VSwitchZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    Description:
      en: The zone ID of the vSwitch.
      zh-cn: The zone ID of the vSwitch.
    Label:
      en: VSwitch Zone ID
      zh-cn: VSwitch Zone ID
  VpcCidrBlock:
    Default: 192.168.0.0/16
    Label:
      zh-cn: VPC CIDR Block
      en: VPC CIDR Block
    Type: String
    Description:
      zh-cn: The CIDR block for the new VPC. We recommend that you use one of the following CIDR blocks:<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: The CIDR block for the new VPC. We recommend that you use one of the following CIDR blocks:<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: Must be a subnet of the VPC and not used by other vSwitches.
      en: Must be a subnet of the VPC and not used by other vSwitches.
    Label:
      zh-cn: vSwitch CIDR block
      en: VSwitch CIDR Block
  ECSInstanceType:
    Type: String
    Description:
      en: <font color='blue'><b>1. Before you select an instance type, confirm that it is in stock in the current zone. For some instance types, you must submit an application in advance.</font><font color='blue'><b>2. Available instance types:</font>[ecs.c5.large <font color='green'>2 vCPUs, 4 GiB memory, 1 Gbit/s internal bandwidth, 300,000 PPS</font>]<br>[ecs.c5.xlarge <font color='green'>4 vCPUs, 8 GiB memory, 1.5 Gbit/s internal bandwidth, 500,000 PPS</font>]<br>[ecs.c5.2xlarge <font color='green'>8 vCPUs, 16 GiB memory, 2.5 Gbit/s internal bandwidth, 800,000 PPS</font>]
      zh-cn: <font color='blue'><b>1. Before you select an instance type, confirm that it is in stock in the current zone. For some instance types, you must submit an application in advance.</font><font color='blue'><b>2. Available instance types:</font>[ecs.c5.large <font color='green'>2 vCPUs, 4 GiB memory, 1 Gbit/s internal bandwidth, 300,000 PPS</font>]<br>[ecs.c5.xlarge <font color='green'>4 vCPUs, 8 GiB memory, 1.5 Gbit/s internal bandwidth, 500,000 PPS</font>]<br>[ecs.c5.2xlarge <font color='green'>8 vCPUs, 16 GiB memory, 2.5 Gbit/s internal bandwidth, 800,000 PPS</font>]
    Label:
      en: Instance Type
      zh-cn: Instance Type
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${VSwitchZoneId}
  ECSDiskSize:
    Type: Number
    Description:
      en: 'The size of the system disk, in GiB. Valid values: 40 to 500.'
      zh-cn: 'The size of the system disk, in GiB. Valid values: 40 to 500.'
    Label:
      en: System Disk Size
      zh-cn: System Disk Size
    MinValue: 40
    MaxValue: 500
    Default: 40
  ECSDiskCategory:
    Type: String
    Description:
      en: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>Ultra Disk</font>]<br>[cloud_ssd: <font color=''green''>Standard SSD</font>]<br>[cloud_essd: <font color=''green''>Enterprise SSD (ESSD)</font>]<br>[cloud: <font color=''green''>Basic Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD</font>]'
      zh-cn: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>Ultra Disk</font>]<br>[cloud_ssd: <font color=''green''>Standard SSD</font>]<br>[cloud_essd: <font color=''green''>Enterprise SSD (ESSD)</font>]<br>[cloud: <font color=''green''>Basic Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD</font>]'
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${VSwitchZoneId}
      InstanceType: ${ECSInstanceType}
    Label:
      en: System Disk Type
      zh-cn: System Disk Type
  EcsInstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: The logon password of the server. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (()`~!@#$%^&*_-+=|{}[]:;'<>,.?/).
      zh-cn: The logon password of the server. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (()`~!@#$%^&*_-+=|{}[]:;'<>,.?/).
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
      zh-cn: Instance Password
    ConstraintDescription:
      en: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (()`~!@#$%^&*_-+=|{}[]:;'<>,.?/).
      zh-cn: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (()`~!@#$%^&*_-+=|{}[]:;'<>,.?/).
    MinLength: 8
    MaxLength: 30
  DBInstanceClass:
    Label:
      zh-cn: DB Instance Class
      en: DB Instance Class
    AssociationProperty: ALIYUN::RDS::Instance::InstanceType
    AssociationPropertyMetadata:
      Engine: MySQL
      ZoneId: ${VSwitchZoneId}
    Type: String
    Description:
      zh-cn: 'Select an instance type based on the database engine and the supported zones.<br>For more information, see <a href=''https://www.alibabacloud.com/help/doc-detail/26312.html'' target=''_blank''><b><font color=''blue''>Instance types</font></a>'
      en: 'Select an instance type based on the database engine and the supported zones.For more information, see <a href=''https://www.alibabacloud.com/help/doc-detail/26312.html'' target=''_blank''><b><font color=''blue''>Instance types</font></a>'
  DBInstanceStorage:
    Label:
      zh-cn: Storage
      en: Storage
    Type: Number
    Description:
      zh-cn: The storage capacity of the RDS instance. Valid values: 20 to 2000. The value must be a multiple of 5. Unit: GB.
      en: The storage capacity of the RDS instance. Valid values: 20 to 2000. The value must be a multiple of 5. Unit: GB.
    MinValue: 20
    MaxValue: 2000
    ConstraintDescription:
      zh-cn: The storage capacity of the RDS instance. Valid values: 20 to 2000. The value must be a multiple of 5. Unit: GB.
      en: The storage capacity of the RDS instance. Valid values: 20 to 2000. The value must be a multiple of 5. Unit: GB.
    Default: 200
  DBName:
    Type: String
    Label:
      en: DB Name
      zh-cn: DB Name
    ConstraintDescription:
      zh-cn: Must start with a letter and can contain only letters and digits.
      en: Must begin with a letter and contain only alphanumeric characters.
    MinLength: 1
    MaxLength: 64
    Default: employees
  DBUsername:
    Type: String
    Description:
      en: The name of the privileged account for the database instance.
      zh-cn: The name of the privileged account for the database instance.
    ConstraintDescription:
      en: The name must be 2 to 16 characters in length and can contain lowercase letters and underscores (_). It must start with a letter and end with a letter or a digit.
      zh-cn: The name must be 2 to 16 characters in length and can contain lowercase letters and underscores (_). It must start with a letter and end with a letter or a digit.
    Label:
      zh-cn: DB Username
      en: DB Username
    Default: rdsuser
    MaxLength: 16
    MinLength: 2
  DBPassword:
    NoEcho: true
    Type: String
    Label:
      en: DB Password
      zh-cn: DB Password
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: Basic Network Configuration
            en: Basic Network Configuration
      - Parameters:
          - ECSInstanceType
          - ECSDiskSize
          - ECSDiskCategory
          - EcsInstancePassword
        Label:
          default:
            en: ECS Instance Configuration
            zh-cn: ECS Instance Configuration
      - Parameters:
          - DBInstanceClass
          - DBInstanceStorage
          - DBName
          - DBUsername
          - DBPassword
        Label:
          default:
            en: Database Configuration
            zh-cn: Database Configuration