mlevit / aws-auto-cleanup

Programmatically delete AWS resources based on an allowlist and time to live (TTL) settings
MIT License
495 stars 55 forks source link

Use root device attach time instead of launch time to get EC2 instance age #129

Closed atqhg23 closed 1 year ago

atqhg23 commented 2 years ago

Describe the bug Currently, the launch time is used to determine the age of the EC2 instance, but this value is reset when someone restarts the instance allowing users to indefinitely keep an instance from being cleaned up.

To Reproduce

  1. Launch an EC2 instance
  2. Make note of the launch time
  3. Stop and start the instance
  4. The launch time is updated to when the instance was started

Expected behavior I looked into this and it seems like looking at the root volume attach time is the best way to determine the actual age of an instance.

mlevit commented 2 years ago

Thanks for raising this @atqhg23. Really interesting discovery. Let me look into it and see what the best approach would be.

atqhg23 commented 1 year ago

The issue with using the root volume attach time is that the root volume can be replaced which would return the wrong time if it had been replaced.

I checked with AWS on this, and it seems like the best approach would be to use the attach time of the instance's primary ENI since the primary ENI cannot de detached or deleted until the EC2 instance is deleted as well. The primary ENI can be identified by looking at the DeviceIndex, the primary ENI will always have the DeviceIndex set to 0.

mlevit commented 1 year ago

Thanks for that info @atqhg23. I've made the change within https://github.com/servian/aws-auto-cleanup/tree/better-ec2-launch-time

Mind testing it out for me? 😄

atqhg23 commented 1 year ago

Will do, thanks!

atqhg23 commented 1 year ago

This worked! Thanks again for the help