Is there any way of viewing, in AWS, what ips in a subnet have been allocated?
Your count may be off because AWS reserves five IP addresses per subnet CIDR block. The first four IP addresses in a subnet CIDR block and the last IP address in that CIDR block for its internal networking.
For example in a 10.0.0.0/24 subnet AWS will reserve:
10.0.0.0: Network address.
10.0.0.1: Reserved by AWS for the VPC router.
10.0.0.2: Reserved by AWS. The IP address of the DNS server is the base of the VPC network range plus two. For VPCs with multiple CIDR blocks, the IP address of the DNS server is located in the primary CIDR. We also reserve the base of each subnet range plus two for all CIDR blocks in the VPC. For more information, see Amazon DNS server.
10.0.0.3: Reserved by AWS for future use.
10.0.0.255: Network broadcast address. We do not support broadcast in a VPC, therefore we reserve this address.
To get a list of the IP addresses in use from the command line:
aws ec2 describe-network-interfaces --filters Name=subnet-id,Values=<subnet id> |jq -r '.NetworkInterfaces[].PrivateIpAddress' |sort
Using the subnet-id
filter allows you to exclude the subnets you're not concerned about.
If you want a count just replace sort
with wc -l
.
You can visually see the number of IP's free per subnet in the VPC -> Subnet section of the AWS Console.
References
- describe-network-interfaces
- AWS VPC Subnets
In EC2 console go to Network interfaces view down in the left hand side column. It will show all the network interfaces allocated not only to EC2s but also to Fargate, RDS, VPC Lambdas, NAT Gateways, etc.
Note also that there is a couple of IPs reserved in each VPC and Subnet - IGW, AWS DNS, etc. IIRC it’s the first 5 IPs that are reserved. These will not show in the list above.
Hope that helps :)