-1

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.

boardrider
  • 889
  • 2
  • 15
  • 26
  • Do you need to delete only stopped instances? – ALex_hha Jan 04 '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 04 '18 at 23:07
  • Yes, @ALex_hha - I'd eliminate only stopped machines. – boardrider Jan 05 '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 06 '18 at 14:56

1 Answers1

1

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.