1

Is there a way to check if an AWS instance has finally come up in python either using boto3 or otherwise. The running state doesnt differentiate between rebooting and finally up phase.

Oliver Blue
  • 677
  • 2
  • 9
  • 22
  • What is your definition of "up"? – ydaetskcoR Apr 13 '16 at 07:22
  • Up as in all the processes have started which are supposed to ( If I reboot through putty, finally up would mean allowing me to login again) – Oliver Blue Apr 13 '16 at 07:29
  • So you mean port 22 is open? – ydaetskcoR Apr 13 '16 at 07:29
  • Yes. If there is some API available in boto3 which can tell me about instance directly, thats even better – Oliver Blue Apr 13 '16 at 07:31
  • You can use socket for this part instead of boto – ydaetskcoR Apr 13 '16 at 07:34
  • describe_instances ALREADY show the states required. Don't reinvent the wheel. If it is rebooting, it will not show "running". You can try it yourself, since rebooting unlike Stop+Start, which will not cost your an instance hour. – mootmoot Apr 13 '16 at 08:02
  • 1
    It stays on running while I am unable to login(during reboot). So if I send some commands checking that state is running, it still wont run. @ydaetscoR: Will try it out. – Oliver Blue Apr 13 '16 at 08:26
  • This is a good question, clear and concise. However, I think you should explain a little bit more about your main goal as you might not being considering other approaches to you're overall project. Could you tell us a bit more about your goals? What other part of the system are waiting for your instance to be 100% 'up'? – Héctor Valverde Apr 13 '16 at 08:52
  • There are a bunch of instances which need to be rebooted after the VMs get created. I need to fire some command once all these are up. Now, I can't fire these commands unless all are up so I need a way to test that all the VMs are up – Oliver Blue Apr 13 '16 at 09:32

3 Answers3

4

If you just want to check that a remote port is open you could use the built-in socket package.

Here's a quick modification of this answer of waiting for a remote port to open:

import socket
import time

def wait_for_socket(host, port, retries, retry_delay=30):
    retry_count = 0
    while retry_count <= retries:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((host, port))
        sock.close()
        if result == 0:
            print "Port is open"
            break
        else:
            print "Port is not open, retrying..."
            time.sleep(retry_delay)
hum3
  • 1,563
  • 1
  • 14
  • 21
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
  • I just realized, not all the nodes have a public ip. What can I do in that case? I wish to give public ip to only one of the nodes which will have internal access to other nodes – Oliver Blue Apr 13 '16 at 08:31
  • 1
    Perhaps putting this script into AWS Lambda, which interact with VPC subnet. – mootmoot Apr 13 '16 at 13:57
3

All the info is available inside boto3 documentation http://boto3.readthedocs.org/en/latest/reference/services/ec2.html

This will show all the information of the instances.

import boto3
reservations = boto3.client("ec2").describe_instances()["Reservations"]
for reservation in reservations:
  for each in reservation["Instances"]:
    print " instance-id{} :  {}".format(each["InstanceId"], each["State"]["Name"])

# or use describe_instance_status, much simpler query
instances = boto3.client("ec2").describe_instance_status()
for each in instances["InstanceStatuses"]: 
  print " instance-id{} :  {}".format(each["InstanceId"], each["InstanceState"]["Name"])

From the documentation :

State (dict) --

The current state of the instance.

Code (integer) --

The low byte represents the state. The high byte is an opaque internal value and should be ignored.

0 : pending
16 : running
32 : shutting-down
48 : terminated
64 : stopping
80 : stopped
Name (string) --

The current state of the instance.

In fact, there is no code state show "rebooting" inside the documentation. I can't really test it out on my own EC2 instances , because after I do reboot, it seems the instances reboot so fast that AWS console doesn't have a chance to show a "Rebooting" state.

Nevertheless, http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/monitoring-system-instance-status-check.html

The following are examples of problems that can cause instance status checks to fail:

Failed system status checks

Incorrect networking or startup configuration

Exhausted memory

Corrupted file system

Incompatible kernel

mootmoot
  • 12,845
  • 5
  • 47
  • 44
2

You could also use the InstanceStatusOk waiter in boto3 or whatever waiter is approprate.

import boto3

instance_id = '0-12345abcde'

client = boto3.client('ec2')
client.reboot_instances(InstanceIds=[instance_id])

waiter = client.get_waiter('instance_status_ok')
waiter.wait(InstanceIds=[instance_id])

print("The instance now has a status of 'ok'!")
maafk
  • 6,176
  • 5
  • 35
  • 58