AlibabaCloudのAuto Scaling x SLBをやってみる(ROS版)

# はじめに

[AlibabaCloudのAuto Scaling x SLBをやってみる](https://qiita.com/wa-ta-ru/items/7a9f56eb0fd84d1962e4)
こちらの記事で行ったものをROSで構築してみます。

# ROSテンプレート

```json
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": "poc",
  "Metadata": {
    "RosGraphicElement-1": {
      "x": 21,
      "y": 87,
      "width": 361,
      "height": 320
    },
    "RosGraphicElement-2": {
      "x": 31,
      "y": 39
    },
    "RosGraphicElement-3": {
      "x": 29,
      "y": 131
    },
    "RosGraphicElement-5": {
      "x": 278,
      "y": 39
    },
    "RosGraphicElement-6": {
      "x": 29,
      "y": 229
    },
    "RosGraphicElement-12": {
      "x": 628,
      "y": 459
    },
    "RosGraphicElement-7": {
      "x": 630,
      "y": 98
    },
    "RosGraphicElement-8": {
      "x": 453,
      "y": 98
    },
    "RosGraphicElement-13": {
      "x": 453,
      "y": 283
    },
    "RosGraphicElement-14": {
      "x": 717,
      "y": 238
    }
  },
  "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"
        },
        "LoadBalancerIds": [
          {
            "Ref": "SLB"
          }
        ],
        "DefaultCooldown": 300
      }
    },
    "SLB": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-7"
      },
      "Type": "ALIYUN::SLB::LoadBalancer",
      "Properties": {
        "Bandwidth": 1,
        "AddressType": "internet",
        "LoadBalancerName": {
          "Ref": "ALIYUN::StackName"
        },
        "InternetChargeType": "paybytraffic"
      }
    },
    "SLB-Listener": {
      "Metadata": {
        "GraphicId": "RosGraphicElement-8"
      },
      "Type": "ALIYUN::SLB::Listener",
      "Properties": {
        "Protocol": "http",
        "ListenerPort": 80,
        "Bandwidth": 1,
        "BackendServerPort": 80,
        "Scheduler": "wrr",
        "LoadBalancerId": {
          "Ref": "SLB"
        },
        "HealthCheck": {
          "HealthyThreshold": 3,
          "UnhealthyThreshold": 3,
          "Interval": 2,
          "Timeout": 5,
          "HttpCode": "http_2xx,http_3xx,http_4xx,http_5xx"
        }
      }
    },
    "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"
        ]
      }
    },
    "ScalingGroup": {
      "Value": {
        "Fn::GetAtt": [
          "ScalingGroup",
          "ScalingGroupId"
        ]
      }
    },
    "ScalingConfiguration": {
      "Value": {
        "Fn::GetAtt": [
          "ScalingConfiguration",
          "ScalingConfigurationId"
        ]
      }
    },
    "ScalingGroupEnable": {
      "Value": {
        "Fn::GetAtt": [
          "ScalingGroupEnable",
          "LifecycleState"
        ]
      }
    }
  }
}
```

ビジュアルエディタで見てみるとこのようなになります。
![87cd8c3f.png](https://qiita-image-store.s3.amazonaws.com/0/241104/e11bf9d9-eb42-8002-3066-62ff3572cf77.png)


# ROSを実行すると
ロードバランサー配下にAuto ScalingにECSが２つ繋がります。

![2ef0e0de.png](https://qiita-image-store.s3.amazonaws.com/0/241104/6b8c2baa-967e-102f-2cf4-e1e74832dfff.png)

※素のCentOSイメージなので、ヘルスチェックは
_異常_
にはなってます。

# まとめ
簡単にAuto ScalingでのECSとロードバランサーの環境を構築できました。ROSを続けてみて、構築のやり方が小慣れてきてミスも少なくなって来ました。皆さんもROSをやってみるとよいと思います。