How to select all resilient IP addresses that are not assigned to an EC2 instance?

I am trying to get all Elastic IPs that are not currently assigned to instances.

It is easy to get all Elastic IPs using this: aws ec2 describe-addresses

From here it will be easy to filter out any results that do not have an "AssociationId" . However, I am not sure how to do this using --query .

I know that the --query parameter uses JMESPath to filter the results, but I don’t know how to tell it to return me all the results that do not have AssociationId . Any help?

Thanks.

+5
source share
2 answers

You can check the address collection for null values, but instead of AssociationId it is better to use the more general InstanceId solution:

InstanceId -> (string)

The identifier of the instance with which the address is associated (if any).

AssociationId β†’ (string)

An identifier representing the association of an address with an instance in a VPC.

Elastic IP addresses that are not in the VPC do not have the AssociationId property, but elastic IP addresses in both VPC and EC2 Classic display the InstanceId .

You can also use AssociationId in the same way if you only care about the IP addresses in the VPC.

<strong> Examples:

 aws ec2 describe-addresses --query 'Addresses[?InstanceId==null]' aws ec2 describe-addresses --query 'Addresses[?AssociationId==null]' 

Further reading:

+9
source

ElasticIPs are also connected to NAT gateways. In this case, the InstanceID value will be null, but AssociationID is the field that will be present there in any scenario. Therefore, it is better to use connectionID to make sure that EIP is used or not.

0
source

Source: https://habr.com/ru/post/1236459/


All Articles