ROSTemplateFormatVersion: '2015-09-01'
Description:
  zh-cn: 部署一个带有安全组的ECS实例和Redis实例，配置了公网访问和自动扩展，支持指定付费方式和资源时长。
  en: Deploy an ECS instance with a security group and a Redis instance, configured
    for public network access and auto-scaling, accommodating specified payment methods
    and resource duration.
Parameters:
  ZoneId:
    Type: String
    Description:
      zh-cn: 创建实例前，请确认可用区是否支持Redis资源的规格。
      en: Before you create an instance, confirm that the Availability Zone
        supports the specifications of Redis resources.
    AssociationProperty: ALIYUN::ECS::Instance:ZoneId
    Label:
      en: Zone ID
      zh-cn: 可用区
  VpcId:
    AssociationProperty: ALIYUN::ECS::VPC::VPCId
    Type: String
    Label:
      zh-cn: VPC ID
      en: VPC ID
  VSwitchId:
    Type: String
    Label:
      zh-cn: 交换机ID
      en: VSwitch ID
    Description:
      zh-cn: 现有网络交换机的实例ID
      en: The instance ID of an existing network switch.
    AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId
    AssociationPropertyMetadata:
      VpcId: ${VpcId}
      ZoneId: ${ZoneId}
  PayType:
    Type: String
    Label:
      zh-cn: 付费类型
      en: ECS Instance Charge Type
    AssociationProperty: ChargeType
    AssociationPropertyMetadata:
      LocaleKey: InstanceChargeType
    Default: PostPaid
    AllowedValues:
      - PostPaid
      - PrePaid
  PayPeriodUnit:
    Type: String
    Label:
      zh-cn: 购买资源时长周期
      en: Pay Period Unit
    AssociationProperty: PayPeriodUnit
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
    Default: Month
    AllowedValues:
      - Month
      - Year
  PayPeriod:
    Type: Number
    Label:
      zh-cn: 购买资源时长
      en: Period
    AssociationProperty: PayPeriod
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
    Default: 1
    AllowedValues:
    - 1
    - 2
    - 3
    - 4
    - 5
    - 6
    - 7
    - 8
    - 9
  EcsInstanceType:
    Type: String
    Label:
      en: Instance Type
      zh-cn: 实例类型
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceChargeType: ${PayType}
  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，必须包含三项（大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ 中的特殊符号）
    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，必须包含三项（大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ 中的特殊符号）
    AssociationProperty: ALIYUN::ECS::Instance::Password
    AllowedPattern: '^[a-zA-Z0-9-\(\)\`\~\!\@\#\$\%\^\&\*\_\-\+\=\|\{\}\[\]\:\;\<\>\,\.\?\/]*$'
    MinLength: 8
    MaxLength: 30
Resources:
  # 安全组
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      # 使用默认名称
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: VpcId
      # 入方向开放80端口
      SecurityGroupIngress:
        - Priority: 1
          PortRange: 80/80
          NicType: internet
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
      # 出方向全开
      SecurityGroupEgress:
        - Priority: 1
          PortRange: -1/-1
          DestCidrIp: 0.0.0.0/0
          NicType: intranet
          IpProtocol: all
  # ecs配置
  EcsInstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
      IoOptimized: optimized
      PeriodUnit:
        Ref: PayPeriodUnit
      Period:
        Ref: PayPeriod
      InstanceChargeType:
        Ref: PayType
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitchId
      MaxAmount: 1
      SystemDiskCategory: cloud_essd
      SystemDiskSize: 100
      VpcId:
        Ref: VpcId
      ZoneId:
        Ref: ZoneId
      ImageId: centos_7
      InstanceType:
        Ref: EcsInstanceType
      Password:
        Ref: InstancePassword
      AllocatePublicIP: true
      InternetMaxBandwidthOut: 5
      UserData:
        Fn::Sub:
          - |
            #!/bin/bash
            cat >> /root/conf.txt << "EOF"
            DBConnectString=${DBConnectString}
            EOF
          - DBConnectString:
              Fn::GetAtt:
                - RedisInstance
                - ConnectionDomain
  # redis 实例配置
  RedisInstance:
    Type: ALIYUN::REDIS::Instance
    Properties:
      VpcId:
        Ref: VpcId
      VSwitchId:
        Ref: VSwitchId
      InstanceClass: redis.master.small.default
      EngineVersion: '5.0'
      EvictionPolicy: 'noeviction'
      ZoneId:
        Ref: ZoneId
      InstanceName:
        Ref: ALIYUN::StackName
      Password:
        Ref: InstancePassword
  Whitelist:
    Type: ALIYUN::REDIS::Whitelist
    Properties:
      InstanceId:
        Fn::GetAtt:
          - RedisInstance
          - InstanceId
      SecurityIps:
        Fn::Join:
          - ','
          - Fn::GetAtt:
              - EcsInstanceGroup
              - PrivateIps
Metadata:
  'ALIYUN::ROS::Interface':
    ParameterGroups:
      - Parameters:
          - ZoneId
        Label:
          default:
            zh-cn: 可用区配置
            en: Zone Configuration
      - Parameters:
          - VpcId
          - VSwitchId
        Label:
          default:
            zh-cn: 选择已有基础资源配置
            en: Choose existing Infrastructure Configuration
      - Parameters:
          - PayType
          - PayPeriodUnit
          - PayPeriod
        Label:
          default:
            zh-cn: 付费类型配置
            en: PayType Configuration
      - Parameters:
          - EcsInstanceType
          - InstancePassword
        Label:
          default:
            en: Instance
            zh-cn: ECS实例配置
      - Parameters:
          - InstanceClass
        Label:
          default:
            zh-cn: Redis配置
            en: Redis Configuration
Outputs:
  InstanceId:
    Value:
      'Fn::GetAtt':
        - RedisInstance
        - InstanceId