Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining Amazon EC2 instance creation date/time

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!

like image 645
megazoe Avatar asked Sep 20 '13 11:09

megazoe


People also ask

How do I find my EC2 creation date?

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.

How do I find my instance of uptime?

In the server dashboard report, you can view the details of the SQL Server instance. The SQL Server instance uptime is in the Server Startup Time dashboard of the Configuration Details grid. The Server startup time is in HH: MM AM/PM format. As you can see in the above screenshot, the server startup time is 12:10 AM.

Does AWS use UTC time?

This service uses a fleet of satellite-connected and atomic reference clocks in each AWS Region to deliver accurate current time readings of the Coordinated Universal Time (UTC) global standard. The Amazon Time Sync Service automatically smooths any leap seconds that are added to UTC.


2 Answers

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.

like image 192
Venkatesh Wadawadagi Avatar answered Sep 24 '22 15:09

Venkatesh Wadawadagi


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

like image 41
Josh Hancock Avatar answered Sep 24 '22 15:09

Josh Hancock