Giuseppe
Giuseppe

Reputation: 678

Retrieving the private IP addresses in a subnet in AWS

Is there an easy way to have all the private IP assigned in a particular subnet with the AWS command line?

I want to give in as my input the subnet ID, ie. subnet-0ca0b01xxxxxxxxxx and the output should the list of all the private IPs assigned in the subnet. Thanks.

Upvotes: 2

Views: 1407

Answers (2)

pabloxio
pabloxio

Reputation: 1493

The previous solution only returns the private IPs for your EC2 instances, but what about your RDS/ElastiCache instances or any other AWS resource that uses a private IP? You actually need to use the describe-network-interfaces command.

This will return all the private IP addresses for a single or multiple subnet-ids:

aws ec2 describe-network-interfaces --filters Name=subnet-id,Values=subnet_id_1,subnet_id_2 --query 'NetworkInterfaces[*].PrivateIpAddress'

But the previous command it's not going to work for all cases either. For example, I'm running multiple EKS clusters at AWS, the VPC CNI plugin for Kubernetes can allocate multiple private IPs for a single Elastic Network Interface In such scenarios this is what you need to use, it's slightly different:

aws ec2 describe-network-interfaces --filters Name=subnet-id,Values=subnet_id_1,subnet_id_2 --query 'NetworkInterfaces[*].PrivateIpAddresses[*].PrivateIpAddress'

Upvotes: 1

AWS PS
AWS PS

Reputation: 4710

You can use the command below:

aws ec2 describe-instances --filters "Name=subnet-id,Values=**YourSubnetID**" --query 'Reservations[*].Instances[*].PrivateIpAddress' --output text

Upvotes: 7

Related Questions