Skip to content

Instantly share code, notes, and snippets.

@sudharsans
Last active December 20, 2023 19:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sudharsans/914b3701d90a7e1e95667ec42a283f56 to your computer and use it in GitHub Desktop.
Save sudharsans/914b3701d90a7e1e95667ec42a283f56 to your computer and use it in GitHub Desktop.
Python Boto3 script to find the number of minutes an instance has been running.
##
# Python Boto3 script to find the number of minutes an instance has been running.
## Output
# i-00asdf5xxx50c96aa 84 days, 13:17:51.468559
# i-0260a1fxxx27ec894 6 days, 13:48:04.468643
# i-0acxxx6c630af322 13 days, 12:13:00.468659
# i-069bxxxd41bf975eb 19:42:08.468670
import boto3
from datetime import datetime, timezone
ec2 = boto3.client('ec2')
response = ec2.describe_instances (Filters=[
{
'Name': 'instance-state-name',
'Values': ['running']
}
])
for r in response['Reservations']:
for instance in r['Instances']:
diff_time = datetime.now(timezone.utc) - instance['LaunchTime']
print (instance['InstanceId'],diff_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment