{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Mappings": {
        "UserDataMap": {
            "us-west-2": {
                "common": "Placeholder"
            }
        }
    },
    "Resources": {
        "LabControlVPC": {
            "Type": "AWS::EC2::VPC",
            "Properties": {
                "CidrBlock": "10.210.0.0/16",
                "EnableDnsSupport": "true",
                "EnableDnsHostnames": "true",
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com"
                    },
                    {
                        "Key": "Name",
                        "Value": "Lab Control V2 VPC"
                    }
                ]
            }
        },
        "LabControlIGW": {
            "Type": "AWS::EC2::InternetGateway",
            "Properties": {
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com"
                    }
                ]
            },
            "DependsOn": [
                "LabControlVPC"
            ]
        },
        "IGWA": {
            "Type": "AWS::EC2::VPCGatewayAttachment",
            "Properties": {
                "InternetGatewayId": {
                    "Ref": "LabControlIGW"
                },
                "VpcId": {
                    "Ref": "LabControlVPC"
                }
            }
        },
        "PublicRouteTable": {
            "Type": "AWS::EC2::RouteTable",
            "Properties": {
                "VpcId": {
                    "Ref": "LabControlVPC"
                },
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com"
                    },
                    {
                        "Key": "Name",
                        "Value": "Lab Control V2 VPC Public Route"
                    }
                ]
            }
        },
        "PublicRoute": {
            "Type": "AWS::EC2::Route",
            "Properties": {
                "DestinationCidrBlock": "0.0.0.0/0",
                "GatewayId": {
                    "Ref": "LabControlIGW"
                },
                "RouteTableId": {
                    "Ref": "PublicRouteTable"
                }
            },
            "DependsOn": [
                "IGWA"
            ]
        },
        "SN1": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "VpcId": {
                    "Ref": "LabControlVPC"
                },
                "CidrBlock": "10.210.2.0/24",
                "AvailabilityZone": "us-west-2a",
                "PrivateDnsNameOptionsOnLaunch": {
                    "EnableResourceNameDnsAAAARecord": false,
                    "EnableResourceNameDnsARecord": true,
                    "HostnameType": "ip-name"
                },
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com"
                    },
                    {
                        "Key": "Name",
                        "Value": {
                            "Fn::Join": [
                                "-",
                                [
                                    "lab-control-subnet-v2",
                                    "1"
                                ]
                            ]
                        }
                    }
                ]
            }
        },
        "SN2": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "VpcId": {
                    "Ref": "LabControlVPC"
                },
                "CidrBlock": "10.210.3.0/24",
                "AvailabilityZone": "us-west-2b",
                "PrivateDnsNameOptionsOnLaunch": {
                    "EnableResourceNameDnsAAAARecord": false,
                    "EnableResourceNameDnsARecord": true,
                    "HostnameType": "ip-name"
                },
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com"
                    },
                    {
                        "Key": "Name",
                        "Value": {
                            "Fn::Join": [
                                "-",
                                [
                                    "lab-control-subnet-v2",
                                    "2"
                                ]
                            ]
                        }
                    }
                ]
            }
        },
        "SRTA1": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "SN1"
                },
                "RouteTableId": {
                    "Ref": "PublicRouteTable"
                }
            }
        },
        "SRTA2": {
            "Type": "AWS::EC2::SubnetRouteTableAssociation",
            "Properties": {
                "SubnetId": {
                    "Ref": "SN2"
                },
                "RouteTableId": {
                    "Ref": "PublicRouteTable"
                }
            }
        },
        "LabControlSG": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": "Allow nodes communication.",
                "VpcId": {
                    "Ref": "LabControlVPC"
                },
                "SecurityGroupIngress": [
                    {
                        "IpProtocol": "tcp",
                        "FromPort": 443,
                        "ToPort": 443,
                        "CidrIp": "0.0.0.0/0"
                    }
                ],
                "SecurityGroupEgress": [
                    {
                        "IpProtocol": "-1",
                        "CidrIp": "0.0.0.0/0"
                    }
                ],
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com"
                    },
                    {
                        "Key": "Name",
                        "Value": {
                            "Fn::Join": [
                                "-",
                                [
                                    "lab-control-v2",
                                    "sg"
                                ]
                            ]
                        }
                    }
                ]
            }
        },
        "IRule": {
            "Type": "AWS::EC2::SecurityGroupIngress",
            "Properties": {
                "IpProtocol": "-1",
                "SourceSecurityGroupId": {
                    "Fn::GetAtt": [
                        "LabControlSG",
                        "GroupId"
                    ]
                },
                "GroupId": {
                    "Fn::GetAtt": [
                        "LabControlSG",
                        "GroupId"
                    ]
                }
            }
        },
        "TG": {
            "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
            "Properties": {
                "HealthCheckEnabled": "True",
                "HealthCheckIntervalSeconds": 5,
                "HealthCheckPort": "443",
                "HealthCheckProtocol": "TCP",
                "HealthCheckTimeoutSeconds": 5,
                "HealthyThresholdCount": 2,
                "Name": "lab-control-v2-target-group",
                "Port": 443,
                "Protocol": "TCP",
                "VpcId": {
                    "Ref": "LabControlVPC"
                }
            }
        },
        "NLB": {
            "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
            "Properties": {
                "Type": "network",
                "Name": "lab-control-v2-nlb",
                "SecurityGroups": [
                    {
                        "Ref": "LabControlSG"
                    }
                ],
                "Scheme": "internet-facing",
                "Subnets": [
                    {
                        "Ref": "SN1"
                    },
                    {
                        "Ref": "SN2"
                    }
                ]
            }
        },
        "LSNR": {
            "Type": "AWS::ElasticLoadBalancingV2::Listener",
            "Properties": {
                "DefaultActions": [
                    {
                        "Type": "forward",
                        "ForwardConfig": {
                            "TargetGroups": [
                                {
                                    "TargetGroupArn": {
                                        "Ref": "TG"
                                    }
                                }
                            ]
                        }
                    }
                ],
                "LoadBalancerArn": {
                    "Ref": "NLB"
                },
                "Port": 443,
                "Protocol": "TCP"
            }
        },
        "MLT": {
            "Type": "AWS::EC2::LaunchTemplate",
            "Properties": {
                "VersionDescription": "v1",
                "LaunchTemplateName": {
                    "Fn::Join": [
                        "-",
                        [
                            "lab-control",
                            "lt-v2"
                        ]
                    ]
                },
                "LaunchTemplateData": {
                    "IamInstanceProfile": {
                        "Name": "lab-control-service-role"
                    },
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda",
                            "Ebs": {
                                "DeleteOnTermination": true,
                                "Encrypted": false,
                                "VolumeSize": 600,
                                "VolumeType": "gp2"
                            }
                        }
                    ],
                    "NetworkInterfaces": [
                        {
                            "DeviceIndex": 0,
                            "AssociatePublicIpAddress": true,
                            "Groups": [
                                {
                                    "Ref": "LabControlSG"
                                }
                            ],
                            "DeleteOnTermination": true
                        }
                    ],
                    "ImageId": "ami-071d4ef23b7e8538f",
                    "InstanceType": "c5.xlarge",
                    "KeyName": "pe-ec2-key",
                    "TagSpecifications": [
                        {
                            "ResourceType": "instance",
                            "Tags": [
                                {
                                    "Key": "owner",
                                    "Value": "guanglei.bao@pingcap.com"
                                },
                                {
                                    "Key": "Name",
                                    "Value": "lab-control-v2"
                                },
                                {
                                    "Key": "role",
                                    "Value": "system"
                                },
                                {
                                    "Key": "student",
                                    "Value": "N/A"
                                }
                            ]
                        }
                    ],
                    "UserData": {
                        "Fn::Base64": {
                            "Fn::Join": [
                                "\n",
                                [
                                    "#!/bin/bash",
                                    "echo LANG=en_US.utf-8 > /etc/environment",
                                    "echo LC_ALL=en_US.utf-8 >> /etc/environment",
                                    "mkdir ~ec2-user/stage/ 2>/dev/null",
                                    "cd ~ec2-user/stage/",
                                    "yum -y install git awscli httpd mod_ssl",
                                    "TOKEN=`curl -X PUT \"http://169.254.169.254/latest/api/token\" -H \"X-aws-ec2-metadata-token-ttl-seconds: 21600\"`",
                                    "REGION_CODE=`curl -H \"X-aws-ec2-metadata-token: $TOKEN\" -v http://169.254.169.254/latest/meta-data/placement/region`",
                                    "INSTANCE_ID=`curl -H \"X-aws-ec2-metadata-token: $TOKEN\" -v http://169.254.169.254/latest/meta-data/instance-id`",
                                    "echo export REGION_CODE=${REGION_CODE} > ~ec2-user/cloud-env.sh",
                                    "echo export INSTANCE_ID=${INSTANCE_ID} >> ~ec2-user/cloud-env.sh",
                                    "alias cp='cp'",
                                    "su - ec2-user -c \"pip3 install boto3\"",
                                    "chown -R ec2-user:ec2-user ~ec2-user/*",
                                    "chmod +x ~ec2-user/*.sh",
                                    "amazon-linux-extras install -y php8.0",
                                    "usermod -a -G apache ec2-user",
                                    "chown -R ec2-user:apache /var/www",
                                    "chmod 2775 /var/www && find /var/www -type d -exec chmod 2775 {} \\;",
                                    "find /var/www -type f -exec chmod 0664 {} \\;",
                                    "aws s3 cp s3://pe-lab-control/config_v2/pingcap.net.cert /etc/pki/tls/certs/",
                                    "aws s3 cp s3://pe-lab-control/config_v2/pingcap.net_chain.cert /etc/pki/tls/certs/",
                                    "aws s3 cp s3://pe-lab-control/config_v2/ca.cert /etc/pki/tls/certs/",
                                    "chmod 644 /etc/pki/tls/certs/*.cert",
                                    "aws s3 cp s3://pe-lab-control/config_v2/pingcap.net.key /etc/pki/tls/private/",
                                    "chmod 600 /etc/pki/tls/private/pingcap.net.key",
                                    "aws s3 cp s3://pe-lab-control/config_v2/ssl.conf /etc/httpd/conf.d/",
                                    "aws s3 cp s3://pe-lab-control/config_v2/httpd.conf /etc/httpd/conf/",
                                    "chmod 644 /etc/httpd/conf.d/ssl.conf",
                                    "chmod 644 /etc/httpd/conf/httpd.conf",
                                    "su - ec2-user -c \"aws s3 sync s3://pe-lab-control/ui_v2/ /var/www/html/\"",
                                    "rm -f google-api-php-client--PHP8.0.zip",
                                    "wget https://pingcap-edu.s3.us-west-2.amazonaws.com/google-api-php-client--PHP8.0.zip",
                                    "unzip -u google-api-php-client--PHP8.0.zip -d /var/www/html/google-api-php-client",
                                    "rm -f google-api-php-client--PHP8.0.zip",
                                    "mkdir /var/www/wallet/ 2>/dev/null",
                                    "aws s3 cp s3://pe-lab-control/config_v2/client_auth_config_lab.pingcap.net.json /var/www/wallet/",
                                    "chown -R ec2-user:apache /var/www/wallet/",
                                    "rm -f /etc/httpd/conf.d/userdir.conf",
                                    "rm -f /etc/httpd/conf.d/autoindex.conf",
                                    "systemctl restart httpd",
                                    "systemctl enable httpd",
                                    "aws s3 sync s3://pe-lab-control/lab_control_scripts_v2/ /usr/share/httpd/lab_control_scripts/",
                                    "chown -R apache /usr/share/httpd/lab_control_scripts/",
                                    "chmod -R 550 /usr/share/httpd/lab_control_scripts/",
                                    "chown apache /usr/share/httpd",
                                    "cp /etc/passwd /etc/passwd.bak",
                                    "sed -i '' -e \"s/Apache:\\/usr\\/share\\/httpd:\\/sbin\\/nologin/Apache:\\/usr\\/share\\/httpd:\\/bin\\/bash/g\" /etc/passwd 2>/dev/null",
                                    "su - apache -c \"pip3 install boto3\"",
                                    "mkdir /usr/share/httpd/.ssh/ 2>/dev/null",
                                    "aws s3 sync s3://pe-lab-control/config_v2/keys /usr/share/httpd/.ssh/",
                                    "chown -R apache /usr/share/httpd/.ssh/",
                                    "chmod 400 /usr/share/httpd/.ssh/*/*.pem",
                                    "mkdir /usr/share/httpd/logs/ 2>/dev/null",
                                    "chown apache:apache /usr/share/httpd/logs/",
                                    "chown root:root /var/www/html/.htaccess",
                                    "chmod 644 /var/www/html/.htaccess",
                                    "chown root:root /var/www/html/lab-controller/.htaccess",
                                    "chmod 644 /var/www/html/lab-controller/.htaccess",
                                    "su - apache -c \"curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh\"",
                                    "su - apache -c \"tiup install cloud\"",
                                    "su - apache -c \"tiup cloud:v1.0.0-beta.1\"",
                                    "aws s3 cp s3://pe-lab-control/config_v2/config.toml /usr/share/httpd/.ticloud/",
                                    "chown apache:apache /usr/share/httpd/.ticloud/config.toml",
                                    "chmod 600 /usr/share/httpd/.ticloud/config.toml",
                                    "su - apache -c \"pip3 install requests\"",
                                    "su - apache -c \"pip3 install mysql-connector\"",
                                    "mkdir /usr/share/httpd/lab_control_scripts/lib/{english,japanese,chinese}",
                                    "aws s3 cp --recursive s3://pe-lab-control/lab_control_scripts_v2/lib/ /usr/share/httpd/lab_control_scripts/lib/",
                                    "chown -R apache /usr/share/httpd/lab_control_scripts/lib/",
                                    "chmod -R 550 /usr/share/httpd/lab_control_scripts/lib/"
                                ]
                            ]
                        }
                    }
                }
            }
        },
        "MG": {
            "Type": "AWS::AutoScaling::AutoScalingGroup",
            "Properties": {
                "AutoScalingGroupName": {
                    "Fn::Join": [
                        "-",
                        [
                            "lab-control-v2",
                            "asg"
                        ]
                    ]
                },
                "LaunchTemplate": {
                    "LaunchTemplateId": {
                        "Ref": "MLT"
                    },
                    "Version": {
                        "Fn::GetAtt": [
                            "MLT",
                            "LatestVersionNumber"
                        ]
                    }
                },
                "MaxSize": "1",
                "MinSize": "1",
                "DesiredCapacity": "1",
                "VPCZoneIdentifier": [
                    {
                        "Ref": "SN1"
                    },
                    {
                        "Ref": "SN2"
                    }
                ],
                "TargetGroupARNs": [
                    {
                        "Ref": "TG"
                    }
                ],
                "Tags": [
                    {
                        "Key": "owner",
                        "Value": "guanglei.bao@pingcap.com",
                        "PropagateAtLaunch": "true"
                    }
                ]
            },
            "DependsOn": [
                "IGWA"
            ]
        }
    }
}