Latchu
Latchu

Reputation: 61

How to know EC2 instance stopped time?

I really need to know about the stopped time of AWS EC2 instances. I have checked with AWS cloudtrail, but its not easy to find the exact stopped EC2 instance. Is possible to see exact time of stopped EC2 instances by aws-cli commands or any boto3 script?

Upvotes: 0

Views: 5027

Answers (4)

Shanu
Shanu

Reputation: 171

As explained in other answers, StateTransitionReason from describe_instances will give state transition time but this is only for User Initiated shutdowns.

If it is OS initiated shutdown, State transition reason will be Client.InstanceInitiatedShutdown: Instance initiated shutdown and in this case, we can't find the stopped time.

Upvotes: -1

dz902
dz902

Reputation: 5818

Sometimes time is missing from StateTransitionReason, you can use CloudTrail and search for Resource Name = instance ID to find out StopInstance(s) API calls.

By default you can track back 90 days, or indefinitely if you create your own trail.

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269091

AWS Config keeps track of the state of resources as they change over time.

From What Is AWS Config? - AWS Config:

AWS Config provides a detailed view of the configuration of AWS resources in your AWS account. This includes how the resources are related to one another and how they were configured in the past so that you can see how the configurations and relationships change over time.

Thus, you could look back through the configuration history of the Amazon EC2 instance and extract times for when the instance changed to a Stopped state.

Upvotes: 1

Marcin
Marcin

Reputation: 238051

You can get this info from StateTransitionReason in describe-instances AWS CLI when you search for stopped instances:

aws ec2 describe-instances --filter Name=instance-state-name,Values=stopped --query 'Reservations[].Instances[*].StateTransitionReason' --output text

Example output:

User initiated (2020-12-03 07:16:35 GMT)

Upvotes: 2

Related Questions