Deleting Resources

Resources cost money, and some resources cost money even when you are not actively using them. It is therefore imperative that you clean up resources that are created as you deploy applications, especially during development and testing. Happily, CloudFormation stacks are an especially good way to manage resources. When you delete a CloudFormation stack in the AWS console, the resources associated with it are also deleted. This is a much under-appreciated aspect of CloudFormation, and it is by itself a good reason to make use of this service for application deployment, as anyone who has accumulated lots of stale and unused resources in the AWS accounts can attest. Deleting stale resources manually and one by one is time-consuming and difficult, because one must determine whether the resources are indeed unused or, as is often the case, are referenced by other resources that may themselves be stale. CloudFormation addresses all these problems.

However, there are certain resources that CloudFormation will not automatically delete when tearing down a stack. The most common example is an S3 bucket that contains objects. CloudFormation will inform you that the bucket is not empty and cannot be removed. You must at that point remove its objects either using the console or the AWS command line or something. If you delete the stack a second time in the console, the bucket will then be removed.