{
    "ROSTemplateFormatVersion": "2015-09-01",
    "Parameters": {
        "OwnerName": {
            "Type": "String"
        },
        "OwnerEmail": {
            "Type": "String"
        },
        "AZIdx": {
            "Type": "Number",
            "Description": "Resources from which AZ",
            "Default": 0,
            "MinValue": 0,
            "MaxValue": 2
        }
    },
    "Mappings": {
        "ValueMap": {
            "ap-southeast-1": {
                "AMI": "m-t4ndkvq971edgpkwmpgu",
                "key": "pe-class-key-singapore"
            }
        }
    },
    "Resources": {
        "EcsVPC": {
            "Type": "ALIYUN::ECS::VPC",
            "Properties": {
                "CidrBlock": "10.90.0.0/16",
                "Tags": [
                    {
                        "Key": "Owner",
                        "Value": {
                            "Ref": "OwnerEmail"
                        }
                    },
                    {
                        "Key": "Name",
                        "Value": {
                            "Ref": "OwnerName"
                        }
                    }
                ]
            }
        },
        "EcsVSwitch": {
            "Type": "ALIYUN::ECS::VSwitch",
            "Properties": {
                "ZoneId": {
                    "Fn::Select": [
                        {
                            "Ref": "AZIdx"
                        },
                        {
                            "Fn::GetAZs": {
                                "Ref": "ALIYUN::Region"
                            }
                        }
                    ]
                },
                "VpcId": {
                    "Ref": "EcsVPC"
                },
                "CidrBlock": "10.90.1.0/24",
                "Tags": [
                    {
                        "Key": "Owner",
                        "Value": {
                            "Ref": "OwnerEmail"
                        }
                    },
                    {
                        "Key": "Name",
                        "Value": {
                            "Fn::Join": [
                                "-",
                                [
                                    "demo",
                                    "subnet",
                                    "1"
                                ]
                            ]
                        }
                    }
                ]
            }
        },
        "EcsSG": {
            "Type": "ALIYUN::ECS::SecurityGroup",
            "Properties": {
                "VpcId": {
                    "Ref": "EcsVPC"
                },
                "Description": "Allow nodes communication.",
                "SecurityGroupIngress": [
                    {
                        "IpProtocol": "tcp",
                        "PortRange": "22/22",
                        "SourceCidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "PortRange": "80/80",
                        "SourceCidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "PortRange": "443/443",
                        "SourceCidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "PortRange": "8000/8000",
                        "SourceCidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "PortRange": "8501/8501",
                        "SourceCidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "PortRange": "8502/8502",
                        "SourceCidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "icmp",
                        "PortRange": "-1/-1",
                        "SourceCidrIp": "0.0.0.0/0"
                    }
                ],
                "SecurityGroupEgress": [
                    {
                        "DestCidrIp": "0.0.0.0/0",
                        "IpProtocol": "all",
                        "PortRange": "-1/-1",
                        "Priority": 1
                    }
                ],
                "Tags": [
                    {
                        "Key": "Owner",
                        "Value": {
                            "Ref": "OwnerEmail"
                        }
                    },
                    {
                        "Key": "Name",
                        "Value": {
                            "Fn::Join": [
                                "-",
                                [
                                    {
                                        "Ref": "OwnerName"
                                    },
                                    "-sg"
                                ]
                            ]
                        }
                    }
                ]
            }
        },
        "EcsLT": {
            "Type": "ALIYUN::ECS::LaunchTemplate",
            "Properties": {
                "VersionDescription": "v1",
                "LaunchTemplateName": {
                    "Ref": "ALIYUN::StackName"
                },
                "ImageId": {
                    "Fn::FindInMap": [
                        "ValueMap",
                        {
                            "Ref": "ALIYUN::Region"
                        },
                        "AMI"
                    ]
                },
                "InstanceType": "ecs.c9i.large",
                "SecurityGroupId": {
                    "Ref": "EcsSG"
                },
                "SystemDiskCategory": "cloud_essd",
                "SystemDiskSize": "50",
                "KeyPairName": {
                    "Fn::FindInMap": [
                        "ValueMap",
                        {
                            "Ref": "ALIYUN::Region"
                        },
                        "key"
                    ]
                },
                "Tags": [
                    {
                        "Key": "trainer",
                        "Value": {
                            "Ref": "OwnerName"
                        }
                    },
                    {
                        "Key": "Name",
                        "Value": "monitor"
                    }
                ],
                "InternetChargeType": "PayByTraffic",
                "InternetMaxBandwidthOut": "100"
            }
        },
        "EssSG": {
            "Type": "ALIYUN::ESS::ScalingGroup",
            "Properties": {
                "VSwitchIds": [
                    {
                        "Ref": "EcsVSwitch"
                    }
                ],
                "MaxSize": "1",
                "MinSize": "1",
                "DesiredCapacity": "1",
                "LaunchTemplateId": {
                    "Ref": "EcsLT"
                },
                "Tags": [
                    {
                        "Key": "Owner",
                        "Value": {
                            "Ref": "OwnerEmail"
                        }
                    },
                    {
                        "Key": "trainer",
                        "Value": {
                            "Ref": "OwnerName"
                        }
                    }
                ]
            }
        },
        "EssSGE": {
            "Type": "ALIYUN::ESS::ScalingGroupEnable",
            "Properties": {
                "ScalingGroupId": {
                    "Ref": "EssSG"
                }
            }
        }
    }
}