caspillaga
caspillaga

Reputation: 563

Get AWS EC2 Spot instance launch time from within the instance itself in python

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

Upvotes: 1

Views: 1306

Answers (1)

Venkatesh Wadawadagi
Venkatesh Wadawadagi

Reputation: 2943

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

Upvotes: 2

Related Questions