AWSTemplateFormatVersion: '2010-09-09'
Description: 'Base Infrastructure for AWS Managed AD Workshop (VPC, 1 Public Subnet, 2 Private Subnets, IGW, NAT Gateway, Route Tables, Security Group, IAM Role)'

Resources:
  # 1. VPC
  WorkshopVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: aws-managed-ad-vpc

  # 2. Internet Gateway
  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: aws-managed-ad-igw

  VPCGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref WorkshopVPC
      InternetGatewayId: !Ref InternetGateway

  # 3. Subnets
  PublicSubnet1A:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref WorkshopVPC
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: !Select [0, !GetAZs '']
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: aws-managed-ad-public-subnet-1a

  PrivateSubnet1A:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref WorkshopVPC
      CidrBlock: 10.0.2.0/24
      AvailabilityZone: !Select [0, !GetAZs '']
      Tags:
        - Key: Name
          Value: aws-managed-ad-private-subnet-1a

  PrivateSubnet1B: # Directory Service requires at least 2 AZs
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref WorkshopVPC
      CidrBlock: 10.0.3.0/24
      AvailabilityZone: !Select [1, !GetAZs '']
      Tags:
        - Key: Name
          Value: aws-managed-ad-private-subnet-1b

  # 4. Elastic IP & NAT Gateway
  NatEIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
      Tags:
        - Key: Name
          Value: aws-managed-ad-eip

  NatGateway:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NatEIP.AllocationId
      SubnetId: !Ref PublicSubnet1A
      Tags:
        - Key: Name
          Value: aws-managed-ad-nat

  # 5. Route Tables
  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref WorkshopVPC
      Tags:
        - Key: Name
          Value: aws-managed-ad-public-rt

  PublicRoute:
    Type: AWS::EC2::Route
    DependsOn: VPCGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  PublicSubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet1A
      RouteTableId: !Ref PublicRouteTable

  PrivateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref WorkshopVPC
      Tags:
        - Key: Name
          Value: aws-managed-ad-private-rt

  PrivateRoute:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway

  PrivateSubnet1ARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnet1A
      RouteTableId: !Ref PrivateRouteTable

  PrivateSubnet1BRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnet1B
      RouteTableId: !Ref PrivateRouteTable

  # 6. Security Group
  VPCSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow RDP and ICMP for Managed AD Workshop EC2 instances
      VpcId: !Ref WorkshopVPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 3389
          ToPort: 3389
          CidrIp: 0.0.0.0/0
          Description: "Allow RDP from anywhere"
        - IpProtocol: icmp
          FromPort: -1
          ToPort: -1
          CidrIp: 10.0.0.0/16
          Description: "Allow Ping within VPC"
      Tags:
        - Key: Name
          Value: aws-managed-ad-sg

  # 7. IAM Role & Instance Profile for Domain Join
  EC2DomainJoinRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: aws-managed-ad-ec2-role
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - ec2.amazonaws.com
            Action:
              - sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
        - arn:aws:iam::aws:policy/AmazonSSMDirectoryServiceAccess
      Tags:
        - Key: Name
          Value: aws-managed-ad-ec2-role

  EC2DomainJoinInstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      InstanceProfileName: aws-managed-ad-ec2-profile
      Roles:
        - !Ref EC2DomainJoinRole

Outputs:
  VPCID:
    Description: Use this VPC ID to deploy Managed AD and EC2
    Value: !Ref WorkshopVPC
  SecurityGroupID:
    Description: Use this Security Group for your EC2 instances
    Value: !Ref VPCSecurityGroup
  IAMInstanceProfile:
    Description: Use this IAM Profile for your EC2 instances to allow Domain Join
    Value: !Ref EC2DomainJoinInstanceProfile
