github.com/hugorut/terraform@v1.1.3/website/docs/language/functions/cidrsubnets.mdx (about)

     1  ---
     2  page_title: cidrsubnets - Functions - Configuration Language
     3  description: |-
     4    The cidrsubnets function calculates a sequence of consecutive IP address
     5    ranges within a particular CIDR prefix.
     6  ---
     7  
     8  # `cidrsubnets` Function
     9  
    10  `cidrsubnets` calculates a sequence of consecutive IP address ranges within
    11  a particular CIDR prefix.
    12  
    13  ```hcl
    14  cidrsubnets(prefix, newbits...)
    15  ```
    16  
    17  `prefix` must be given in CIDR notation, as defined in
    18  [RFC 4632 section 3.1](https://tools.ietf.org/html/rfc4632#section-3.1).
    19  
    20  The remaining arguments, indicated as `newbits` above, each specify the number
    21  of additional network prefix bits for one returned address range. The return
    22  value is therefore a list with one element per `newbits` argument, each
    23  a string containing an address range in CIDR notation.
    24  
    25  For more information on IP addressing concepts, see the documentation for the
    26  related function [`cidrsubnet`](/language/functions/cidrsubnet). `cidrsubnet` calculates
    27  a single subnet address within a prefix while allowing you to specify its
    28  subnet number, while `cidrsubnets` can calculate many at once, potentially of
    29  different sizes, and assigns subnet numbers automatically.
    30  
    31  When using this function to partition an address space as part of a network
    32  address plan, you must not change any of the existing arguments once network
    33  addresses have been assigned to real infrastructure, or else later address
    34  assignments will be invalidated. However, you _can_ append new arguments to
    35  existing calls safely, as long as there is sufficient address space available.
    36  
    37  This function accepts both IPv6 and IPv4 prefixes, and the result always uses
    38  the same addressing scheme as the given prefix.
    39  
    40  -> **Note:** As a historical accident, this function interprets IPv4 address
    41  octets that have leading zeros as decimal numbers, which is contrary to some
    42  other systems which interpret them as octal. We have preserved this behavior
    43  for backward compatibility, but recommend against relying on this behavior.
    44  
    45  -> **Note:** [The Terraform module `hashicorp/subnets/cidr`](https://registry.terraform.io/modules/hashicorp/subnets/cidr)
    46  wraps `cidrsubnets` to provide additional functionality for assigning symbolic
    47  names to your networks and skipping prefixes for obsolete allocations. Its
    48  documentation includes usage examples for several popular cloud virtual network
    49  platforms.
    50  
    51  ## Examples
    52  
    53  ```
    54  > cidrsubnets("10.1.0.0/16", 4, 4, 8, 4)
    55  [
    56    "10.1.0.0/20",
    57    "10.1.16.0/20",
    58    "10.1.32.0/24",
    59    "10.1.48.0/20",
    60  ]
    61  
    62  > cidrsubnets("fd00:fd12:3456:7890::/56", 16, 16, 16, 32)
    63  [
    64    "fd00:fd12:3456:7800::/72",
    65    "fd00:fd12:3456:7800:100::/72",
    66    "fd00:fd12:3456:7800:200::/72",
    67    "fd00:fd12:3456:7800:300::/88",
    68  ]
    69  ```
    70  
    71  You can use nested `cidrsubnets` calls with
    72  [`for` expressions](/language/expressions/for)
    73  to concisely allocate groups of network address blocks:
    74  
    75  ```
    76  > [for cidr_block in cidrsubnets("10.0.0.0/8", 8, 8, 8, 8) : cidrsubnets(cidr_block, 4, 4)]
    77  [
    78    [
    79      "10.0.0.0/20",
    80      "10.0.16.0/20",
    81    ],
    82    [
    83      "10.1.0.0/20",
    84      "10.1.16.0/20",
    85    ],
    86    [
    87      "10.2.0.0/20",
    88      "10.2.16.0/20",
    89    ],
    90    [
    91      "10.3.0.0/20",
    92      "10.3.16.0/20",
    93    ],
    94  ]
    95  ```
    96  
    97  ## Related Functions
    98  
    99  * [`cidrhost`](/language/functions/cidrhost) calculates the IP address for a single host
   100    within a given network address prefix.
   101  * [`cidrnetmask`](/language/functions/cidrnetmask) converts an IPv4 network prefix in CIDR
   102    notation into netmask notation.
   103  * [`cidrsubnet`](/language/functions/cidrsubnet) calculates a single subnet address, allowing
   104    you to specify its network number.