开发者

EC2 Java Api Wait till Ec2 Instance gets created.

开发者 https://www.devze.com 2023-04-10 21:49 出处:网络
I have just started using Amazon EC2 API in Java. I have created instances using ec2.runInstances(runInstancesRequest);

I have just started using Amazon EC2 API in Java.

I have created instances using ec2.runInstances(runInstancesRequest);

But it will take some time for the instance to get launched (typically 1-2 mins). I need to get the 开发者_如何学Cpublic DNS of the machine via Java EC2 API.

How do I know when the instances change from "pending" state to "processed" state, and How can I get the public DNS of the EC2 instance through the EC2 API.

Thanks in advance. Kanna


There is no event model or other signal raised by the SDK to tell you when an EC2 object changes state - the only way to find out is to issue a DescribeXXXXXXXX call on the object on a repeated basis, say once every 30 seconds, until the state field changes.

There's a finite minimum time for the call to execute and respond, so you need to find an interval that doesn't fire requests before the prior one has completed. Or simply wait for the response, and then wait another 'n' seconds before re-issuing the call. You also don't want to spam the AWS API with rapid requests, even if they're timed between responses. In my controller application, I set the interval at 30 seconds, issue the request, wait for the response, and then subtract the elapsed time from the interval and sleep that long. In a multithreaded model, I can thereby track state changes on many objects simultaneously without swamping either my local CPU or the API.

Once the change of state has been detected (and assuming the new state is whet you expect - don't forget to handle failure modes) you can get a wide variety of descriptive information, including public DNS address (in the case of instance objects) from the structure returned in the API response object.


Actually, you can POLL to find out the status of an instance. Here is some Bash code to do that, just adapt it to JAVA. You MAY have a similar command in the Java SDK so that you don't have to do an execution of Bash from Java. The command 'ec2-describe-instances' comes from the Amazon AWS CLI. I would start out the function or method to wait for running state to test if instance is 'pending', and fail if it is not starting or 'pending'. Then record the time, and give it a max of let's say 3 minutes, and just keep polling for 'running' status in a loop, checking for the 3 minute limit. Return to the calling point whichever comes first, 'not started','startup time exceeded', or 'running'.

    setInstanceStatus () {
  instanceStatus=`ec2-describe-instances $INSTANCE_ID -C $CERTIFICATE_FILE -K $PRIVATE_KEY --region $REGION -U $AWS_URL`

  is_pending="`echo $instanceStatus|grep -c " pending "`"
  is_running="`echo $instanceStatus|grep -c " running "`"
  is_shutting_down="`echo $instanceStatus|grep -c " shutting-down "`"
  is_terminated="`echo $instanceStatus|grep -c " terminated "`"
  is_stopping="`echo $instanceStatus|grep -c " stopping "`"
  is_stopped="`echo $instanceStatus|grep -c " stopped "`"

  if   [ "$is_pending" -eq "1" ]; then
    status="pending"
  elif [  "$is_running" -eq "1" ]; then
    status="running"
  elif [  "$is_shutting_down" -eq "1" ]; then
    status="shutting-down"
  elif [  "$is_terminated" -eq "1" ]; then
    status="terminated"
  elif [  "$is_stopping" -eq "1" ]; then
    status="stopping"
  elif [  "$is_stopped" -eq "1" ]; then
    status="stopped"
  else
    status="bad-instance-state"
  fi
}
0

精彩评论

暂无评论...
验证码 换一张
取 消