github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/functions/cidrsubnet.html.md (about)

     1  ---
     2  layout: "language"
     3  page_title: "cidrsubnet - Functions - Configuration Language"
     4  sidebar_current: "docs-funcs-ipnet-cidrsubnet"
     5  description: |-
     6    The cidrsubnet function calculates a subnet address within a given IP network
     7    address prefix.
     8  ---
     9  
    10  # `cidrsubnet` Function
    11  
    12  `cidrsubnet` calculates a subnet address within given IP network address prefix.
    13  
    14  ```hcl
    15  cidrsubnet(prefix, newbits, netnum)
    16  ```
    17  
    18  `prefix` must be given in CIDR notation, as defined in
    19  [RFC 4632 section 3.1](https://tools.ietf.org/html/rfc4632#section-3.1).
    20  
    21  `newbits` is the number of additional bits with which to extend the prefix.
    22  For example, if given a prefix ending in `/16` and a `newbits` value of
    23  `4`, the resulting subnet address will have length `/20`.
    24  
    25  `netnum` is a whole number that can be represented as a binary integer with
    26  no more than `newbits` binary digits, which will be used to populate the
    27  additional bits added to the prefix.
    28  
    29  This function accepts both IPv6 and IPv4 prefixes, and the result always uses
    30  the same addressing scheme as the given prefix.
    31  
    32  Unlike the related function [`cidrsubnets`](./cidrsubnets.html), `cidrsubnet`
    33  allows you to give a specific network number to use. `cidrsubnets` can allocate
    34  multiple network addresses at once, but numbers them automatically starting
    35  with zero.
    36  
    37  -> **Note:** As a historical accident, this function interprets IPv4 address
    38  octets that have leading zeros as decimal numbers, which is contrary to some
    39  other systems which interpret them as octal. We have preserved this behavior
    40  for backward compatibility, but recommend against relying on this behavior.
    41  
    42  ## Examples
    43  
    44  ```
    45  > cidrsubnet("172.16.0.0/12", 4, 2)
    46  172.18.0.0/16
    47  > cidrsubnet("10.1.2.0/24", 4, 15)
    48  10.1.2.240/28
    49  > cidrsubnet("fd00:fd12:3456:7890::/56", 16, 162)
    50  fd00:fd12:3456:7800:a200::/72
    51  ```
    52  
    53  ## Netmasks and Subnets
    54  
    55  Using `cidrsubnet` requires familiarity with some network addressing concepts.
    56  
    57  The most important idea is that an IP address (whether IPv4 or IPv6) is
    58  fundamentally constructed from binary digits, even though we conventionally
    59  represent it as either four decimal octets (for IPv4) or a sequence of 16-bit
    60  hexadecimal numbers (for IPv6).
    61  
    62  Taking our example above of `cidrsubnet("10.1.2.0/24", 4, 15)`, the function
    63  will first convert the given IP address string into an equivalent binary
    64  representation:
    65  
    66  ```
    67        10 .        1 .        2 .        0
    68  00001010   00000001   00000010 | 00000000
    69           network               |   host
    70  ```
    71  
    72  The `/24` at the end of the prefix string specifies that the first 24
    73  bits -- or, the first three octets -- of the address identify the network
    74  while the remaining bits (32 - 24 = 8 bits in this case) identify hosts
    75  within the network.
    76  
    77  The CLI tool [`ipcalc`](https://gitlab.com/ipcalc/ipcalc) is useful for
    78  visualizing CIDR prefixes as binary numbers. We can confirm the conversion
    79  above by providing the same prefix string to `ipcalc`:
    80  
    81  ```
    82  $ ipcalc 10.1.2.0/24
    83  Address:   10.1.2.0             00001010.00000001.00000010. 00000000
    84  Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
    85  Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
    86  =>
    87  Network:   10.1.2.0/24          00001010.00000001.00000010. 00000000
    88  HostMin:   10.1.2.1             00001010.00000001.00000010. 00000001
    89  HostMax:   10.1.2.254           00001010.00000001.00000010. 11111110
    90  Broadcast: 10.1.2.255           00001010.00000001.00000010. 11111111
    91  Hosts/Net: 254                   Class A, Private Internet
    92  ```
    93  
    94  This gives us some additional information but also confirms (using a slightly
    95  different notation) the conversion from decimal to binary and shows the range
    96  of possible host addresses in this network.
    97  
    98  While [`cidrhost`](./cidrhost.html) allows calculating single host IP addresses,
    99  `cidrsubnet` on the other hand creates a new network prefix _within_ the given
   100  network prefix. In other words, it creates a subnet.
   101  
   102  When we call `cidrsubnet` we also pass two additional arguments: `newbits` and
   103  `netnum`. `newbits` decides how much longer the resulting prefix will be in
   104  bits; in our example here we specified `4`, which means that the resulting
   105  subnet will have a prefix length of 24 + 4 = 28 bits. We can imagine these
   106  bits breaking down as follows:
   107  
   108  ```
   109        10 .        1 .        2 .    ?        0
   110  00001010   00000001   00000010 |   XXXX | 0000
   111           parent network        | netnum | host
   112  ```
   113  
   114  Four of the eight bits that were originally the "host number" are now being
   115  repurposed as the subnet number. The network prefix no longer falls on an
   116  exact octet boundary, so in effect we are now splitting the last decimal number
   117  in the IP address into two parts, using half of it to represent the subnet
   118  number and the other half to represent the host number.
   119  
   120  The `netnum` argument then decides what number value to encode into those
   121  four new subnet bits. In our current example we passed `15`, which is
   122  represented in binary as `1111`, allowing us to fill in the `XXXX` segment
   123  in the above:
   124  
   125  ```
   126        10 .        1 .        2 .    15       0
   127  00001010   00000001   00000010 |   1111 | 0000
   128           parent network        | netnum | host
   129  ```
   130  
   131  To convert this back into normal decimal notation we need to recombine the
   132  two portions of the final octet. Converting `11110000` from binary to decimal
   133  gives 240, which can then be combined with our new prefix length of 28 to
   134  produce the result `10.1.2.240/28`. Again we can pass this prefix string to
   135  `ipcalc` to visualize it:
   136  
   137  ```
   138  $ ipcalc 10.1.2.240/28
   139  Address:   10.1.2.240           00001010.00000001.00000010.1111 0000
   140  Netmask:   255.255.255.240 = 28 11111111.11111111.11111111.1111 0000
   141  Wildcard:  0.0.0.15             00000000.00000000.00000000.0000 1111
   142  =>
   143  Network:   10.1.2.240/28        00001010.00000001.00000010.1111 0000
   144  HostMin:   10.1.2.241           00001010.00000001.00000010.1111 0001
   145  HostMax:   10.1.2.254           00001010.00000001.00000010.1111 1110
   146  Broadcast: 10.1.2.255           00001010.00000001.00000010.1111 1111
   147  Hosts/Net: 14                    Class A, Private Internet
   148  ```
   149  
   150  The new subnet has four bits available for host numbering, which means
   151  that there are 14 host addresses available for assignment once we subtract
   152  the network's own address and the broadcast address. You can thus use
   153  [`cidrhost`](./cidrhost.html) function to calculate those host addresses by
   154  providing it a value between 1 and 14:
   155  
   156  ```
   157  > cidrhost("10.1.2.240/28", 1)
   158  10.1.2.241
   159  > cidrhost("10.1.2.240/28", 14)
   160  10.1.2.254
   161  ```
   162  
   163  For more information on CIDR notation and subnetting, see
   164  [Classless Inter-domain Routing](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing).
   165  
   166  ## Related Functions
   167  
   168  * [`cidrhost`](./cidrhost.html) calculates the IP address for a single host
   169    within a given network address prefix.
   170  * [`cidrnetmask`](./cidrnetmask.html) converts an IPv4 network prefix in CIDR
   171    notation into netmask notation.
   172  * [`cidrsubnets`](./cidrsubnets.html) can allocate multiple consecutive
   173    addresses under a prefix at once, numbering them automatically.