In the Amazon Web Services cloud (AWS), I want to create a launch template that launches an AMI with a root EBS volume and a separate EBS data volume. When the instance is deleted, I want both volumes deleted, but first I want the data volume to have a snapshot taken using a DeletionPolicy
of Snapshot
rather than Delete
or Retain
. I would like to create this launch template with the AWS CDK in Python, but as a last resort it would be helpful if I could configure the EBS volumes this way by any means, including a script run after the instance launches.
Currently, I have the launch template configured to create the EBS volumes and delete them when the AMI terminates. So far, so good. What I cannot find is a say to have the "data" volume take a snapshot before it deletes. The closest I can come is to trigger a script to respond to the instance termination signal by initiating a snapshot, but I worry that that is unreliable.
The AWS CloudFormation documentation claims you can set a DeletionPolicy
of Snapshot
on an AWS::EC2::Volume
. I mostly cannot figure out how to get that to work for a random EBS volume attached to an EC2 instance as a data volume (not root or AMI volume).
- I do not see any place in the AWS Console to set the DeletionPolicy
- I do not see any place in the
aws
CLI to read the DeletionPolicy - Looking at the LaunchTemplate, I do not see any place to set this policy
So possibly I am thinking about this the wrong way, or possibly the documentation is just misleading. After pouring over the documentation for several hours, I am left to believe that this is not a real option, but rather a hack to enable "snapshot on delete" for RDS clusters.
How can I get this to work for a normal EC2 instance?