---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Amazon Local EKS Sample VPC - Private and Public subnets'

Parameters:

  VpcBlock:
    Type: String
    Default: 192.168.0.0/16
    Description: The CIDR range for the VPC. This should be a valid private (RFC 1918) CIDR range.

  OutpostArn:
    Type: String
    Description: Outpost Arn 
    AllowedPattern: ^arn:aws:outposts:.+:.+:outpost\/.+

  OutpostAz:
    Type: String
    Description: Outpost Availability Zone
    AllowedPattern: ^.+

  PublicSubnetBlock:
    Type: String
    Default: 192.168.0.0/18
    Description: CidrBlock for public subnet within the VPC

  PrivateSubnetBlock:
    Type: String
    Default: 192.168.128.0/18
    Description: CidrBlock for private subnet within the VPC

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      -
        Label:
          default: "Worker Network Configuration"
        Parameters:
          - VpcBlock
          - OutpostArn
          - OutpostAz
          - PublicSubnetBlock
          - PrivateSubnetBlock

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock:  !Ref VpcBlock
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
      - Key: Name
        Value: !Sub '${AWS::StackName}-VPC'

  InternetGateway:
    Type: "AWS::EC2::InternetGateway"

  VPCGatewayAttachment:
    Type: "AWS::EC2::VPCGatewayAttachment"
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
      - Key: Name
        Value: public-route-table
      - Key: Network
        Value: Public

  PrivateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
      - Key: Name
        Value: private-route-table
      - Key: Network
        Value: Private

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

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

  NatGateway:
    DependsOn:
    - NatGatewayEIP
    - PublicSubnet
    - VPCGatewayAttachment
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt 'NatGatewayEIP.AllocationId'
      SubnetId: !Ref PublicSubnet
      Tags:
      - Key: Name
        Value: !Sub '${AWS::StackName}-NatGatewayAZ'

  NatGatewayEIP:
    DependsOn:
    - VPCGatewayAttachment
    Type: 'AWS::EC2::EIP'
    Properties:
      Domain: vpc

  PublicSubnet:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Public Subnet for EKS local cluster
    Properties:
      MapPublicIpOnLaunch: true
      AvailabilityZone: !Ref OutpostAz
      CidrBlock:
        Ref: PublicSubnetBlock
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-PublicSubnet"

  PrivateSubnet:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Private Subnet in outpost for EKS local cluster
    Properties:
      AvailabilityZone: !Ref OutpostAz
      CidrBlock:
        Ref: PrivateSubnetBlock
      VpcId:
        Ref: VPC
      OutpostArn: !Ref OutpostArn
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-PrivateSubnet"

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

  PrivateSubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnet
      RouteTableId: !Ref PrivateRouteTable

Outputs:

  PublicSubnetId:
    Description: Public Subnet ID in the VPC
    Value: !Ref PublicSubnet

  PrivateSubnetId:
    Description: Private Subnet ID in the VPC
    Value: !Ref PrivateSubnet

  VpcId:
    Description: The VPC Id
    Value: !Ref VPC
