Is there an elegant way to shutdown and delete instances?

I have recently been recently using Amazon's CloudFormation to launch instances of EC2 Ubuntu machines running MongoDB.

When it comes to deleting the stack it often fails I guess due to not being able to dismount the ebs raid 10 drives Mongo is using.

I would like to something in that template (currently has bash script in user data) or machine to intercept that message, shutdown Mongo and dismount the drives so the delete can happen cleanly rather than having to be performed twice. Is there a simple way to augment CloudFormation to handle stack deletes more cleanly?

I realise there are alternatives (OWS running Chef 9 still / Chef installed on one of our machines in AWS / Juju / Puppet / etc / etc ) but right here and now I just want to get this working cleanly first if at all possible.


Solution 1:

We have similar problem due to the S3 buckets CF stack created could not be deleted if not empty.

Our solution/hack so far is have a bash script calling the AWS API using aws-cli to iterate though all S3 buckets and delete the contents inside first then call the aws cloudformation delete-stack.

For your problem you can do the same just detach ebs volumes on your Mongo instance before you delete the stack.