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

ⅰ亾dé卋堺 提交于 2019-12-04 03:17:34

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:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!