AWSTemplateFormatVersion: 2010-09-09
Description: Setup a License Server with FlexLM

Parameters:
  SubnetId:
    Type: AWS::EC2::Subnet::Id
  InstanceType:
    Description: "Specify the Instance type - defaults to t3.micro."
    Type: String
    Default: t3.micro
  Keypair:
    Description: "SSH Keypair"
    Type: AWS::EC2::KeyPair::KeyName
  AmiID:
    Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
    Description: "The ID of the AMI."
    Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2

Resources:
  FlexLMInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref AmiID
      InstanceType: !Ref InstanceType
      KeyName: !Ref Keypair
      DisableApiTermination: True
      NetworkInterfaces:
        - DeleteOnTermination: False
          Description: FlexLM ENI
          DeviceIndex: 0
          NetworkInterfaceId: !Ref FlexLMENI
      Tags:
        - Key : "Name"
          Value : "FlexLM License Server"

  FlexLMSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Security Group for FlexLM Instance
      GroupName: !Sub ${AWS::StackName}-SecurityGroup
  
  FlexLMIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
      InstanceId: !Ref FlexLMInstance

  FlexLMENI:
    Type: AWS::EC2::NetworkInterface
    Properties: 
      Description: ENI for FlexLM License Server
      GroupSet:
        - !GetAtt FlexLMSecurityGroup.GroupId
      SubnetId: !Ref SubnetId

  FlexLMEIPAssociation:
    Type: AWS::EC2::EIPAssociation
    Properties: 
      AllocationId: !GetAtt FlexLMIP.AllocationId
      NetworkInterfaceId: !Ref FlexLMENI

Outputs:
  IpAddress:
    Value: !Ref FlexLMIP
    Description: Instance ID of the FlexLM instance
  InstanceId:
    Value: !Ref FlexLMInstance
    Description: Instance ID of the FlexLM instance