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!
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
Prints the Network interface attached time for instances present.
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:
Note that this requires the IAM Role of the instance to have
ec2:DescribeInstanceAttribute
andec2:DescribeVolumes
permissionsAs 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:
There's is no attribute called
create_time
for EC2 instance, onlylaunch_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):
The alternative is using custom code. When you create instances with
create_instances()
, you can log thelaunch_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: