17

Is it possible to determine (via boto) when a particular EC2 instance was created?

http://boto.readthedocs.org/en/latest/ref/ec2.html doesn't seem to be giving anything helpful in this case. Need to find out the creation date of a particular set of EC2 instances.

Thanks!

Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
megazoe
  • 545
  • 1
  • 4
  • 17

5 Answers5

10

There's is no attribute called create_time for EC2 instance, only launch_time is available.

However, you can use the following Python code to know when the volume was created, which in turn gives you instance creation time (note that I'm talking about the volume attached while creating instance):

import boto3
ec2 = boto3.resource('ec2', region_name='instance_region_name')
volume = ec2.Volume('vol-id')
print volume.create_time.strftime("%Y-%m-%d %H:%M:%S")

The alternative is using custom code. When you create instances with create_instances(), you can log the launch_time for given instance along with it's instance ID and name to some place like DynamoDB so that you can retrieve the "create times" whenever you want using the instance IDs.

showdev
  • 28,454
  • 37
  • 55
  • 73
Venkatesh Wadawadagi
  • 2,793
  • 21
  • 34
  • When is this different from the attachment time shown in the AWS Console, if ever? – Zachary Ryan Smith Oct 17 '22 at 13:44
  • @ZacharyRyanSmith Time shown in AWS console is 'launch time' not 'instance creation time', note that both need not be same. If you stop the instance in between and start it again, launch time will differ from instance creation time – Venkatesh Wadawadagi Jan 18 '23 at 17:46
8

Assuming you are using an EBS-backed instance and aren't doing any fancy drive juggling, the best way to figure out the creation date of an instance is to look at the creation time of the drive's root volume. While the launch time of the instance will change every time you stop and start it, the creation time of an EBS volume is static.

Here's a quick script to find the creation time of the instance you're currently running from:

import boto
import subprocess

instance_id = subprocess.check_output(['curl', '-s', 'http://169.254.169.254/latest/meta-data/instance-id'])

conn = boto.connect_ec2()

root_device = conn.get_instance_attribute(instance_id, 'rootDeviceName')['rootDeviceName']
root_vol = conn.get_all_volumes(filters={"attachment.instance-id": instance_id, "attachment.device": root_device})[0]

print root_vol.create_time

Note that this requires the IAM Role of the instance to have ec2:DescribeInstanceAttribute and ec2:DescribeVolumes permissions

Josh Hancock
  • 795
  • 10
  • 15
3

It is not possible to get the creation time of EC2 instance directly. Since Launch time of EC2 instance will get updated upon every start and stop of Instance.

We can get Instance creation time by 2 ways:

1) By obtaining Network interface attach time of Instance

2) By obtaining Volume attach time as shown above.

How to get Network Interface Attach time in boto3

import boto3
from datetime import datetime
instance_details = client.describe_instances()
num_ins=(len(instance_details['Reservations'])
for x in range(num_ins):


   InstanceID=(instance_details['Reservations'][x]['Instances'][0]['InstanceId'])
   NetworkInterfaceID = (instance_details['Reservations'][x]['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId'])
   NetworkInterface_details = client.describe_network_interfaces(NetworkInterfaceIds=[NetworkInterfaceID])
   networkinterface_id_attachedtime = NetworkInterface_details['NetworkInterfaces'][0]['Attachment']['AttachTime']

   print(networkinterface_id_attachedtime)

Prints the Network interface attached time for instances present.

Roopa
  • 305
  • 1
  • 2
  • 11
0

As shown here:

http://boto.readthedocs.org/en/latest/ref/ec2.html#module-boto.ec2.instance

Each Instance object has an attribute called launch_time which contains a IS08601 datetime string representing when the instance was launched.

In boto, you could do something like this:

import boto.ec2

conn = boto.ec2.connect_to_region('us-west-1')
reservations = conn.get_all_instances()
for r in reservations:
    for i in r.instances:
        print('%s\t%s' % (i.id, i.launch_time)
garnaat
  • 44,310
  • 7
  • 123
  • 103
  • 1
    But is that the same as creation time? Am confused between launch time and creation time. :( – megazoe Sep 20 '13 at 13:59
  • 1
    Maybe I'm confused about what you are looking for. Launch time will tell you when that particular instance was actually started. As in when the VM was spun up and it began booting. Is that what you are looking for? – garnaat Sep 20 '13 at 14:06
  • 14
    Actually no, it is not equal. If you launch and instance, stop it overnight, and start it in the morning, your launch time will be the morning's date, not the date when you first created the instance. – npiani Nov 20 '13 at 17:03
  • @npiani AFAIK, creation time is not stored anywhere in instance meta. – alko Dec 04 '13 at 14:16
0

if you want to calculate the current uptime based on launchtime of an EC2 instance, one can try this:

import datetime
lt_datetime = datetime.datetime.strptime(i.launch_time, '%Y-%m-%dT%H:%M:%S')
lt_delta = datetime.datetime.utcnow() - lt_datetime
uptime = str(lt_delta)
admirableadmin
  • 2,669
  • 1
  • 24
  • 41