Presenter: Alright, lets learn how
to retain data on deletes.
So we can put something called a Deletion Policy on
any resource we want to control what happens when
the Cloud Formation gets template gets deleted.
So we can say Deletion Policy equals Retain and we'll
specify on on any resource we want and in that case,
Cloud Formation will preserve or backup that resource.
Which is kinda neat.
And we can specify, to keep a resource we'll specify retain.
And it works for anything.
For resources, for nested stacks, for anything you want.
There's another one called Deletion Policy equals Snapshots.
And this one is quite cool because it only applies to
EBS Volume, ElastiCache Cluster, Replication Groups
RDS DBInstance, DBCluster, and RedShift Cluster
but what we get is that the actual instance
gets deleted for whatever
I just named here but it will make a Snapshot
just before deleting it.
Hence we can keep our data.
So if you have a DEV environment and for some reason you
want to delete the DEV environment but you want to keep
the data for later use and analysis,
maybe you want to use a Deletion Policy equals Snapshot.
Finally, Deletion Policy equals Delete,
that's the default behavior.
That's what you would do.
And by the way there is a few other exception for DB Cluster
the default policy is Snapshots but for the rest of the
resources in Cloud Formation templates
Deletion Policy equals Delete.
And if you wanted to delete an S3 bucket something you
should know is that you first need to
delete everything in that bucket.
Make it empty before you can delete the S3 bucket yourself.
So lets go have a look at how we retaining
data on deletes work.
So I am going to open deletionpolicy.yaml
and very simply we have a my security group.
This is a security group that enables SSH access
on port 22 from anywhere.
And the Deletion Policy for this one is retain.
So we won't lose that security group
when we delete our stack.
And we have a EBS Volume, and its a EC2 Volume,
and this time the Deletion Policy is Snapshots.
And the properties is that the availability zone
is going to be in us-east-1a.
This size is going to be one gigabyte
and its going to be a gp2.
So when we delete our stack we should expect the
security group to remain,
the EBS Volume to go away,
but a snapshot to be made of it.
So lets go and create a stack.
I will upload a template file and I will chose my
Deletion Policy stack.
Alright, excellent.
Click on next and I'll call it Deletion Policy.
And I'm really bad at writing right now but whatever.
And we'll click on next and click on create stack.
And so very quickly, for us, this will prevision an
EBS Volume and a security group.
So as we can see now, both my security group and my
EBS volume are in create complete stage.
So in my resources, I refresh this, I have an EBS Volume
and I have a security group.
So here they are, I am going to close the rest.
Alright, heres my EBS Volume one gigabyte, gp2.
And here is my security group right here.
So now what I am going to do is go to my stack
and click on the stack name.
And then click on action, delete stack.
And this will delete the stack itself.
So I click on delete.
And so from what we've seen before when we delete
a Cloud Formation stack, everything should go away
but because we have specified, if you remember a
Deletion Policy, retain for the security group and
Snapshot for the EC2 Volume,
we shall see a very different behavior.
So lets go have a look.
Here we get a deletion in progress and if we look
at the events, as we can see, my EBS Volume
is getting Snapshots.
So it is actually creating a Snapshot for us.
And my security group was in status, delete skipped.
So this is kinda neat.
So now if we can see our Volume for now is still here,
but if I click on Snapshots, we can see that there is a
one gigabyte Snapshot that was created for me
automatically by Cloud Formation.
So lets go back to Cloud Formation stack.
Refresh everything so we can see now that the create
of the Snapshot is in create complete.
So now what should happen is that now that it is in
create complete, my EBS Volume should be in
delete in progress.
So its going to be actually deleted
because its been Snapshoted.
So lets go back to the volumes.
And as we can see, we do not have any EBS Volumes
anymore because it actually has been deleted.
So its a cool way to expose how things worked and
finally, our stack is in delete complete stage.
And so we have no more stacks to display.
So the outcome of this is that we have a security group
that still is there and we have a snapshot that got
created for us for this EBS Volume automatically.
So what you need to remember though is that
once you do these things,
well you need to delete these things manually.
So we'll delete this snapshot manually to clean
out properly and I will also delete the security group
manually to delete properly.
But it is quite a neat trick to see.
And remember, we get Deletion Policy retain,
Snapshot and by default, delete.
Alright, that's it.
I will see you in the next lecture.