简体   繁体   English

检查停止的 AWS EC2 实例是否将获得带有 boto3 的 Public IP

[英]Check if stopped AWS EC2 instance will get a Public IP with boto3

If an EC2 instance has been set up to retrieve a Public IP (not Elastic IP) and it is currently stopped, what can I look at to see that it will get a Public IP when it starts up?如果 EC2 实例已设置为检索公共 IP(不是弹性 IP)并且它当前已停止,我可以查看什么以确保它在启动时将获得公共 IP? Let's assume the su.net it is on is not set up give Public IPs to all instances.让我们假设它所在的 su.net 没有设置为所有实例提供公共 IP。 When the instance is running I can see the public IP information when I retrieve the instance with client.describe_instances() but I don't see anythihng when it is stopped.当实例运行时,当我使用 client.describe_instances() 检索实例时,我可以看到公共 IP 信息,但当它停止时我看不到任何信息。 Thanks.谢谢。

After looking through lots of documentations I came to the conclusion that it can't be done reliably with the information that is exposed through the APIs.在查看大量文档后,我得出结论,无法使用通过 API 公开的信息可靠地完成此操作。 The answer by John covers important details on the factors that determine if an EC2 instance will be assigned a public IP on first boot. John 的回答涵盖了有关确定EC2 实例是否会在首次启动时分配公共 IP 的因素的重要细节。

Given that information it's possible to determine if a newly launched EC2 instance will get a public IP, which is essentially controlled by the AssociatePublicIpAddress parameter.根据该信息,可以确定新启动的 EC2 实例是否将获得公共 IP,这基本上由AssociatePublicIpAddress参数控制。 This can be set explicitly while starting an instance (see doc1 - Example 5 , doc2 ) and will have a default value that depends on the su.net the instance is launched in.这可以在启动实例时明确设置(请参阅doc1 - Example 5doc2 )并且将具有取决于启动实例的 su.net 的默认值。

If the instance is launched in a su.net that has MapPublicIPOnLaunch set to true, this defaults to true and if MapPublicIPOnLaunch is set to false, it defaults to false.如果实例在将MapPublicIPOnLaunch设置为 true 的 su.net 中启动,则默认为 true,如果MapPublicIPOnLaunch设置为 false,则默认为 false。 However, you can overwrite this default.但是,您可以覆盖此默认设置。

The information if a public IP is supposed to be associated is retained on the ENI , if the docs are to be believed (emphasis mine).如果要相信文档(强调我的),则应该将公共 IP 关联的信息保留在 ENI 上

When you create a.network interface, it inherits the public IPv4 addressing attribute from the su.net.当您创建一个 .network 接口时,它会从 su.net 继承公共 IPv4 寻址属性。 If you later modify the public IPv4 addressing attribute of the su.net, the.network interface keeps the setting that was in effect when it was created.如果您稍后修改 su.net 的公共 IPv4 寻址属性,.network 接口将保留创建时有效的设置。 If you launch an instance and specify an existing.network interface as the primary.network interface, the public IPv4 address attribute is determined by this.network interface.如果您启动实例并将现有的.network 接口指定为主要.network 接口,则公共 IPv4 地址属性由此.network 接口确定。

Unfortunately it doesn't seem like any API exposes the value of this internal Flag - neither the DescribeInstances nor the DescribeNetworkInterfaces API-call include it in the response.不幸的是,似乎没有任何 API 公开此内部标志的值 - DescribeInstancesDescribeNetworkInterfaces API 调用均未将其包含在响应中。

As a result of that, you can make an educated guess based on the su.net the instance lives in, but however educated, it is still a guess, because this only works, if the default for AssociatePublicIpAddress hasn't been changed.因此,您可以根据实例所在的 su.net 进行有根据的猜测,但无论有多大根据,它仍然是一个猜测,因为这仅在AssociatePublicIpAddress的默认值未更改的情况下才有效。 The only way to determine that reliably is to turn the instance on, to a DescribeInstances on it and check if it has received a public IP.确定可靠的唯一方法是打开实例,在其上使用DescribeInstances并检查它是否已收到公共 IP。

Public IP addresses can be assigned to instances in 3 ways:公共 IP 地址可以通过 3 种方式分配给实例:

  • An Elastic IP address is assigned to the instance, or弹性 IP 地址分配给实例,
  • The instance is launched with AssociatePublicIpAddress set to True on an ENI, or在 ENI 上启动实例时将AssociatePublicIpAddress设置为 True,或者
  • The su.net has MapPublicIpOnLaunch set to True su.net 已将MapPublicIpOnLaunch设置为 True

To know whether the su.net will automatically attach a public IP address, call DescribeSu.nets() and check the MapPublicIpOnLaunch attribute.要知道 su.net 是否会自动附加一个公共 IP 地址,请调用DescribeSu.nets()并检查MapPublicIpOnLaunch属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM