# Infrastructure as Code 
# Exercise solution
# Carlos Rivas February 2021

AWSTemplateFormatVersion: "2010-09-09"
Description: Creates the required network infrastructure for Public and Private routing with Internet access
Parameters:
  VPCCIDR:
    Type: String
  PrivSubnetCIDR:
    Type: String
  PubSubnetCIDR:
    Type: String
Resources:
  MainVPC:
    Type: AWS::EC2::VPC
    Properties: 
      CidrBlock: !Ref VPCCIDR
      Tags: 
        - Key: Name
          Value: "Main VPC"
  InternetGW:
    Type: AWS::EC2::InternetGateway
  AttachInternetGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref MainVPC
      InternetGatewayId: !Ref InternetGW
  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties: 
      # Availability zone at your discretion or
      # as a parameter
      AvailabilityZone: 'us-east-1a'
      CidrBlock: !Ref PubSubnetCIDR
      MapPublicIpOnLaunch: true
      Tags: 
        - Key: Name
          Value: "Public Subnet AZ1"
      VpcId: !Ref MainVPC
  PrivateSubnet1:
    Type: AWS::EC2::Subnet
    Properties: 
      AvailabilityZone: 'us-east-1b'
      CidrBlock: !Ref PrivSubnetCIDR
      MapPublicIpOnLaunch: false
      Tags: 
        - Key: Name
          Value: "Private Subnet AZ1"
      VpcId: !Ref MainVPC
  NAT:
    Type: AWS::EC2::NatGateway
    Properties:
        AllocationId:
          Fn::GetAtt:
          - EIP
          - AllocationId
        SubnetId: !Ref PublicSubnet1
        Tags:
        - Key: Name
          Value: "NAT to be used by servers in the private subnet"
  EIP:
    Type: AWS::EC2::EIP
    Properties:
        Tags:
        - Key: Name
          Value: "Elastic IP for our NAT Gateway"
  PrivateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties: 
      Tags: 
        - Key: Name
          Value: "Private Route Table"
      VpcId: !Ref MainVPC
  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties: 
      Tags: 
        - Key: Name
          Value: "Public Route Table"
      VpcId: !Ref MainVPC
  AssociatePUBRT:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties: 
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet1
  AssociatePRIVRT:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties: 
      RouteTableId: !Ref PrivateRouteTable
      SubnetId: !Ref PrivateSubnet1
  PublicInternetRoute:
    Type: AWS::EC2::Route
    Properties:
       RouteTableId:
         Ref: PublicRouteTable
       DestinationCidrBlock: 0.0.0.0/0
       GatewayId:
         Ref: InternetGW
  PrivateInternetOutboundRoute:
    Type: AWS::EC2::Route
    Properties:
       RouteTableId:
         Ref: PrivateRouteTable
       DestinationCidrBlock: 0.0.0.0/0
       NatGatewayId:
         Ref: NAT