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.
Reserved instances are a billing construct only.
If you buy a reserved instance for a m1.small in us-west-2a, all that means is that one of your on-demand RDS instances that's an m1.small in us-west-2a receives that lower hourly price (and incidentally, if you delete that instance and make a new one, the reservation's lower hourly rate just shifts onto the new instance). No instance is launched by reserving one.
Best Answer
With RDS this is easy. You will have some downtime that depends on the amount of data. For a small instance an hour, for a large instance set aside a day. The good part is that after your create the snapshot you can test independently without affecting your instance.
Everything is performed in the AWS RDS Console.
1) Create a snapshot of your RDS instance.
2) Copy the snapshot selecting a new master key.
3) Restore the instance or create a new instance from the snapshot.
Note: For items like this that modify data, I prefer to just create a new RDS instance. Verify everything and then shutdown the old instance.