AWS Clean-Up Automation
With the help of Amazon’s huge infrastructure, Enterprises today can make use of numerous AWS tools, Auto Scaling, and Elastic Load Balancing mechanisms to scale their applications up or down based on demand. In general, while using AWS, organizations focus on automated bootstrapping of the VMs and deploying application on it in automated manner. However, when the purpose is met, very few bother to clean-up the resources when EC2 instance or AWS resource is terminated/deleted. This becomes more critical especially while working with Auto Scaling groups. Enterprises might require to perform different clean-up activities when EC2 instance is terminated. This may include clean-up of Chef nodes, clean-up for agent registrations (e.g Zabbix agents), security keys (e.g OSSEC), instance snapshots, s3 buckets data deletion and many such dependent clean-up activities. Following section describes few such approaches to perform clean-up with pros and cons of each approach. There is no “fit for all” solution here but it actually depends on your own environment and requirements.
The following approaches are described on the basis of “Reliability”, “Complexity” and “Urgency” of the clean-up activities. Based on your needs you should pick right approach.
In order to mitigate reliability aspect of the given solutions you could additionally have a batch script which performs regular clean-up activities irrespective of real time events from CloudWatch. For example, for chef node clean-up, you could also have a batch job which runs nightly, lists all registered chef nodes, queries AWS for existence of given nodes, and if node is not alive, it simply deletes that node. This makes sure that even if you fail to process instance termination event, such script will perform the required clean-up at a later time.