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

后端 未结 2 2161
-上瘾入骨i
-上瘾入骨i 2021-02-20 15:38

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

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 16:41

    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:

    • AWS Documentation: aws ec2 describe-addresses
    • AWS Documentation: Elastic IP Addresses in a VPC

提交回复
热议问题