AlibabaCloudのROSからAuto ScalingでECSを起動してみる

# はじめに
[AlibabaCloudのAuto Scalingを触ってみる](https://qiita.com/wa-ta-ru/items/df420ba29cd457db8275)
からの、AlibabaCloudのAuto Scalingを
ROSで構築してみます。

# ROSテンプレート


```json
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": "poc",
  "Metadata": {
    "RosGraphicElement-3": {
      "x": 404,
      "y": 117,
      "width": 386,
      "height": 270
    },
    "RosGraphicElement-1": {
      "x": 39,
      "y": 39
    },
    "RosGraphicElement-2": {
      "x": 147,
      "y": 39
    },
    "RosGraphicElement-5": {
      "x": 139,
      "y": 359
    },
    "RosGraphicElement-6": {
      "x": 139,
      "y": 359
    },
    "RosGraphicElement-12": {
      "x": 242,
      "y": 237
    },
    "RosGraphicElement-13": {
      "x": 139,
      "y": 237
    },
    "RosGraphicElement-14": {
      "x": 181,
      "y": 133
    }
  },
  "Parameters": {
    "CidrBlock": {
      "Type": "String",
      "AllowedValues": [
        "192.168.0.0/16",
        "172.16.0.0/12",
        "10.0.0.0/8"
      ],
      "Default": "10.0.0.0/8"
    },
    "ImageId": {
      "Type": "String",
      "Description": "ECS Image",
      "Label": "ECS Image",
      "Default": "centos_7_04_64_20G_alibase_201701015.vhd"
    },
    "InstanceType": {
      "Type": "String",
      "Description": "ECS Instance Type",
      "AllowedValues": [
        "ecs.t5-lc2m1.nano"
      ],
      "Label": "ECS Instance Type",
      "Default": "ecs.t5-lc2m1.nano"
    },
    "KeyPair": {
      "Type": "String",
      "Description": "ECS Key Pair",
      "Label": "ECS Key Pair",
      "Default": "aliyun-ap-northeast-1"
    }
  },
  "Mappings": {},
  "Conditions": {},
  "Resources": {
    "VPC": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-1"
      },
      "Type": "ALIYUN::ECS::VPC",
      "Properties": {
        "VpcName": {
          "Ref": "ALIYUN::StackName"
        },
        "CidrBlock": "10.0.0.0/8"
      }
    },
    "VSwitch-ECS": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-2"
      },
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VSwitchName": "ecs",
        "VpcId": {
          "Ref": "VPC"
        },
        "CidrBlock": "10.0.0.0/24",
        "Description": "ecs",
        "ZoneId": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": {
                "Ref": "ALIYUN::Region"
              }
            }
          ]
        }
      }
    },
    "VSwitch-SLB": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-3"
      },
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VSwitchName": "slb",
        "VpcId": {
          "Ref": "VPC"
        },
        "CidrBlock": "10.0.1.0/24",
        "Description": "slb",
        "ZoneId": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": {
                "Ref": "ALIYUN::Region"
              }
            }
          ]
        }
      }
    },
    "SG": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-5"
      },
      "Type": "ALIYUN::ECS::SecurityGroup",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "SecurityGroupName": {
          "Fn::Join": [
            "-",
            [
              {
                "Ref": "ALIYUN::StackName"
              },
              "ecs-sg"
            ]
          ]
        },
        "SecurityGroupEgress": [
          {
            "PortRange": "-1/-1",
            "Priority": 1,
            "IpProtocol": "all",
            "DestCidrIp": "0.0.0.0/0",
            "NicType": "intranet",
            "SecurityGroupId": "0"
          }
        ],
        "SecurityGroupIngress": [
          {
            "PortRange": "-1/-1",
            "Priority": 1,
            "SourceCidrIp": "0.0.0.0/0",
            "IpProtocol": "all",
            "NicType": "intranet",
            "SecurityGroupId": "0"
          }
        ]
      }
    },
    "VSwitch-RDS": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-6"
      },
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VSwitchName": "rds",
        "VpcId": {
          "Ref": "VPC"
        },
        "CidrBlock": "10.0.2.0/24",
        "Description": "rds",
        "ZoneId": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": {
                "Ref": "ALIYUN::Region"
              }
            }
          ]
        }
      }
    },
    "ScalingGroup": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-12"
      },
      "Type": "ALIYUN::ESS::ScalingGroup",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "ScalingGroupName": "SSG",
        "RemovalPolicys": [
          "OldestScalingConfiguration",
          "OldestInstance"
        ],
        "MinSize": "2",
        "MaxSize": "2",
        "VSwitchId": {
          "Ref": "VSwitch-ECS"
        },
        "DefaultCooldown": 300
      }
    },
    "ScalingConfiguration": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-13"
      },
      "Type": "ALIYUN::ESS::ScalingConfiguration",
      "Properties": {
        "SystemDiskCategory": "cloud_ssd",
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "SecurityGroupId": {
          "Ref": "SG"
        },
        "ImageId": {
          "Ref": "ImageId"
        },
        "InternetMaxBandwidthOut": 1,
        "IoOptimized": "optimized",
        "ScalingGroupId": {
          "Ref": "ScalingGroup"
        },
        "InternetChargeType": "PayByTraffic",
        "InternetMaxBandwidthIn": 200,
        "ScalingConfigurationName": {
          "Ref": "ALIYUN::StackName"
        }
      }
    },
    "ScalingGroupEnable": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-14"
      },
      "Type": "ALIYUN::ESS::ScalingGroupEnable",
      "Properties": {
        "ScalingConfigurationId": {
          "Ref": "ScalingConfiguration"
        },
        "ScalingGroupId": {
          "Ref": "ScalingGroup"
        }
      }
    }
  },
  "Outputs": {
    "VRouterId": {
      "Value": {
        "Fn::GetAtt": [
          "VPC",
          "VRouterId"
        ]
      }
    },
    "VSwitch-ECS": {
      "Value": {
        "Fn::GetAtt": [
          "VSwitch-ECS",
          "VSwitchId"
        ]
      }
    },
    "RouteTableId": {
      "Value": {
        "Fn::GetAtt": [
          "VPC",
          "RouteTableId"
        ]
      }
    },
    "VpcId": {
      "Value": {
        "Fn::GetAtt": [
          "VPC",
          "VpcId"
        ]
      }
    },
    "VSwitch-SLB": {
      "Value": {
        "Fn::GetAtt": [
          "VSwitch-ECS",
          "VSwitchId"
        ]
      }
    },
    "VSwitch-RDS": {
      "Value": {
        "Fn::GetAtt": [
          "VSwitch-RDS",
          "VSwitchId"
        ]
      }
    },
    "ScalingGroupEnable": {
      "Value": {
        "Fn::GetAtt": [
          "ScalingGroupEnable",
          "LifecycleState"
        ]
      }
    },
    "ScalingGroup": {
      "Value": {
        "Fn::GetAtt": [
          "ScalingGroup",
          "ScalingGroupId"
        ]
      }
    },
    "ScalingConfiguration": {
      "Value": {
        "Fn::GetAtt": [
          "ScalingConfiguration",
          "ScalingConfigurationId"
        ]
      }
    }
  }
}
```

# Auto ScalingのResourceの部分を見てみる

##  [ALIYUN::ESS::ScalingGroup](https://jp.alibabacloud.com/help/doc-detail/51196.htm)

スケーリングのグループ管理になります。
インスタンスの個数（サイズ）を調整できます。

```json
    "ScalingGroup": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-12"
      },
      "Type": "ALIYUN::ESS::ScalingGroup",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "ScalingGroupName": "SSG",
        "RemovalPolicys": [
          "OldestScalingConfiguration",
          "OldestInstance"
        ],
        "MinSize": "2",
        "MaxSize": "2",
        "VSwitchId": {
          "Ref": "VSwitch-ECS"
        },
        "DefaultCooldown": 300
      }
    },
```


### [ALIYUN::ESS::ScalingConfiguration](https://jp.alibabacloud.com/help/doc-detail/51203.htm)

ECSインスタンスをどのように作るかになります。

```json
    "ScalingConfiguration": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-13"
      },
      "Type": "ALIYUN::ESS::ScalingConfiguration",
      "Properties": {
        "SystemDiskCategory": "cloud_ssd",
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "SecurityGroupId": {
          "Ref": "SG"
        },
        "ImageId": {
          "Ref": "ImageId"
        },
        "InternetMaxBandwidthOut": 1,
        "IoOptimized": "optimized",
        "ScalingGroupId": {
          "Ref": "ScalingGroup"
        },
        "InternetChargeType": "PayByTraffic",
        "InternetMaxBandwidthIn": 200,
        "ScalingConfigurationName": {
          "Ref": "ALIYUN::StackName"
        }
      }
    },
```

### [ALIYUN::ESS::ScalingGroupEnable](https://jp.alibabacloud.com/help/doc-detail/51196.htm)

名前の通り、スケーリングの有効を管理します。

```json
    "ScalingGroupEnable": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-14"
      },
      "Type": "ALIYUN::ESS::ScalingGroupEnable",
      "Properties": {
        "ScalingConfigurationId": {
          "Ref": "ScalingConfiguration"
        },
        "ScalingGroupId": {
          "Ref": "ScalingGroup"
        }
      }
    }
```

# ROSから起動するとこのようになります。

![8630463c.png](https://qiita-image-store.s3.amazonaws.com/0/241104/95f98cd3-b2f7-2eda-c1af-95b9e95892ee.png)

# さいごに

簡単にAutoScaling構成でECSが構築できました。
気づきましたでしょうか？
今回は、インスタンスタイプを
ecs.t5-lc2m1.nano
にしております。

[AlibabaCloudのECSを東京リージョンの最安値で起動してみる！](https://qiita.com/wa-ta-ru/items/defd0114030fdfa95ea1)
の記事より、１台で起動するともっと安くなるはずです。