AWSTemplateFormatVersion: "2010-09-09" Description: Provides VPC networking configuration Parameters: AvailabilityZones: Description: The list of Availability Zones to use for the subnets in the VPC. Type: List VPCName: Description: The VPC Name Type: String Default: My VPC VPCCidr: Type: String Default: 10.0.0.0/16 AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$ ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28 VPCPublicSubnet1Cidr: Description: CIDR block of Public Subnet 1 Type: String Default: 10.0.0.0/24 AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$ ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28 VPCPublicSubnet2Cidr: Description: CIDR block of Public Subnet 2 Type: String Default: 10.0.1.0/24 AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$ ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28 VPCPrivateSubnet1Cidr: Description: CIDR block of Private Subnet 1 Type: String Default: 10.0.2.0/24 AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$ ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28 VPCPrivateSubnet2Cidr: Description: CIDR block of Private Subnet 2 Type: String Default: 10.0.3.0/24 AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$ ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28 Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VPCCidr EnableDnsHostnames: true EnableDnsSupport: true InstanceTenancy: default Tags: - Key: VpcId Value: !Ref VPCName VPCPublicSubnet1Subnet: Type: AWS::EC2::Subnet Properties: CidrBlock: !Ref VPCPublicSubnet1Cidr VpcId: !Ref VPC AvailabilityZone: !Select [0, !Ref AvailabilityZones] MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PublicSubnet1]] VPCPublicSubnet1RouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PublicSubnet1]] VPCPublicSubnet1RouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: RouteTableId: !Ref VPCPublicSubnet1RouteTable SubnetId: !Ref VPCPublicSubnet1Subnet VPCPublicSubnet1DefaultRoute: Type: AWS::EC2::Route DependsOn: - VPCGW Properties: RouteTableId: !Ref VPCPublicSubnet1RouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref VPCIGW VPCPublicSubnet1EIP: Type: AWS::EC2::EIP Properties: Domain: vpc VPCPublicSubnet1NATGateway: Type: AWS::EC2::NatGateway Properties: AllocationId: !GetAtt VPCPublicSubnet1EIP.AllocationId SubnetId: !Ref VPCPublicSubnet1Subnet Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PublicSubnet1]] VPCPublicSubnet2Subnet: Type: AWS::EC2::Subnet Properties: CidrBlock: !Ref VPCPublicSubnet2Cidr VpcId: !Ref VPC AvailabilityZone: !Select [1, !Ref AvailabilityZones] MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PublicSubnet2]] VPCPublicSubnet2RouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PublicSubnet2]] VPCPublicSubnet2RouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: RouteTableId: !Ref VPCPublicSubnet2RouteTable SubnetId: !Ref VPCPublicSubnet2Subnet VPCPublicSubnet2DefaultRoute: Type: AWS::EC2::Route DependsOn: - VPCGW Properties: RouteTableId: !Ref VPCPublicSubnet2RouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref VPCIGW VPCPrivateSubnet1Subnet: Type: AWS::EC2::Subnet Properties: CidrBlock: !Ref VPCPrivateSubnet1Cidr VpcId: !Ref VPC AvailabilityZone: !Select [0, !Ref AvailabilityZones] MapPublicIpOnLaunch: false Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PrivateSubnet1]] VPCPrivateSubnet1RouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PrivateSubnet1]] VPCPrivateSubnet1RouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: RouteTableId: !Ref VPCPrivateSubnet1RouteTable SubnetId: !Ref VPCPrivateSubnet1Subnet VPCPrivateSubnet1DefaultRoute: Type: AWS::EC2::Route Properties: RouteTableId: !Ref VPCPrivateSubnet1RouteTable DestinationCidrBlock: 0.0.0.0/0 NatGatewayId: !Ref VPCPublicSubnet1NATGateway VPCPrivateSubnet2Subnet: Type: AWS::EC2::Subnet Properties: CidrBlock: !Ref VPCPrivateSubnet2Cidr VpcId: !Ref VPC AvailabilityZone: !Select [1, !Ref AvailabilityZones] MapPublicIpOnLaunch: false Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PrivateSubnet2]] VPCPrivateSubnet2RouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Join [/, [!Ref VPCName, PrivateSubnet2]] VPCPrivateSubnet2RouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: RouteTableId: !Ref VPCPrivateSubnet2RouteTable SubnetId: !Ref VPCPrivateSubnet2Subnet VPCPrivateSubnet2DefaultRoute: Type: AWS::EC2::Route Properties: RouteTableId: !Ref VPCPrivateSubnet2RouteTable DestinationCidrBlock: 0.0.0.0/0 NatGatewayId: !Ref VPCPublicSubnet1NATGateway VPCIGW: Type: AWS::EC2::InternetGateway Properties: Tags: - Key: Name Value: VPC/VPC VPCGW: Type: AWS::EC2::VPCGatewayAttachment Properties: VpcId: !Ref VPC InternetGatewayId: !Ref VPCIGW Outputs: VpcID: Value: !Ref VPC PublicSubnet1: Value: !Ref VPCPublicSubnet1Subnet PublicSubnet2: Value: !Ref VPCPublicSubnet2Subnet PrivateSubnet1: Value: !Ref VPCPrivateSubnet1Subnet PrivateSubnet2: Value: !Ref VPCPrivateSubnet2Subnet