Can you suggest an automatic procedure to collect all AWS/EC2 instances for later deletion of unnecessary ones?

Unused (stopped) instances tend to accumulate in companies that use AWS, which wastes money and eventually inhibit the creation of new instances (when instance number is limited per AWS site).

If you have any procedures to automate the collection of stopped/running EC2 instances, and procedures for deletion of EC2 instances (with or without deletion protection), do share.

  • Do you need to delete only stopped instances? – ALex_hha Jan 4 '18 at 17:07
  • It would probably be fairly easy to write a lambda script to do this. Possibly easier and safer to address this with policy than automation. – Tim Jan 4 '18 at 23:07
  • Yes, @ALex_hha - I'd eliminate only stopped machines. – boardrider Jan 5 '18 at 17:38
  • As Tim suggested, a Lambda function would solve your use case. When an EC2 instance is stopped, State transition reason would give you the time when it was stopped. Also, CloudTrail stores 90 days worth of events. You can do a LookupEvents call and find out if the instance was stopped in the last 'x' days. Once the instance ids are identified, a TerminateInstances API call would terminate the instances. – Krishna Kumar R Jan 6 '18 at 14:56

Cloud Custodian can handle this use case. There is an example set of policies close to what you are looking for at http://www.capitalone.io/cloud-custodian/docs/usecases/ec2unpatchedworkflow.html and in left hand side near top is a link to "Getting Started", and if the docs are not enough the community at https://gitter.im/capitalone/cloud-custodian is often available to help people get started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.