Is it possible to force Re-creation of EC2::Instance or RDS::DBInstance in amazon cloudformation?
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:
{
"Resources" : {
"MyEC2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
// ... other properties ...
"UserData": {
"Fn::Base64" : {
"Fn::Join" : [ ":", [
"#!/bin/bash\n",
"# Version: 1.0\n",
// ... rest of user data ...
]]}
}
}
}
}
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
:
{
"Resources" : {
"MyDB" : {
"Type" : "AWS::RDS::DBInstance",
"Properties" : {
// ... other properties ...
"DBSnapshotIdentifier": "<db snapshot ID>"
}
}
}
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:
For most resources, changing the logical name of a resource is equivalent to deleting that resource and replacing it with a new one. Any other resources that depend on the renamed resource also need to be updated and might cause them to be replaced. Other resources require you to update a property (not just the logical name) in order to trigger an update.
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.