--- AWSTemplateFormatVersion: '2010-09-09' Parameters: SubnetIndex: Description: 'Index of the subnet' Type: Number MinValue: 0 MaxValue: 5 SubnetCount: Description: 'To slice the IP address ranges you need to specify how many subnets you want to create in the VPC' Type: Number MinValue: 1 MaxValue: 6 Size: Type: String FirstTierSubnet1CIDR: Type: String Mappings: CidrBitsMap: "2": AZCidrBits: 16 Conditions: IsProd: !Equals [!Ref AWS::AccountId, '123456789012'] Resources: Subnet: Type: 'AWS::EC2::Subnet' Properties: CidrBlock: !Select [!Ref SubnetIndex, !Cidr [{'Fn::ImportValue': 'vpc-CidrBlock'}, !Ref SubnetCount, 12]] VpcId: 'vpc-123456' SubnetWihtMap: Type: AWS::EC2::Subnet Properties: VpcId: 'vpc-123456' CidrBlock: !Select [ 2, !Cidr [ {'Fn::ImportValue': 'vpc-CidrBlock'}, 4, !FindInMap [CidrBitsMap, !Ref Size, AZCidrBits] ]] MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 SubnetWithConditions: Type: AWS::EC2::Subnet Properties: # /28 block #1 (VPC Allocation), inside /26 block #3, inside /24 block #3 (prod) or block #15 (non-prod) CidrBlock: Fn::Select: - 1 - Fn::Cidr: - Fn::Select: - 3 - Fn::Cidr: - Fn::If: - IsProd - Fn::Select: - 3 - Fn::Cidr: - Fn::GetAtt: MyVPC.CidrBlock - 4 - 8 - Fn::Select: - 15 - Fn::Cidr: - Fn::GetAtt: MyVPC.CidrBlock - 16 - 8 - 4 - 6 - 2 - 4 VpcId: 'vpc-123456' PublicSubnet1: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC AvailabilityZone: !Select [0, !GetAZs ""] CidrBlock: !If - IsProd - !Ref FirstTierSubnet1CIDR - !Select - 0 - !Cidr - 10.0.0.0/24 - !If [IsProd, 16, 8] - 4