Preventing the deletion of EBS Volumes during the termination of AWS Spot Instances
I started learning about AWS (Amazon Web Services) a few weeks ago as part of the openSAP class. Although this blog is geared towards the openSAP Software Development on SAP HANA class, it may be useful for others who are new to AWS.
AWS Instance (HANA One Developer Edition):
Let us run through the process of creating an image (AMI — Amazon Machine Image) via the AWS Console:
The Delete on Termination flag controls the deletion of the EBS volume during termination of Spot Instances: If the flag is marked, the EBS volume is deleted and if it is unmarked, the EBS volume is not deleted.
EBS Volumes tab:
Here the non-root EBS volume devices of the Instance are named /dev/sdb1 through /dev/sdb6. If you take a look at the Device:/dev/.. dropdown to the left of the Delete on Termination: entry, you can only choose /dev/sdb through /dev/sdk. If our instance non-root devices were in the range /dev/sdb through /dev/sdk, we may be able to Remove the device with the true flag and Add a replacement entry with the false flag. This is the limitation of using the console to create an AMI for our Instance. An alternate method for creating an AMI with the Delete on Termination flag set to false for all the EBS volumes is described below.
Follow the steps in Installing the Amazon EC2 Command Line Interface Tools on Windows – Amazon Elastic Compute Cloud. You may skip step 7 since we will be accessing a Linux Instance.
Below are my Windows 7 environment variables and values:
JAVA_HOME = C:\Program Files\Java\jdk1.6.0_45
EC2_HOME = C:\AWS\CLI Tools\ec2-api-tools\ec2-api-tools-18.104.22.168
EC2_URL = https://ec2.us-east-1.amazonaws.com
AWS_ACCESS_KEY = <AWS account specific>
AWS_SECRET_KEY = <AWS account specific>
Let us issue a command from a Windows command prompt (CLI Commands List ==> Commands (CLI Tools) – Amazon Elastic Compute Cloud) and also save the output — we need this later.
ec2-describe-instance-attribute i-c88ef4ad –block-device-mapping
Below are the steps for creating and using a Spot Instance:
Step 1: Create Snapshots of your Instance (or terminated Spot Instance) EBS volumes:
You are free to choose any names for the Snapshots. I’m including the device name (from the CLI command output) in the Snapshot name for easy identification later. I have found it useful to name Snapshots as Base-Root-sda1, Base-sdb[1-6] for the initial base Snapshots, and Week[1-6]-Root-sda1, Week[1-6]-sdb[1-6] for Snapshots after Week [1-6].
Wait for the Snapshot to be created (green “Completed” status — should take less than a minute)
Repeat the above steps for the other 6 EBS volumes.
Below are the Snapshots for all the 7 EBS volumes:
Once you have created the Snapshots from EBS volumes, you may delete the EBS volumes (available state) if they are persisting from a terminated Spot Instance. Obviously , don’t delete the EBS volumes of the non-Spot Instance (in-use state) as shown in the screenshot above.
Step 2: Create an AMI from the Snapshots:
Obtain the Snapshot-to-Volume mapping as shown above. Obtain volume-to-device mapping from the CLI command (ec2-describe-instance-attribute) output and use it in the ec2-register command below.
Create a Windows .txt file with the lines below:
–name “Base-AMI-Rev56” ^
–description “Base AMI after upgrade to Rev 56” ^
–architecture x86_64 ^
–kernel aki-825ea7eb ^
–root-device-name /dev/sda1 ^
–region us-east-1 ^
–block-device-mapping “/dev/sda1=snap-f26356ac:10:false:standard” ^
–block-device-mapping “/dev/sdb1=snap-086b5e56:20:false:standard” ^
–block-device-mapping “/dev/sdb5=snap-a96a5ff7:20:false:standard” ^
–block-device-mapping “/dev/sdb4=snap-e95560b7:20:false:standard” ^
–block-device-mapping “/dev/sdb3=snap-6054613e:20:false:standard” ^
–block-device-mapping “/dev/sdb2=snap-db576285:20:false:standard” ^
ℹ ^ is the line continuation character for Windows Command line.
ℹ I obtained the kernel ID above from the Instance Description tab.
ℹ I obtained the architecture by creating an AMI via the AWS Console
❗ I found it safer to use region even if EC2_URL is defined as a Windows environment variable.
Copy and paste the above lines into a Windows command prompt and run it by hitting <Enter>:
❗ I could have created a *.cmd file above. The risk is that if you accidentally double-clicked on the *.cmd file, it will execute the commands in it.
❗ When you want to create the next Spot Instance, architecture, kernel, root-device-name, and region will remain the same as in the above CLI command. You may want to change the Name & Description (for example: –name “Week1-AMI-Rev56”, –description “AMI after Week 1”). The snapshot IDs will change.
Step 3: Request a Spot Instance:
Run through the other screens as you normally would.
Spot Instance EBS Volumes:
Save the volume-to-device mapping:
As soon as the Spot Instance starts running, issue the command below and save the output — we need this when we loop back to Step 2. Once the Spot Instance terminates, the EBS volumes change from in-use to available state at which point the volume-to-device mapping would be lost.
ec2-describe-instance-attribute i-ae0890cd –block-device-mapping
Step 4: Spot Instance Usage and termination:
You can now work with the Spot Instance .
The above 7 EBS volumes will remain (undeleted) during the Spot Instance termination regardless of whether the termination was user-initiated or AWS-initiated.
Below is the message you get when you terminate the Spot Instance — it indicates the EBS volumes will NOT be deleted.
Loop back to Step 1 when you need to create a new Spot Instance from the EBS volumes of the terminated Spot Instance.
You may delete the old AMIs/Snapshots or you may keep them around if you need to go back and create a Spot Instance from a previously saved AMI/Snapshots.
The method described above results in the persistence of EBS volumes after the termination of AWS Spot Instances.