1

I'm running a big fleet of EC2 spot instances. I need to know the current instance age (since launch time) from within each instance, using Python. I'm using boto3

caspillaga
  • 573
  • 4
  • 16
  • Possible duplicate of [Determining Amazon EC2 instance creation date/time](http://stackoverflow.com/questions/18916135/determining-amazon-ec2-instance-creation-date-time) – Mark B Jul 13 '16 at 03:28
  • Thanks Mark, but my problem is to achieve that from within the instance itself and using boto3 – caspillaga Jul 13 '16 at 03:39
  • In that case, look at this: https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Instance.launch_time This is really simple to figure out if you just look at the docs. – Mark B Jul 13 '16 at 03:44

1 Answers1

2

By "within the EC2 instance", I assume you want to execute the python code on EC2 instance. Once you've working AWS python code, it doesn't matter on which computer/machine you run, it will work as long as system meets python and network requirements.

If you're looking for launch_time, you can use the following code.

import boto3

ec2 = boto3.resource('ec2', region_name='instance_region_name')
instance = ec2.Instance('instance_id')
print instance.launch_time.strftime("%Y-%m-%d %H:%M:%S")

If you're looking for instance's creation time, Please refer my answer here.

Please Note that creation time and launch time both are different. You can launch the given instance as many times as you want, but creation happens only once

Community
  • 1
  • 1
Venkatesh Wadawadagi
  • 2,793
  • 21
  • 34