{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "PEIGW": {
            "Type": "AWS::EC2::InternetGateway",
            "Properties": {
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com - PE service"
                    }
                ]
            },
            "DependsOn": [
                "PEVPC"
            ]
        },
        "PEVPC": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "10.0.0.0/16",
                "EnableDnsSupport": "true",
                "EnableDnsHostnames": "true",
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com - PE service"
                    },
                    {
                        "Key": "Name",
                        "Value": "pe-service-vpc"
                    }
                ]
            }
        },
        "PEIGWAttachment": {
            "Type": "AWS::EC2::VPCGatewayAttachment",
            "Properties": {
                "InternetGatewayId": {
                    "Ref": "PEIGW"
                },
                "VpcId": {
                    "Ref": "PEVPC"
                }
            }
        },
        "PEPublicRouteTable": {
            "Type": "AWS::EC2::RouteTable",
            "Properties": {
                "VpcId": {
                    "Ref": "PEVPC"
                },
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com - PE service"
                    }
                ]
            }
        },
        "PEPublicRoute": {
            "Type": "AWS::EC2::Route",
            "Properties": {
                "DestinationCidrBlock": "0.0.0.0/0",
                "GatewayId": {
                    "Ref": "PEIGW"
                },
                "RouteTableId": {
                    "Ref": "PEPublicRouteTable"
                }
            },
            "DependsOn": [
                "PEIGWAttachment"
            ]
        },
        "PEServiceSubnet": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "VpcId": {
                    "Ref": "PEVPC"
                },
                "CidrBlock": "10.0.0.0/24",
                "AvailabilityZone": "us-west-2b",
                "PrivateDnsNameOptionsOnLaunch": {
                    "EnableResourceNameDnsAAAARecord": false,
                    "EnableResourceNameDnsARecord": true,
                    "HostnameType": "ip-name"
                },
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com - PE service"
                    },
                    {
                        "Key": "Name",
                        "Value": "pe-service-subnet"
                    }
                ]
            }
        },
        "PEServiceSubnetRouteTableAssociation": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "PEServiceSubnet"
                },
                "RouteTableId": {
                    "Ref": "PEPublicRouteTable"
                }
            }
        },
        "PEServiceSecurityGroup": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": "Allow nodes communication.",
                "VpcId": {
                    "Ref": "PEVPC"
                },
                "SecurityGroupIngress": [
                    {
                        "IpProtocol": "tcp",
                        "FromPort": 22,
                        "ToPort": 22,
                        "CidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "FromPort": 2379,
                        "ToPort": 2379,
                        "CidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "FromPort": 4000,
                        "ToPort": 4002,
                        "CidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "tcp",
                        "FromPort": 3000,
                        "ToPort": 3000,
                        "CidrIp": "0.0.0.0/0"
                    },
                    {
                        "IpProtocol": "icmp",
                        "FromPort": -1,
                        "ToPort": -1,
                        "CidrIp": "0.0.0.0/0"
                    }
                ],
                "SecurityGroupEgress": [
                    {
                        "IpProtocol": "-1",
                        "CidrIp": "0.0.0.0/0"
                    }
                ],
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com - PE service"
                    },
                    {
                        "Key": "Name",
                        "Value": "pe-service-sg"
                    }
                ]
            }
        },
        "PENodeSecurityGroupRefInboundRule": {
            "Type": "AWS::EC2::SecurityGroupIngress",
            "Properties": {
                "IpProtocol": "-1",
                "SourceSecurityGroupId": {
                    "Fn::GetAtt": [
                        "PEServiceSecurityGroup",
                        "GroupId"
                    ]
                },
                "GroupId": {
                    "Fn::GetAtt": [
                        "PEServiceSecurityGroup",
                        "GroupId"
                    ]
                }
            }
        },
        "PEServiceLT": {
            "Type": "AWS::EC2::LaunchTemplate",
            "Properties": {
                "VersionDescription": "v1",
                "LaunchTemplateName": {
                    "Fn::Sub": "${AWS::StackName}-service-lt"
                },
                "LaunchTemplateData": {
                    "IamInstanceProfile": {
                        "Name": {
                            "Ref": "PEInstanceProfile"
                        }
                    },
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda",
                            "Ebs": {
                                "DeleteOnTermination": true,
                                "Encrypted": false,
                                "VolumeSize": 100,
                                "VolumeType": "gp2"
                            }
                        }
                    ],
                    "NetworkInterfaces": [
                        {
                            "DeviceIndex": 0,
                            "AssociatePublicIpAddress": true,
                            "Groups": [
                                {
                                    "Ref": "PEServiceSecurityGroup"
                                }
                            ],
                            "DeleteOnTermination": true
                        }
                    ],
                    "ImageId": "ami-094125af156557ca2",
                    "InstanceType": "c5.xlarge",
                    "KeyName": "pe-ec2-key",
                    "TagSpecifications": [
                        {
                            "ResourceType": "instance",
                            "Tags": [
                                {
                                    "Key": "owner",
                                    "Value": "guanglei.bao@pingcap.com - PE service"
                                },
                                {
                                    "Key": "Name",
                                    "Value": "pe-service-01"
                                }
                            ]
                        }
                    ],
                    "UserData": {
                        "Fn::Base64": {
                            "Fn::Join": [
                                "",
                                [
                                    "#!/bin/bash\n",
                                    "echo LANG=en_US.utf-8 >> /etc/environment\n",
                                    "echo LC_ALL=en_US.utf-8 >> /etc/environment\n",
                                    "mkdir ~ec2-user/stage/\n",
                                    "cd ~ec2-user/stage/\n",
                                    "rm -rf tidb-course-201-lab/\n",
                                    "yum -y update\n",
                                    "yum -y install git numactl awscli\n",
                                    "rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022\n",
                                    "wget https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm\n",
                                    "yum -y install mysql57-community-release-el7-10.noarch.rpm\n",
                                    "yum -y install mysql-community-client\n",
                                    "wget https://pingcap-edu.s3.us-west-2.amazonaws.com/mysql-community-server-5.7.41-1.el7.x86_64.rpm\n",
                                    "rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm\n",
                                    "wget https://pingcap-edu.s3.us-west-2.amazonaws.com/tidb-admin-dataset.zip\n",
                                    "git clone https://github.com/pingcap/tidb-course-201-lab.git\n",
                                    "REGION_CODE=`curl http://169.254.169.254/latest/meta-data/placement/region`\n",
                                    "echo export REGION_CODE=${REGION_CODE} > ~ec2-user/cloud-env.sh\n",
                                    "alias cp='cp'\n",
                                    "cp -R tidb-course-201-lab/setup/tidb-lab-aws-1-node-amz2/* ~ec2-user/\n",
                                    "cp -R tidb-course-201-lab/setup/tidb-lab-mysql-init-amz2/show-mysql-password.sh ~ec2-user/\n",
                                    "cd ~\n",
                                    "chown -R ec2-user:ec2-user ~ec2-user/*\n",
                                    "chmod +x ~ec2-user/*.sh\n",
                                    "echo \"if [ -f /home/ec2-user/hosts-env.sh ]; then\" >> ~ec2-user/.bashrc\n",
                                    "echo \"    source /home/ec2-user/hosts-env.sh\" >> ~ec2-user/.bashrc\n",
                                    "echo \"fi;\" >> ~ec2-user/.bashrc\n",
                                    "chown ec2-user:ec2-user ~ec2-user/.bashrc\n"
                                ]
                            ]
                        }
                    }
                }
            }
        },
        "PEServiceASG": {
            "Type": "AWS::AutoScaling::AutoScalingGroup",
            "Properties": {
                "AutoScalingGroupName": "pe-service-asg",
                "LaunchTemplate": {
                    "LaunchTemplateId": {
                        "Ref": "PEServiceLT"
                    },
                    "Version": {
                        "Fn::GetAtt": [
                            "PEServiceLT",
                            "LatestVersionNumber"
                        ]
                    }
                },
                "MaxSize": "1",
                "MinSize": "1",
                "DesiredCapacity": "1",
                "VPCZoneIdentifier": [
                    {
                        "Ref": "PEServiceSubnet"
                    }
                ],
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com - PE service",
                        "PropagateAtLaunch": "true"
                    },
                    {
                        "Key": "trainer",
                        "Value": "guanglei",
                        "PropagateAtLaunch": "true"
                    },
                    {
                        "Key": "role",
                        "Value": "pd1",
                        "PropagateAtLaunch": "true"
                    },
                    {
                        "Key": "student",
                        "Value": "user0",
                        "PropagateAtLaunch": "true"
                    }
                ]
            },
            "DependsOn": [
                "PEIGWAttachment"
            ]
        },
        "PEInstanceRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "ec2.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                },
                "Path": "/"
            }
        },
        "PEInstanceRolePolicy": {
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": "PEInstancePolicy01",
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": "sts:GetCallerIdentity",
                            "Resource": "*"
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "elasticloadbalancing:RegisterTargets",
                                "elasticloadbalancing:DeregisterTargets",
                                "elasticloadbalancing:Describe*"
                            ],
                            "Resource": "*"
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "ec2:Describe*",
                                "ec2:List*"
                            ],
                            "Resource": "*"
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "sqs:*"
                            ],
                            "Resource": "*"
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "s3:List*",
                                "s3:Get*",
                                "s3:Put*",
                                "s3:Delete*"
                            ],
                            "Resource": [
                                "arn:aws:s3:::pe-class/*",
                                "arn:aws:s3:::pe-demo/*"
                            ]
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "s3:List*",
                                "s3:Get*"
                            ],
                            "Resource": [
                                "arn:aws:s3:::pe-class",
                                "arn:aws:s3:::pe-demo"
                            ]
                        }
                    ]
                },
                "Roles": [
                    {
                        "Ref": "PEInstanceRole"
                    }
                ]
            }
        },
        "PEInstanceProfile": {
            "Type": "AWS::IAM::InstanceProfile",
            "Properties": {
                "Path": "/",
                "Roles": [
                    {
                        "Ref": "PEInstanceRole"
                    }
                ]
            }
        }
    }
}