AWSTemplateFormatVersion: '2010-09-09'
Description: 'Amazon EKS - VPC'

Parameters:
  ProjectName:
    Type: String
    Default: "app"
  AvailabilityZones:
    Description: 'List of Availability Zones to use for the subnets in the VPC. Note:
      The logical order is preserved.'
    Type: List<AWS::EC2::AvailabilityZone::Name>
  VPCStackName:
    Type: String
    Description: VPC Stack Name
    Default: "eks-vpc"

Resources:
  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
      - Key: Name
        Value: !Sub "eks-${ProjectName}"
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsSupport: 'true'
      EnableDnsHostnames: 'true'
      InstanceTenancy: default
      Tags:
      - Key: Name
        Value: !Sub "${ProjectName}-${VPCStackName}"
  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId:
        Ref: VPC
      InternetGatewayId:
        Ref: InternetGateway
  subnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: VPC
      CidrBlock: 10.0.0.0/24
      AvailabilityZone:
        Fn::Select:
        - '0'
        - Ref: AvailabilityZones
      Tags:
      - Key: Name
        Value: !Sub "${ProjectName}-${VPCStackName}-a"
  subnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: VPC
      CidrBlock: 10.0.1.0/24
      AvailabilityZone:
        Fn::Select:
        - '1'
        - Ref: AvailabilityZones
      Tags:
      - Key: Name
        Value: !Sub "${ProjectName}-${VPCStackName}-b"
  subnetARouteTable:
      Type: AWS::EC2::RouteTable
      Properties:
        VpcId:
          Ref: VPC
        Tags:
        - Key: Name
          Value: Private subnet A
        - Key: Network
          Value: Private
  subnetARoute:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId:
        Ref: subnetARouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId:
        Ref: InternetGateway
  subnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId:
        Ref: subnetA
      RouteTableId:
        Ref: subnetARouteTable
  subnetBRouteTable:
      Type: AWS::EC2::RouteTable
      Properties:
        VpcId:
          Ref: VPC
        Tags:
        - Key: Name
          Value: Private subnet B
        - Key: Network
          Value: Private
  subnetBRoute:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId:
        Ref: subnetBRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId:
        Ref: InternetGateway
  subnetBRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId:
        Ref: subnetB
      RouteTableId:
        Ref: subnetBRouteTable

Outputs:
  VPCID:
    Value:
      Ref: VPC
    Description: VPC ID
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-VPCID"
  subnetAID:
    Description: SubnetA ID
    Value:
      Ref: subnetA
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-subnetAID"
  subnetBID:
    Description: SubnetB ID
    Value:
      Ref: subnetB
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-subnetBID"
  VPCCIDR:
    Value: !GetAtt VPC.CidrBlock
    Description: VPC CIDR Block
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-VPCCIDR"
