How can I select all elastic IPs that are not assigned to an EC2 instance?
Asked Answered
E

2

13

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

It's easy to get all of the Elastic IPs using this: aws ec2 describe-addresses

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

I know that the --query option uses JMESPath to filter results, but I have no idea how to tell it to return me all results that do not have an AssociationId. Any help?

Thanks.

Equipotential answered 20/11, 2015 at 17:17 Comment(0)
O
23

You can check the Addresses collection for null values, but instead of AssociationId a better general solution may be better to use InstanceId:

InstanceId -> (string)

The ID of the instance that the address is associated with (if any).

AssociationId -> (string)

The ID representing the association of the address with an instance in a VPC.

Elastic IPs that are not in a VPC do not have the AssociationId property, but elastic IPs in both VPC and EC2 Classic will output InstanceId.

You can alternatively use AssociationId in the same way, if you only care about IPs in a VPC.

Examples:

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

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

Further Reading:

Octahedron answered 20/11, 2015 at 18:13 Comment(8)
Note that in some situations, unassociated EIPs will yield InstanceId==null and sometimes InstanceId=="". Believe that the distinction is VPC EIPs (show null) vs EC2-Classic EIPs (show "").Barrios
Thanks for the comment. I've additionally seen EC2-Classic EIPs explicitly show null in the result set ("InstanceId": null), and VPC EIPS simply not include InstanceId at all if it isn't set. For that situation at least, only checking null returns both cases from my console.Octahedron
Appreciate it - this seems like a good way to go. Wasn't aware that I could check for null on non-existing parameters. Thanks!Equipotential
Need to use double quotes on Windows: "Addresses[?InstanceId==null]"Heckle
While this works for the aws CLI as described, I can't make a similar request work with the API. I'm sending a body Action=DescribeAddresses&Filter.1.Name=instance-id&Version=2016-11-15 but I get no matching results. Any idea how to format this?Puritanism
@Puritanism Hi, this dimension probably changes the answer sufficiently to ask a new/separate question. Feel free to link to this one for additional context!Octahedron
@AnthonyNeace that's a good point. I had thought of that and found #34385327 which is exactly my issue and appears to be unsolved. I'll keep digging and follow up there with any findings.Puritanism
For anyone wondering where the query command is documented it's here. I was able to filter for all IP addresses where neither InstanceId or AssociationId is set with the following (on Windows): aws ec2 describe-addresses --query "Addresses[?InstanceId==null&&AssociationId==null].[PublicIp]" --output textMorality
E
6

ElasticIPs are also attached to NAT Gateways. In that case InstanceID value will be null but AssociationID is the field which will be present there in any scenario. So better to use associationID to be sure that EIP is in use or not.

Elvera answered 22/3, 2018 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.