Search code examples
amazon-web-servicesaws-cloudformationamazon-vpcinfrastructure-as-code

Is Fn::Cidr random?


Will Fn::Cidr always output the same values if called with the same input? I want to obtain 3 sub addresses for three different subnets from the same Vpc ip block, can I guarantee that I'll get different addresses for those 3 subnets if I use them as below:

I'm changing Fn::Select's first argument for each one.

"SubBlock1" : { "Fn::Select" : [ 0, { "Fn::Cidr" : ["10.0.1.0/16", 3, 24]} ] }

"SubBlock2" : { "Fn::Select" : [ 1, { "Fn::Cidr" : ["10.0.1.0/16", 3, 24]} ] }

"SubBlock3" : { "Fn::Select" : [ 2, { "Fn::Cidr" : ["10.0.1.0/16", 3, 24]} ] }

Solution

  • To test this, I created a stack with this template:

    AWSTemplateFormatVersion: 2010-09-09
    
    Resources:
    
      Bucket:
        Type: AWS::S3::Bucket
    
    Outputs:
      
      Block1:
        Value: !Select 
            - 0
            - Fn::Cidr: 
              - 10.0.0.0/16
              - 3
              - 8
    
      Block2:
        Value: !Select 
            - 1
            - Fn::Cidr: 
              - 10.0.0.0/16
              - 3
              - 8
    
      Block3:
        Value: !Select 
            - 2
            - Fn::Cidr: 
              - 10.0.0.0/16
              - 3
              - 8
    

    (The bucket is included simply because at least one resource is required.)

    The output was:

    • Block1: 10.1.0.0/24
    • Block2: 10.1.1.0/24
    • Block3: 10.1.2.0/24

    So, yes, it will output the same blocks each time because it is simply dividing the given CIDR as requested.

    Some notes:

    • I had to supply cidrBits=8 to generate a /24 block
    • 10.0.1.0/16 in your example is not a valid /16 block, so I changed it to 10.1.0.0/16