Terraform provides two useful functions for calculating CIDR ranges for subnets: cidrsubnet and cidrsubnets.
cidrsubnet Function
cidrsubnet finds out the CIDR of the netnum-th subnet of prefix-defined (prefix is in form of CIDR) network by expanding its network mask by newbits bits. Here is the function's signature:
cidrsubnet(prefix, newbits, netnum)
Here are some examples:
$ terraform console
> cidrsubnet("192.168.0.0/16", 8, 0)
"192.168.0.0/24"
> cidrsubnet("192.168.0.0/16", 8, 1)
"192.168.1.0/24"
> cidrsubnet("192.168.0.0/16", 8, 2)
"192.168.2.0/24"
> cidrsubnet("192.168.0.0/16", 8, 3)
> cidrsubnet("192.168.0.0/16", 5, 0)
"192.168.0.0/21"
> cidrsubnet("192.168.0.0/16", 5, 1)
"192.168.8.0/21"
> cidrsubnet("192.168.0.0/16", 5, 2)
"192.168.16.0/21"
> cidrsubnet("192.168.0.0/16", 5, 3)
"192.168.24.0/21"
cidrsubnets Function
cidrsubnets calculates a sequence of consecutive IP address ranges within a particular CIDR prefix.
tolist([
"192.168.0.0/24",
"192.168.1.0/25",
"192.168.128.0/17",
])
Address: 192.168.0.0 11000000.10101000.00000000. 00000000
Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111
=>
Network: 192.168.0.0/24 11000000.10101000.00000000. 00000000
HostMin: 192.168.0.1 11000000.10101000.00000000. 00000001
HostMax: 192.168.0.254 11000000.10101000.00000000. 11111110
Broadcast: 192.168.0.255 11000000.10101000.00000000. 11111111
Hosts/Net: 254 Class C, Private Internet
Address: 192.168.1.0 11000000.10101000.00000001.0 0000000
Netmask: 255.255.255.128 = 25 11111111.11111111.11111111.1 0000000
Wildcard: 0.0.0.127 00000000.00000000.00000000.0 1111111
=>
Network: 192.168.1.0/25 11000000.10101000.00000001.0 0000000
HostMin: 192.168.1.1 11000000.10101000.00000001.0 0000001
HostMax: 192.168.1.126 11000000.10101000.00000001.0 1111110
Broadcast: 192.168.1.127 11000000.10101000.00000001.0 1111111
Hosts/Net: 126 Class C, Private Internet
ipcalc shows that the max IP address in this range is 192.168.1.127 which means that next IP range can start with IP address higher or equal to 192.168.1.128.
How to get Default VPC details?
default = true
}
value = data.aws_vpc.default
}
How to get a list of IDs of Default VPC subnets?
filter {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
}
value = data.aws_subnets.default
}
List of subnet IDs is: data.aws_subnets.default.ids
---
No comments:
Post a Comment