# Terraform cidrsubnet

Terraform 에 cidrsubnet 이라는 함수가 있는데, 설명을 읽어도 이해가 안되서, 삽질을 좀 하며 이해한 내용을 정리합니다.

더불어 ipcalc 라는 완소 유틸리티도 사용해보세요.

cidrsubnet("10.0.0.0/20", <newbit>, <newnum>)
10.0.0.0/20 = 

❯ ipcalc 10.0.0.0/20
Address:   10.0.0.0             00001010.00000000.0000 0000.00000000
Netmask:   255.255.240.0 = 20   11111111.11111111.1111 0000.00000000
Wildcard:  0.0.15.255           00000000.00000000.0000 1111.11111111
=>
Network:   10.0.0.0/20          00001010.00000000.0000 0000.00000000
HostMin:   10.0.0.1             00001010.00000000.0000 0000.00000001
HostMax:   10.0.15.254          00001010.00000000.0000 1111.11111110
Broadcast: 10.0.15.255          00001010.00000000.0000 1111.11111111
Hosts/Net: 4094                  Class A, Private Internet

이걸 2^<newbit> 만큼 쪼갠 후, 그 중 <newnum> 번 째 블럭을 가르키는 cidrblock 표현식을 리턴

ex:

cidrsubnet("10.0.0.0/20", 2, 1)

-> 10.0.0.0/20 을 2^2 = 4 개로 쪼갬. 

10.0.0.0/22

❯ ipcalc 10.0.0.0/22
Address:   10.0.0.0             00001010.00000000.000000 00.00000000
Netmask:   255.255.252.0 = 22   11111111.11111111.111111 00.00000000
Wildcard:  0.0.3.255            00000000.00000000.000000 11.11111111
=>
Network:   10.0.0.0/22          00001010.00000000.000000 00.00000000
HostMin:   10.0.0.1             00001010.00000000.000000 00.00000001
HostMax:   10.0.3.254           00001010.00000000.000000 11.11111110
Broadcast: 10.0.3.255           00001010.00000000.000000 11.11111111
Hosts/Net: 1022                  Class A, Private Internet

10.0.4.0/22

❯ ipcalc 10.0.4.0/22
Address:   10.0.4.0             00001010.00000000.000001 00.00000000
Netmask:   255.255.252.0 = 22   11111111.11111111.111111 00.00000000
Wildcard:  0.0.3.255            00000000.00000000.000000 11.11111111
=>
Network:   10.0.4.0/22          00001010.00000000.000001 00.00000000
HostMin:   10.0.4.1             00001010.00000000.000001 00.00000001
HostMax:   10.0.7.254           00001010.00000000.000001 11.11111110
Broadcast: 10.0.7.255           00001010.00000000.000001 11.11111111
Hosts/Net: 1022                  Class A, Private Internet

10.0.8.0/22

❯ ipcalc 10.0.8.0/22
Address:   10.0.8.0             00001010.00000000.000010 00.00000000
Netmask:   255.255.252.0 = 22   11111111.11111111.111111 00.00000000
Wildcard:  0.0.3.255            00000000.00000000.000000 11.11111111
=>
Network:   10.0.8.0/22          00001010.00000000.000010 00.00000000
HostMin:   10.0.8.1             00001010.00000000.000010 00.00000001
HostMax:   10.0.11.254          00001010.00000000.000010 11.11111110
Broadcast: 10.0.11.255          00001010.00000000.000010 11.11111111
Hosts/Net: 1022                  Class A, Private Internet

10.0.12.0/22

❯ ipcalc 10.0.12.0/22
Address:   10.0.12.0            00001010.00000000.000011 00.00000000
Netmask:   255.255.252.0 = 22   11111111.11111111.111111 00.00000000
Wildcard:  0.0.3.255            00000000.00000000.000000 11.11111111
=>
Network:   10.0.12.0/22         00001010.00000000.000011 00.00000000
HostMin:   10.0.12.1            00001010.00000000.000011 00.00000001
HostMax:   10.0.15.254          00001010.00000000.000011 11.11111110
Broadcast: 10.0.15.255          00001010.00000000.000011 11.11111111
Hosts/Net: 1022                  Class A, Private Internet

이렇게 4개로 쪼개짐.

--------

그 중 첫번째 블럭인 10.0.0.0/22 가 리턴됨.