AWSTemplateFormatVersion: "2010-09-09" # Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 Description: > This template builds a VPC with two public and two private subnets. Parameters: vpccidr: Type: String MinLength: 9 MaxLength: 18 AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})" ConstraintDescription: Must be a valid CIDR range in the form x.x.x.x/16 Default: 10.20.0.0/16 AppPublicCIDRA: Type: String MinLength: 9 MaxLength: 18 AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})" ConstraintDescription: Must be a valid CIDR range in the form x.x.x.x/22 Default: 10.20.1.0/24 AppPublicCIDRB: Type: String MinLength: 9 MaxLength: 18 AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})" ConstraintDescription: Must be a valid CIDR range in the form x.x.x.x/22 Default: 10.20.2.0/24 AppPrivateCIDRA: Type: String MinLength: 9 MaxLength: 18 AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})" ConstraintDescription: Must be a valid CIDR range in the form x.x.x.x/22 Default: 10.20.3.0/24 AppPrivateCIDRB: Type: String MinLength: 9 MaxLength: 18 AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})" ConstraintDescription: Must be a valid CIDR range in the form x.x.x.x/22 Default: 10.20.4.0/24 Resources: VPC: Type: "AWS::EC2::VPC" Properties: CidrBlock: !Ref vpccidr EnableDnsHostnames: 'true' EnableDnsSupport: 'true' IGW: Type: "AWS::EC2::InternetGateway" GatewayAttach: Type: "AWS::EC2::VPCGatewayAttachment" Properties: InternetGatewayId: !Ref IGW VpcId: !Ref VPC SubnetPublicA: Type: "AWS::EC2::Subnet" Properties: AvailabilityZone: !Select [0, !GetAZs ] CidrBlock: !Ref AppPublicCIDRA MapPublicIpOnLaunch: true VpcId: !Ref VPC SubnetPrivateA: Type: "AWS::EC2::Subnet" Properties: AvailabilityZone: !Select [0, !GetAZs ] CidrBlock: !Ref AppPrivateCIDRA MapPublicIpOnLaunch: false VpcId: !Ref VPC SubnetPublicB: Type: "AWS::EC2::Subnet" Properties: AvailabilityZone: !Select [1, !GetAZs ] CidrBlock: !Ref AppPublicCIDRB MapPublicIpOnLaunch: true VpcId: !Ref VPC SubnetPrivateB: Type: "AWS::EC2::Subnet" Properties: AvailabilityZone: !Select [1, !GetAZs ] CidrBlock: !Ref AppPrivateCIDRB MapPublicIpOnLaunch: false VpcId: !Ref VPC SubnetRouteTableAssociatePublicA: # Associates the subnet with a route table - passed via import DependsOn: SubnetPublicA Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref RouteTablePublic SubnetId: !Ref SubnetPublicA SubnetRouteTableAssociatePublicB: # Associates the subnet with a route table - passed via import DependsOn: SubnetPublicB Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref RouteTablePublic SubnetId: !Ref SubnetPublicB # Associates the subnet with a route table - passed via import SubnetRouteTableAssociatePrivateA: # Associates the subnet with a route table - passed via parameter DependsOn: SubnetPrivateA Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref RouteTablePrivateA SubnetId: !Ref SubnetPrivateA # Associates the subnet with a route table - passed via parameter SubnetRouteTableAssociatePrivateB: # Associates the subnet with a route table - passed via parameter DependsOn: SubnetPrivateB Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref RouteTablePrivateB SubnetId: !Ref SubnetPrivateB # Associates the subnet with a route table - passed via parameter RouteDefaultPublic: Type: "AWS::EC2::Route" DependsOn: GatewayAttach Properties: DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref IGW RouteTableId: !Ref RouteTablePublic RouteTablePublic: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref VPC RouteDefaultPrivateA: Type: "AWS::EC2::Route" Properties: DestinationCidrBlock: 0.0.0.0/0 NatGatewayId: !Ref NatGatewayA RouteTableId: !Ref RouteTablePrivateA RouteDefaultPrivateB: Type: "AWS::EC2::Route" Properties: DestinationCidrBlock: 0.0.0.0/0 NatGatewayId: !Ref NatGatewayB RouteTableId: !Ref RouteTablePrivateB RouteTablePrivateA: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref VPC RouteTablePrivateB: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref VPC EIPNatGWA: DependsOn: GatewayAttach Type: "AWS::EC2::EIP" Properties: Domain: vpc EIPNatGWB: DependsOn: GatewayAttach Type: "AWS::EC2::EIP" Properties: Domain: vpc NatGatewayA: Type: "AWS::EC2::NatGateway" Properties: AllocationId: !GetAtt EIPNatGWA.AllocationId SubnetId: !Ref SubnetPublicA NatGatewayB: Type: "AWS::EC2::NatGateway" Properties: AllocationId: !GetAtt EIPNatGWB.AllocationId SubnetId: !Ref SubnetPublicB Outputs: VpcId: Description: VPC ID Value: !Ref VPC SubnetIdPublicA: Description: Subnet ID for first public subnet Value: !Ref SubnetPublicA SubnetIdPublicB: Description: Subnet ID for second public subnet Value: !Ref SubnetPublicB SubnetIdPrivateA: Description: Subnet ID for first private subnet Value: !Ref SubnetPrivateA SubnetIdPrivateB: Description: Subnet ID for second private subnet Value: !Ref SubnetPrivateB