简体   繁体   English

检查 EC2 实例是否完成初始化。 使用 AWS Java SDK v2

[英]Check if EC2 instance finished initializing. Using the AWS Java SDK v2

I am programatically spinning up an EC2 instance from the AWS Java SDK v2.我正在以编程方式从 AWS Java SDK v2 启动一个 EC2 实例。 I then associate an elastic IP address to this instance.然后,我将弹性 IP 地址关联到此实例。 This takes a couple minutes.这需要几分钟。 AWS then initializes the instance. AWS 然后初始化实例。 In the AWS console it looks like this...在 AWS 控制台中它看起来像这样...... 在此处输入图像描述 I cannot ssh into this instance until the initialization completes.在初始化完成之前,我不能 ssh 进入这个实例。 Is there a way to programatically check that the instance has completed initialization?有没有办法以编程方式检查实例是否已完成初始化? I have seen the method describeInstances(), but to the best of my knowledge, the most relevant information this method's response contains is the instance's state. The state is "running" and is not therefore probably not the variable that indicates initializing status.我看过 describeInstances() 方法,但据我所知,此方法的响应包含的最相关信息是实例的 state。state 正在“运行”,因此可能不是指示初始化状态的变量。 Maybe there is a different variable or a different method that is helpful for monitoring this initialization process?也许有不同的变量或不同的方法有助于监视这个初始化过程? Cheers.干杯。

Not really.并不真地。

Amazon EC2 is responsible for creating the virtual machine, attaching a disk with the operating system and then booting the instance. Amazon EC2 负责创建虚拟机,将磁盘与操作系统连接,然后启动实例。 Everything after that is up to the operating system and startup scripts.之后的一切都取决于操作系统和启动脚本。 The Amazon EC2 service can't 'see' what is happening on the instance. Amazon EC2 服务无法“看到”实例上发生的事情。

If you particularly need to know this information, you could use a script on the instance to 'publish' this fact somewhere.如果您特别需要了解此信息,您可以使用实例上的脚本在某处“发布”此事实。 For example, it could add a Tag to the instance saying State: Ready .例如,它可以向实例添加一个标记为State: Ready Or, if you need to programmatically respond somehow, the script could trigger an AWS Lambda function or put a message into an Amazon SQS queue.或者,如果您需要以某种方式以编程方式响应,脚本可以触发 AWS Lambda 函数或将消息放入 Amazon SQS 队列。

I check EC2 instance status after starting an instance with this Java code and the AWS SDK.使用此 Java 代码和 AWS 开发工具包启动实例后,我会检查 EC2 实例状态。 An EC2 cd5.large instance takes about 90 seconds to stop and start back to the "running" state and then takes about another 3.5 minutes to complete initializing. EC2 cd5.large 实例大约需要 90 秒才能停止并重新启动到“正在运行”状态,然后大约需要 3.5 分钟才能完成初始化。 This code is after the part which you probably have that spins the instance up to "running"...此代码在您可能拥有的将实例旋转到“运行”的部分之后......

boolean isInstanceInitialized = false;
while (!isInstanceInitialized) {
  final DescribeInstanceStatusResult result = ec2
          .describeInstanceStatus(
                  new DescribeInstanceStatusRequest()
                          .withInstanceIds(instanceId));
  final List<InstanceStatus> instanceStatuses = result.getInstanceStatuses();
  boolean isInstanceStatusOK;
  boolean isSystemStatusOK;

  for (final InstanceStatus instanceStatus : instanceStatuses) {
    LOGGER.info("  InstanceStatus.instanceStatus: " + instanceStatus.getInstanceStatus().getStatus());
    LOGGER.info("  SystemStatus:                  " + instanceStatus.getSystemStatus().getStatus());
    isInstanceStatusOK = ("ok".equals(instanceStatus.getInstanceStatus().getStatus()));
    isSystemStatusOK = ("ok".equals(instanceStatus.getSystemStatus().getStatus()));
    isInstanceInitialized = isInstanceStatusOK && isSystemStatusOK;
  }


  if (isInstanceInitialized) {
    break;
  } else {
    try {
      // sleep 10 seconds between polling the instance state
      Thread.sleep(10_000);
    } catch (InterruptedException ex) {
      // ignore
    }
  }
}

You can potentially use the waiter feature in AWS SDK for Java. EC2 supports waiters for a number of operations , one of which is waitUntilInstanceExists .您可以潜在地使用 AWS SDK 中的服务员功能来表示 Java。EC2 支持服务员执行多种操作,其中之一是waitUntilInstanceExists

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

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