EC2 Java Api Wait until an instance of Ec2 is created.

I just started using the Amazon EC2 API in Java.

I created instances using ec2.runInstances (runInstancesRequest);

But it will take some time to start the instance (usually 1-2 minutes). I need to get a public DNS machine through the Java EC2 API.

How to find out when instances change from pending to processed state and how can I get the public DNS of an EC2 instance through the EC2 API.

Thanks in advance. Canna

+8
java amazon-web-services amazon-ec2 ec2-api-tools
source share
2 answers

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 to the object on a repeated basis, say, once every 30 seconds, until the status field changes.

It takes a minimum amount of time to make a call and answer, so you need to find an interval that does not start requests before the previous one is completed. Or just wait for an answer, and then wait a few more seconds before resending the call. You also don't want to spam AWS APIs with fast requests, even if they are synchronized between responses. In my controller application, I set the interval after 30 seconds, issued a request, waited for a response, and then subtracted the elapsed time from the interval and continued to sleep. In a multi-threaded model, I can thus monitor state changes on many objects at the same time, without overloading either my local processor or the API.

After a state change has been detected (and if you expect a new state to be waiting for you - be sure to handle the failure modes), you can get a wide range of descriptive information, including a public DNS address (in the case of instance objects) from the structure returned in the API response object.

+9
source share

Actually, you can POLL find out the status of an instance. Here is what Bash code is for this, just adapt it to JAVA. You have a similar command in the Java SDK, so you do not need to execute Bash with Java. The ec2-describe-instances command comes from the Amazon AWS CLI . I would start using a function or method to wait for the state to complete, to check if the instance is "pending", and fail if it does not start or "pending". Then write down the time and give it a maximum of 3 minutes, and just continue polling for the “start” status in the loop, checking the 3-minute limit. Return to the calling point, whichever comes first: “not started”, “start 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 } 
-one
source share

All Articles