Ends in

$3 OFF all reviewers plus chance to win freebies extended up to this week

AWS CloudFormation Deletion Policy

Deleting a stack on CloudFormation also removes all the provisioned resources in it. In some cases, you want some resources to be retained even after deleting its stack. The good thing is that you can do this by defining its DeletionPolicy.

This is pretty straightforward – you just need to define DeletionPolicy with Retain value and for the resources that support snapshot, (like RDS databases) you can set Snapshot as its value. With DeletionPolicy: Snapshot, a snapshot is created before a resource is deleted. This allows you to have a backup of the resource that’s been deleted from the stack.

Let’s say for example that you want to delete a deployed application. This app uses S3 for storing its object and RDS as its database, and you want to keep a copy of this resource as your reference. You may want to update its stack and add DeletionPolicy: Retain for S3 and DeletionPolicy: Snapshot for RDS before deleting it.

By default, deleting a stack will also delete its provisioned resources like the AWS::S3::Bucket that was launched using the below CloudFormation template.

This is an example of a CloudFormation stack template in YAML format:

AWSTemplateFormatVersion: 2010-09-09
Type: AWS::S3::Bucket
Value: !Ref ‘SampleBucket’
 Description: This is a sample bucket

awscloudformation deletionpolicy1


Adding DeletionPolicy: Retain on the template will retain the provisioned resources even after deleting its stack.

Tutorials dojo strip

AWSTemplateFormatVersion: 2010-09-09
Type: AWS::S3::Bucket
   DeletionPolicy: Retain
Value: !Ref ‘SampleBucket’
Description: This is a sample bucket

aws cloudformation deletionpolicy2


DeletionPolicy: Snapshot can be added on resources that support snapshots like the following:








In this example, we have here a Volume created along with an EC2 instance. Because Snapshot is defined as its DeletionPolicy, we expect this to create a snapshot when the stack is deleted.

AWSTemplateFormatVersion: 2010-09-09
Type: AWS::EC2::Instance
ImageId: ami-0615132a0f36d24f4
Type: AWS::EC2::Volume
DeletionPolicy: Snapshot
Size: 10
AvailabilityZone: !GetAtt Ec2Instance.AvailabilityZone

You can see from the Resources tab that an EC2 instance and a separate Volume was created.

aws cloudformation deletionpolicy3

aws cloudformation deletionpolicy4

We have two volumes here, volume mounted on the EC2 instance and the volume that was defined on the CloudFormation template.

aws cloudformation deletionpolicy5

After deleting its stack, the provisioned EC2 instance and volumes are deleted as well.

aws cloudformation deletionpolicy6

aws cloudformation deletionpolicy7

But because of the deletion policy defined as “DeletionPolicy: Snapshot” on the YAML file, a snapshot was created before the volume was deleted.

aws cloudformation deletionpolicy8



Tutorials Dojo portal

Win Exciting Freebies!

FREE AWS Exam Readiness Digital Courses

Enroll Now – Our Azure Certification Exam Reviewers

azure reviewers tutorials dojo

Enroll Now – Our Google Cloud Certification Exam Reviewers

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE Intro to Cloud Computing for Beginners

FREE AWS, Azure, GCP Practice Test Samplers

Browse Other Courses

Generic Category (English)300x250

Recent Posts

Written by: Lervin John Obando

Lervin is an experienced Cloud Systems Engineer with a strong background in managing and building solutions in Amazon Web Services (AWS) using primary services like EC2, S3, VPC, IAM, etc. He also has experience handling multi-cloud server infrastructures like Google Cloud Platform (GCP) and data-center operations across Linux and Windows platforms.He is an Associate Cloud Engineer – Google Cloud Certified, an AWS Certified Solutions Architect Associate, and an AWS Certified Cloud Practitioner. These certifications have proven his ability in architecting, deploying, and maintaining secure and efficient solutions applying the best practices across Amazon Web Services and Google Cloud Platform. Furthermore, he progressed his resourcefulness, analytical, and problem-solving skills when exposed to Network Administration, VMware Administration, Oracle Database, and SQL.He finished a degree in Bachelor of Science in Information Technology. His drive for continuous learning and grit for knowledge expansion and growth did not stop there. He also pursued various certifications and was able to pull them off, as mentioned above.As a Systems Engineer, stabilizing the systems and implementing strategic IT solutions are his primary commitment and value-added contributions to the organization in which he belongs. His core functions include resolving technical and cloud-related issues from routine to complex, running multiple command-line scripts, troubleshooting and networking, delivering recommendations and solutions for processes and facilities improvement, and providing excellent customer and client support.He is an outgoing and enthusiastic individual with a strong passion for music. During his free time, he loves to play guitar and travel with his motorcycle.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?