AWSTemplateFormatVersion: '2010-09-09'

Description: TGW HandsOn VPC for Outbound

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: "VPC CIDR for VPC-Outbound"
        Parameters:
          - VpcCidr1
      - Label:
          default: "Subnet CIDR for Subnet-Outbound"
        Parameters:          
          - VPC1PubSubCidr1
          - VPC1PrvSubCidr1

Parameters:
  VpcCidr1:
    Description: CIDR Block for the VPC-Outbound
    Type: String
    MinLength: 9
    MaxLength: 18
    Default: 10.1.4.0/24
    AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
    ConstraintDescription: Must be like 10.1.0.0/16

  VPC1PubSubCidr1:
    Description: CIDR Block for Public Subnet
    Type: String
    MinLength: 9
    MaxLength: 18
    Default: 10.1.4.0/26
    AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
    ConstraintDescription: Must be like 10.1.1.0/24

  VPC1PrvSubCidr1:
    Description: CIDR Block for Private Subnet
    Type: String
    MinLength: 9
    MaxLength: 18
    Default: 10.1.4.64/26
    AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
    ConstraintDescription: Must be like 10.1.1.0/24

Resources:
#
# VPC-Outbound
#
  VPC1:
    Type: 'AWS::EC2::VPC'
    Properties:
      CidrBlock: !Ref VpcCidr1
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: VPC-Outbound

  VPC1PrvSub1:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref VPC1
      CidrBlock: !Ref VPC1PrvSubCidr1
      AvailabilityZone: ap-northeast-1a
      Tags:
        - Key: Name
          Value: VPCOutPrvSub1

  VPC1PrivateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC1
      Tags:
        - Key: Name
          Value: VPC Outbound Private RouteTable

  VPC1PrvSub1Assoc:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref VPC1PrvSub1
      RouteTableId: !Ref VPC1PrivateRouteTable

  VPC1PubSub1:
    Type: 'AWS::EC2::Subnet'
    Properties:
      VpcId: !Ref VPC1
      CidrBlock: !Ref VPC1PubSubCidr1
      AvailabilityZone: ap-northeast-1a
      Tags:
        - Key: Name
          Value: VPCOutPubSub1

  VPC1PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC1
      Tags:
        - Key: Name
          Value: VPC Outbound Public RouteTable

  VPC1PubSub1Assoc:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref VPC1PubSub1
      RouteTableId: !Ref VPC1PublicRouteTable

  IGW:
    Type: 'AWS::EC2::InternetGateway'
    Properties:
      Tags:
        - Key: Name
          Value: IGW-Outbound

  AttachGateway:
    Type: 'AWS::EC2::VPCGatewayAttachment'
    Properties:
      VpcId: !Ref VPC1
      InternetGatewayId: !Ref IGW

  NATGW:
    Type: 'AWS::EC2::NatGateway'
    Properties:
      SubnetId: !Ref VPC1PubSub1
      AllocationId: !GetAtt 
        - EIPNATGW
        - AllocationId
      Tags:
        - Key: Name
          Value: NATGW-Outbound
    DependsOn: IGW

  EIPNATGW:
    Type: 'AWS::EC2::EIP'
    Properties:
      Domain: vpc

