如何选择未分配给EC2实例的所有弹性IP? [英] How can I select all elastic IPs that are not assigned to an EC2 instance?

查看:196
本文介绍了如何选择未分配给EC2实例的所有弹性IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前未分配给实例的所有弹性IP。

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

使用以下方法很容易获得所有弹性IP: aws ec2 describe-addresses

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

从这里,很容易过滤掉没有 AssociationId 。但是,我不确定如何使用-query 来做到这一点。

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.

我知道-query 选项使用JMESPath筛选结果,但是我不知道如何告诉它返回没有 AssociationId 。有帮助吗?

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?

谢谢。

推荐答案

您可以检查地址收集null值,但是使用 InstanceId 代替 AssociationId 更好的常规解决方案可能会更好:

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


InstanceId->(字符串)

InstanceId -> (string)

与该地址关联的实例的ID (如果有)。

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

AssociationId->(字符串)

AssociationId -> (string)

表示地址与

不在VPC中的弹性IP没有 AssociationId 属性,但VPC和EC2 Classic中的弹性IP都将输出 InstanceId

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.

您可以如果只关心VPC中的IP,也可以以相同的方式使用 AssociationId

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

示例:

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

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

进一步阅读:

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

这篇关于如何选择未分配给EC2实例的所有弹性IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆