Is it possible to force Re-creation of EC2::Instance or RDS::DBInstance in amazon cloudformation?
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.