AWS DevOps Blog

Delete Your Stacks But Keep Your Data

When you delete a stack, by default AWS CloudFormation deletes all stack resources so that you aren’t left with any strays. This also means any data that you have stored in your stack are also deleted (unless you take manual snapshots).  For example, data stored in Amazon EC2 volumes or Amazon RDS database instances are deleted.

But what if you want to retain your data when you or someone else deletes your stack? Maybe you want to migrate your data to another stack or maybe you want to prevent your data from being unintentionally deleted. If that’s the case, you can have CloudFormation automatically retain resources or take snapshots of your database resources. Doing so preserves your data even if your stack is deleted.

To retain a resource or to create a snapshot when a stack is deleted, specify a DeletionPolicy for the corresponding resource in your CloudFormation template. Describe the resource like you normally would and just add the DeletionPolicy attribute, as shown in the following example:

"myS3Bucket" : {
  "Type" : "AWS::S3::Bucket",
  "DeletionPolicy" : "Retain"
}

You can specify retain with any resource, but you can only create snapshots of resources that support snapshots, such as the AWS::EC2::Volume, AWS::RDS::DBInstance, and AWS::Redshift::Cluster resources.

If you launch a stack with this template snippet, CloudFormation creates an Amazon S3 bucket just like any other bucket. However, when the stack is deleted, CloudFormation deletes the stack and all stack resources except for the bucket. The bucket is still available, but you’ll need to use the S3 service to work with the bucket, not CloudFormation. Note that you’ll still be charged for any costs that are associated with the bucket.

For resources with a snapshot DeletionPolicy, the behavior is a little bit different because the resource is deleted. However, CloudFormation creates a snapshot of that resource before deleting it.

For instance, imagine that you launched a stack with the following template snippet, which has a snapshot DeletionPolicy associated with an RDS database:

"MyDB" : {
  "Type" : "AWS::RDS::DBInstance",
  "Properties" : {
    "DBName" : { "Ref" : "DBName" },
    "AllocatedStorage" : { "Ref" : "DBAllocatedStorage" },
    "DBInstanceClass" : { "Ref" : "DBInstanceClass" },
    "Engine" : "MySQL",
    "EngineVersion" : "5.5",
    "MasterUsername" : { "Ref" : "DBUser" },
    "MasterUserPassword" : { "Ref" : "DBPassword" },
    "Tags" : [ { "Key" : "Name", "Value" : "My SQL Database" } ]
  },
  "DeletionPolicy" : "Snapshot"
}

When you delete the stack, CloudFormation creates a snapshot of the database instance and then deletes the stack and all stack resources. The snapshot won’t show up in CloudFormation; you’ll need to use the RDS service to work with the snapshot. The name of the snapshot will include the stack name, the logical ID of the database instance, and other identifying information. You could also retain the database instance if you wanted to keep it up and running. But, depending on your goal, it might be more cost effective to create a snapshot. Note that you’ll be charged for any costs that are associated with the snapshot.

A DeletionPolicy is a great way to preserve your data after a stack is deleted. For more information, see DeletionPolicy Attribute in the AWS CloudFormation User Guide.