AWSTemplateFormatVersion: '2010-09-09'
Description: >-
  Base network for the autonomous patching workshop: a VPC with public and
  private subnets across two Availability Zones, an Internet Gateway, and a
  NAT Gateway so instances in the private subnets can reach the internet.

Parameters:
  VpcCidr:
    Type: String
    Default: 10.20.0.0/16
    Description: >-
      CIDR range for the VPC. Avoids 172.31.0.0/16, which the default VPC uses.
  ProjectTag:
    Type: String
    Default: awsstudygroup-000099-imagebuilder
    Description: Value applied to the ResourceType tag on every resource.

Resources:
  #------------------------------------------------------------
  # VPC and subnets. Each subnet gets a /24 carved from VpcCidr.
  #------------------------------------------------------------
  Pattern3VPC:
    Type: 'AWS::EC2::VPC'
    Properties:
      CidrBlock: !Ref VpcCidr
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-vpc"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3PublicSubnet1:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref Pattern3VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      CidrBlock: !Select [ 0, !Cidr [ !Ref VpcCidr, 4, 8 ] ]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-PublicSubnet1"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3PublicSubnet2:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref Pattern3VPC
      AvailabilityZone: !Select [ 1, !GetAZs '' ]
      CidrBlock: !Select [ 1, !Cidr [ !Ref VpcCidr, 4, 8 ] ]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-PublicSubnet2"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3PrivateSubnet1:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref Pattern3VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      CidrBlock: !Select [ 2, !Cidr [ !Ref VpcCidr, 4, 8 ] ]
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-PrivateSubnet1"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3PrivateSubnet2:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref Pattern3VPC
      AvailabilityZone: !Select [ 1, !GetAZs '' ]
      CidrBlock: !Select [ 3, !Cidr [ !Ref VpcCidr, 4, 8 ] ]
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-PrivateSubnet2"
        - Key: ResourceType
          Value: !Ref ProjectTag

  #-------------------------------------------------------------
  # Internet Gateway for public subnets, NAT Gateway for private ones
  #-------------------------------------------------------------
  Pattern3IGW:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-igw"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3IGWAttach:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref Pattern3VPC
      InternetGatewayId: !Ref Pattern3IGW

  Pattern3NatPublicIP:
    Type: "AWS::EC2::EIP"
    DependsOn: Pattern3IGWAttach
    Properties:
      Domain: vpc
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-NatPublicIP"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3NatGateway:
    Type: "AWS::EC2::NatGateway"
    Properties:
      AllocationId: !GetAtt Pattern3NatPublicIP.AllocationId
      SubnetId: !Ref Pattern3PublicSubnet1
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-nat"
        - Key: ResourceType
          Value: !Ref ProjectTag

  #-------------------------------------------------------------
  # Public routing: default route to the Internet Gateway
  #-------------------------------------------------------------
  Pattern3PublicRouteTable1:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref Pattern3VPC
      Tags:
        - Key: Network
          Value: Public
        - Key: Name
          Value: !Sub "${AWS::StackName}-PublicRouteTable1"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3PublicRouteTable2:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref Pattern3VPC
      Tags:
        - Key: Network
          Value: Public
        - Key: Name
          Value: !Sub "${AWS::StackName}-PublicRouteTable2"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3PublicRoute1:
    Type: 'AWS::EC2::Route'
    DependsOn: Pattern3IGWAttach
    Properties:
      RouteTableId: !Ref Pattern3PublicRouteTable1
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref Pattern3IGW

  Pattern3PublicRoute2:
    Type: 'AWS::EC2::Route'
    DependsOn: Pattern3IGWAttach
    Properties:
      RouteTableId: !Ref Pattern3PublicRouteTable2
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref Pattern3IGW

  Pattern3PublicSubnet1RouteTableAssociation:
    Type: 'AWS::EC2::SubnetRouteTableAssociation'
    Properties:
      SubnetId: !Ref Pattern3PublicSubnet1
      RouteTableId: !Ref Pattern3PublicRouteTable1

  Pattern3PublicSubnet2RouteTableAssociation:
    Type: 'AWS::EC2::SubnetRouteTableAssociation'
    Properties:
      SubnetId: !Ref Pattern3PublicSubnet2
      RouteTableId: !Ref Pattern3PublicRouteTable2

  #-------------------------------------------------------------
  # Private routing: default route to the NAT Gateway.
  # Required so build instances and application instances can
  # download packages and reach Systems Manager.
  #-------------------------------------------------------------
  Pattern3PrivateRouteTable1:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref Pattern3VPC
      Tags:
        - Key: Network
          Value: Private
        - Key: Name
          Value: !Sub "${AWS::StackName}-PrivateRouteTable1"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3PrivateRouteTable2:
    Type: 'AWS::EC2::RouteTable'
    Properties:
      VpcId: !Ref Pattern3VPC
      Tags:
        - Key: Network
          Value: Private
        - Key: Name
          Value: !Sub "${AWS::StackName}-PrivateRouteTable2"
        - Key: ResourceType
          Value: !Ref ProjectTag

  Pattern3PrivateRoute1:
    Type: 'AWS::EC2::Route'
    DependsOn: Pattern3NatGateway
    Properties:
      RouteTableId: !Ref Pattern3PrivateRouteTable1
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref Pattern3NatGateway

  Pattern3PrivateRoute2:
    Type: 'AWS::EC2::Route'
    DependsOn: Pattern3NatGateway
    Properties:
      RouteTableId: !Ref Pattern3PrivateRouteTable2
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref Pattern3NatGateway

  Pattern3PrivateSubnet1RouteTableAssociation:
    Type: 'AWS::EC2::SubnetRouteTableAssociation'
    Properties:
      SubnetId: !Ref Pattern3PrivateSubnet1
      RouteTableId: !Ref Pattern3PrivateRouteTable1

  Pattern3PrivateSubnet2RouteTableAssociation:
    Type: 'AWS::EC2::SubnetRouteTableAssociation'
    Properties:
      SubnetId: !Ref Pattern3PrivateSubnet2
      RouteTableId: !Ref Pattern3PrivateRouteTable2

#---------------------------------------------------------
# Exported so the application and pipeline stacks can
# import the VPC and subnet IDs.
#---------------------------------------------------------
Outputs:
  OutputPatternStackName:
    Description: Stack name
    Value: !Ref AWS::StackName
  OutputPattern3VPC:
    Description: Baseline VPC
    Value: !Ref Pattern3VPC
    Export:
      Name: !Sub "${AWS::StackName}-VpcId"
  OutputPattern3PublicSubnet1:
    Description: Public Subnet 1
    Value: !Ref Pattern3PublicSubnet1
    Export:
      Name: !Sub "${AWS::StackName}-PublicSubnet1"
  OutputPattern3PublicSubnet2:
    Description: Public Subnet 2
    Value: !Ref Pattern3PublicSubnet2
    Export:
      Name: !Sub "${AWS::StackName}-PublicSubnet2"
  OutputPattern3PrivateSubnet1:
    Description: Private Subnet 1
    Value: !Ref Pattern3PrivateSubnet1
    Export:
      Name: !Sub "${AWS::StackName}-PrivateSubnet1"
  OutputPattern3PrivateSubnet2:
    Description: Private Subnet 2
    Value: !Ref Pattern3PrivateSubnet2
    Export:
      Name: !Sub "${AWS::StackName}-PrivateSubnet2"
