how to find out when an EC2 instance was last stopped
You could use the reason
variable of stopped
instances:
import boto.ec2
conn = boto.ec2.connect_to_region("eu-west-1")
reservations = conn.get_all_instances()
for r in reservations:
for i in r.instances:
if i.state == 'stopped':
print "%s [%s] %s" % (i.id, i.state, i.reason)
Output:
i-11223344 [stopped] User initiated (2013-12-20 13:59:08 GMT)
This works also for terminated
instances (as long as they are still displayed).
I think that better practice would be:
import boto.ec2
conn = boto.ec2.connect_to_region("eu-west-1")
reservations = conn.get_all_instances()
instances = []
for reservation in reservations:
for instance in reservation.instances:
if "Name" in instance.tags.keys():
instances.append((instance.tags["Name"],
instance.get_console_output().timestamp))
You can also replace the if
and get whatever you want, but instance.get_console_output().timestamp
is the right way to get instance's stopped timestamp