Subnet problem in VPC creation via cloudformation

Refresh

2 weeks ago

Views

6 time

0

I am a noob in networking. I am creating VPC via cloudformation. In that, I have to add 4 subnets. When I run that template it is giving me this error. Template error: Fn::Select cannot select nonexistent value at index 3

But, when I try to create with 3 subnets it gets created.

My Template sample:

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.

  Subnet01Block:
    Type: String
    Default: 192.168.0.0/14
    Description: CidrBlock for subnet 01 within the VPC

  Subnet02Block:
    Type: String
    Default: 192.168.64.0/14
    Description: CidrBlock for subnet 02 within the VPC

  Subnet03Block:
    Type: String
    Default: 192.168.128.0/14
    Description: CidrBlock for subnet 03 within the VPC

  Subnet04Block:
    Type: String
    Default: 192.168.192.0/14
    Description: CidrBlock for subnet 04 within the VPC

Resources:
  Subnet01:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 01
    Properties:
      AvailabilityZone:
        Fn::Select:
        - '0'
        - Fn::GetAZs:
            Ref: AWS::Region
      CidrBlock:
        Ref: Subnet01Block
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-Services-Subnet01"

  Subnet02:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 02
    Properties:
      AvailabilityZone:
        Fn::Select:
        - '1'
        - Fn::GetAZs:
            Ref: AWS::Region
      CidrBlock:
        Ref: Subnet02Block
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-Services-Subnet02"

  Subnet03:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 03
    Properties:
      AvailabilityZone:
        Fn::Select:
        - '2'
        - Fn::GetAZs:
            Ref: AWS::Region
      CidrBlock:
        Ref: Subnet03Block
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-Services-Subnet03"

  Subnet04:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 04
    Properties:
      AvailabilityZone:
        Fn::Select:
        - '3'
        - Fn::GetAZs:
            Ref: AWS::Region
      CidrBlock:
        Ref: Subnet04Block
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-Services-Subnet04"

am I doing wrong anything here?

0 answers