AWSTemplateFormatVersion: '2010-09-09'
Description: This example template describes an Amazon EC2 instance from a user-specified
  Amazon Machine Image (AMI) based on Microsoft Windows. The instance is associated
  to a Systems Manager document to join a domain securely. **WARNING** This template
  creates a Windows instance and related resources. You will be billed for the AWS
  resources used if you create a stack from this template.
Parameters:
  ADStackName:
    AllowedPattern: '[a-zA-Z]+[-a-zA-Z0-9]*'
    Description: The name of the CloudFormation stack that has created the Active
      Directory set-up on Amazon EC2.
    MaxLength: '255'
    MinLength: '1'
    Type: String
  DomainJoinSSMDocumentStackName:
    AllowedPattern: '[a-zA-Z]+[-a-zA-Z0-9]*'
    Description: The name of the CloudFormation stack that has created the domain
      join SSM Document.
    MaxLength: '255'
    MinLength: '1'
    Type: String
  ImageID:
    AllowedPattern: ami-[a-z0-9]+
    ConstraintDescription: please specify an AMI ID.
    Description: ID of the AMI to use for the EC2 instance.
    Type: String
  InstanceProfileStackName:
    AllowedPattern: '[a-zA-Z]+[-a-zA-Z0-9]*'
    Description: The name of the CloudFormation stack that has created the InstanceProfile
      Role.
    MaxLength: '255'
    MinLength: '1'
    Type: String
  InstanceType:
    AllowedValues:
    - t1.micro
    - t2.micro
    - t2.small
    - t2.medium
    - m1.small
    - m1.medium
    - m1.large
    - m1.xlarge
    - m2.xlarge
    - m2.2xlarge
    - m2.4xlarge
    - m3.medium
    - m3.large
    - m3.xlarge
    - m3.2xlarge
    - m4.large
    - m4.xlarge
    - m4.2xlarge
    - m4.4xlarge
    - m4.10xlarge
    - c1.medium
    - c1.xlarge
    - c3.large
    - c3.xlarge
    - c3.2xlarge
    - c3.4xlarge
    - c3.8xlarge
    - c4.large
    - c4.xlarge
    - c4.2xlarge
    - c4.4xlarge
    - c4.8xlarge
    - g2.2xlarge
    - r3.large
    - r3.xlarge
    - r3.2xlarge
    - r3.4xlarge
    - r3.8xlarge
    - i2.xlarge
    - i2.2xlarge
    - i2.4xlarge
    - i2.8xlarge
    - d2.xlarge
    - d2.2xlarge
    - d2.4xlarge
    - d2.8xlarge
    - hi1.4xlarge
    - hs1.8xlarge
    - cr1.8xlarge
    - cc2.8xlarge
    - cg1.4xlarge
    ConstraintDescription: must be a valid EC2 instance type.
    Default: t2.micro
    Description: Amazon EC2 instance type
    Type: String
  KeyName:
    ConstraintDescription: must be the name of an existing EC2 KeyPair.
    Description: Name of an existing EC2 KeyPair
    Type: AWS::EC2::KeyPair::KeyName
Resources:
  DomainJoinSSMDocumentAssociation:
    Type: AWS::SSM::Association
    Properties:
      Name: !ImportValue
        Fn::Sub: ${DomainJoinSSMDocumentStackName}-Document
      Targets:
      - Key: InstanceIds
        Values:
        - !Ref 'EC2Instance'
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      IamInstanceProfile: !ImportValue
        Fn::Sub: ${InstanceProfileStackName}-InstanceProfile
      ImageId: !Ref 'ImageID'
      InstanceType: !Ref 'InstanceType'
      KeyName: !Ref 'KeyName'
      SecurityGroupIds:
      - !ImportValue
        Fn::Sub: ${ADStackName}-DomainMemberSecurityGroupID
      Tags:
      - Key: Name
        Value: !Sub '${AWS::StackName}-ExampleDomainJoin'
Outputs:
  EC2InstanceDNSName:
    Description: Public DNS name of the EC2 instance.
    Value: !GetAtt 'EC2Instance.PublicDnsName'
  InstanceID:
    Description: ID of the EC2 instance.
    Value: !Ref 'EC2Instance'

