---
AWSTemplateFormatVersion: "2010-09-09"
Description: Aviatrix FlightCheck Automation
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: Network Configuration
        Parameters:
          - VPCParam
          - SubnetParam
      - Label:
          default: Amazon EC2 Configuration
        Parameters:
          - KeyNameParam
          - InstanceTypeParam
    ParameterLabels:
      VPCParam:
        default: Which VPC should Aviatrix FlightCheck be deployed in?
      SubnetParam:
        default: Which public subnet in the VPC?
      KeyNameParam:
        default: Which keypair will be used?
      InstanceTypeParam:
        default: Select Controller Size
Parameters:
  LatestAmiId:
    Type: "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>"
    Default: "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
  VPCParam:
    Type: AWS::EC2::VPC::Id
    Description: Select Your VPC
  SubnetParam:
    Type: AWS::EC2::Subnet::Id
    Description: Aviatrix FlightCheck must be launched on a Public Subnet
  KeyNameParam:
    Type: AWS::EC2::KeyPair::KeyName
    Description: Select your Keypair
  InstanceTypeParam:
    Type: String
    Default: t3.medium
    AllowedValues:
      - t3.medium
      - t2.small
    Description: Select an instance size for Aviatrix FlightCheck. Default is t3.medium
Resources:
  AviatrixFlightCheck:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref LatestAmiId
      DisableApiTermination: "false"
      KeyName:
        Ref: KeyNameParam
      InstanceType:
        Ref: InstanceTypeParam
      NetworkInterfaces:
        - DeviceIndex: "0"
          GroupSet:
            - Ref: AviatrixSG
          SubnetId:
            Ref: SubnetParam
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash -xe
          sudo yum update -y
          sudo yum install python3
          sudo yum install python3-pip -y
          sudo yum install nc -y
          pip3 install aviatrix-flightcheck
          cd /home/ec2-user
          echo 'ClientAliveInterval 60' | sudo tee --append /etc/ssh/sshd_config
          sudo systemctl restart sshd.service
          logout
      Tags:
        - Key: Name
          Value: AviatrixFlightCheck
  AviatrixEIP:
    Type: AWS::EC2::EIP
    DeletionPolicy: Delete
    Properties:
      InstanceId:
        Ref: AviatrixFlightCheck
      Domain: vpc
  AviatrixSG:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Aviatrix - Allow SSH access to Aviatrix FlightCheck Instance
      VpcId:
        Ref: VPCParam
      SecurityGroupIngress:
        - IpProtocol: tcp
          CidrIp: 0.0.0.0/0
          FromPort: "22"
          ToPort: "22"
      Tags:
        - Key: Name
          Value: AviatrixFlightCheckSecurityGroup
Outputs:
  AviatrixFlightCheckEIP:
    Description: Aviatrix FlightCheck External IP
    Value:
      Fn::GetAtt:
        - AviatrixFlightCheck
        - PublicIp
  AviatrixFlightCheckPrivateIP:
    Description: Aviatrix FlightCheck Private IP
    Value:
      Fn::GetAtt:
        - AviatrixFlightCheck
        - PrivateIp
