It's possible to force the re-creation of a EC2 or RDS instance using cloudformation stacks?
My stack goes stuck in a point where simply destroying and creating the resource will fix it, instead of that I had to delete entire stack to continue work.
edit:
This issue hit me twice. First I created an AWS::RDS::Instance with some defaults and then tried to downgrade it to "EngineVersion" : "5.5". Changing this its suposed to happen with some interruption, but mysql instances cannot be downgraded from 5.6 to 5.5 so the stack was left in UPDATE_FAILED state and I cannot be able to recreate RDS without a nasty trick.
The other occurrence was that i have several "AWS::EC2::Instance" which downloads and executes an script from it's "UserData" obviously if Y change the downloaded script I must recrete the instance, and theres no way to do so. Once again I use the same nasty trick to get the machine recreated.
The nasty trick:
Instead of using an autoscaling group of one machine, I solved both problems changing the availability zone in the properties... but left me with a bad taste
For instance store-backed EC2 instances, one trick is to add a comment to the user data script containing a version number, date, or similar, then change that whenever you want the instance recreated:
Any change to
UserData
will cause the instance to be replaced (i.e., regenerated). The behavior of the user data script should be the same, though, since the only modification is a comment. Note that this doesn't work for EBS-backed instances.For RDS, you could take a DB snapshot of the current RDS instance, then modify your template to use that snapshot with
DBSnapshotIdentifier
:Whenever
DBSnapshotIdentifier
is changed, the database instance will be replaced. Using snapshots will also let you keep the data from when the snapshot was made. (If you want to wipe the data, you could create an empty snapshot and pass that as input. Or delete and recreate the entire CloudFormation stack.)A more generic approach is to change the logical name of the resource. From Modifying a Stack Template in the CloudFormation docs:
If you put it into an AutoScalingGroup, you can edit the AutoScalingGroup min/max/default to 0, then as soon as it starts to destroy the old instance, you can then put the min/max/default to 1/1/1 and presto: new instance.
If your EC2 are into an AutoScalingGroup you can set the
AutoScalingGroupName
property with a version number in it.Every time you change that version number CFN will: 1. create a new auto-scaling group and spin up the desired instances 2. kill instances in the old auto-scaling group and delete it
Here is a piece of code from my stack where I use this technique to force a large number of EC2 machines to re-create and automatically pull new software from S3.