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 any
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
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.
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)
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.
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)