Well AWS EBS that is Elastic block store is a very important and useful service provided by AWS. Its a permanent and shared storage and is used with various applications deployed in AWS EC2 instance. Automation is playing a vital role in provisioning or managing all the Infrastructure and related components.
Having said that , In this tutorial we will learn what is an AWS EBS , AWS EBS Snapshots and many amazing things about storage types and how to delete EBS snapshots using shell script on AWS step by step.
AWS EBS is your pendrive for instances, always use it when necessary and share with other instances.
Table of Content
- What is Shell script ?
- What is AWS EBS ?
- What are EBS Snapshots in AWS ?
- Install AWS CLI Version 2 on windows machine
- How to Delete EBS Snapshots from AWS account using shell script
What is Shell Scripting or Bash Scripting?
Shell Script is simply a text of file with various or lists of commands that are executed even on terminal or shell one by one. But in order to make thing little easier and run together as a group and in quick time we write them in single file and run it.
Main tasks which are performed by shell scripts are : file manipulation , printing text , program execution. We can include various environmental variables in script that can be used at multiple places , run programs and perform various activities are known as wrapper scripts.
A good shell script will have comments, preceded by a pound sign or hash mark, #, describing the steps. Also we can include conditions or pipe some commands to make more creative scripts.
When we execute a shell script, or function, a command interpreter goes through the ASCII text line-by-line, loop-by-loop, test-by-test, and executes each statement as each line is reached from the top to the bottom.
What is EBS ?
EBS stands for Amazon Elastic block store which is permanent storage just like your pendrive or harddisk. You can mount EBS volume to AWS EC2 instances. It is very much possible to create your own file system on top of these EBS volumes.
EBS volumes are mounted on AWS EC2 instance and are not dependent on AWS EC2 instance life. They remain persistent.
Key features of EBS
- EBS can be created in any Availability zones
- EBS cannot be directly attached with any instance in different Availability zone. We would need to create a Snapshot that is like a backup copy and then from that snapshot restore it to new volume and then finally use it in other Availability zone.
What are HDD and SSD storage ?
HDD ( Hard disk drive )
Hard disk drive is a old technology. They depends on spinning disks and platters to read and write data. There is a motor which spins the platter whenever any request comes to read or write the data. Platter contains tracks and each track contains severs sectors. These drives run slowly. They are less costly.
SSD ( Solid State drive )
Solid State drive is a new technology. It uses flash memory so they consume less energy and runs much faster as compared to HDD and is highly durable. It depends on electronic energy rather than mechanical energy so its easy to maintain and more efficient. They are more costly than HDD’s.
- EBS are classified further into 4 types
- General purpose SSD : Used in case of general use such as booting a machine or test labs.
- Provisioned IOPS SSD: Used in case of scalable and high IOPS applications.
- Throughput Optimized HDD: These are low cost magnetic storage which depends on throughput rather than IOPS such as EMR, data warehouse.
- Cold HDD: These are also low cost magnetic storage which depends on throughput rather than IOPS.
How to create AWS EBS manually in AWS account?
- You must have AWS account to create AWS EBS. If you don’t have AWS account please create from AWS account or AWS Account
- Go to AWS account and on the top search for AWS EC2 service
- Click on Create volume
- Now fill all the details such as type of volume, size , IOPS , Tags etc.
- Now click on Create volume and verify
What are EBS Snapshots in AWS ?
We just discussed about EBS that is storage. There are high chances that you might require backup to keep yourself in safe position. So basically EBS snapshots are backup of EBS volumes. There is also a option to backup your EBS with point in time snapshot which are incremental backups and these gets stored in AWS S3. This helps in saving tons of mins by keeping the snapshots with only difference to what changed in previous backup.
How to create EBS snapshots?
- Go to AWS EBS console
- Choose the AWS EBS volume for which you wish to create Snapshot
- Add the description and Tag and then click on Create Snapshot.
- Verify the Snapshot
- If you wish to create AWS EBS snapshots using AWS CLI , please run the command ( Make sure you have AWS CLI installed and if not then we have explained below in this tutorial.
aws ec2 create-snapshot --volume-id <vol-1234567890> --description "My volume snapshot"
- AWS account to create AWS IAM user. If you don’t have AWS account please create from AWS account or AWS Account
- Windows 7 or plus edition where you will execute the shell script.
- Python must be installed on windows machine which will be required by AWS cli. If you want to install python on windows machine follow here
- You must have Git bash already installed on your windows machine. If you don’t have install from here
- Code editor for writing the shell script on windows machine. I would recommend to use visual studio code on windows machine. If you wish to install visual studio on windows machine please find steps here
In this demo , we will use shell script to launch AWS IAM user. So In order to use shell scripts from your local machine that is windows you will require AWS CLI installed and configured. So First lets install AWS CLI and then configure it.
Install AWS CLI Version 2 on windows machine
- Download the installed for AWS CLI on windows machine from here
- Select I accept the terms and then click next button
- Do custom setup like location of installation and then click next button
- Now you are ready to install the AWS CLI 2
- Click finish and now verify the AWS cli
- Verify the AWS version by going to command prompt and type
Now AWS cli version 2 is successfully installed on windows machine, now its time to configure AWS credentials so that our shell script connects AWS account and execute commands.
- Configure AWS Credentials by running the command on command prompt
- Enter the details such as AWS Access key , ID , region . You can skip the output format as default.
- Check the location on your system C:\Users\YOUR_USER\.aws file to confirm the the AWS credentials
- Now, you’re AWS credentials are configured successfully.
How to Delete EBS Snapshots from AWS account using shell script
Now we have configured AWS CLI on windows machine , its time to create our shell script to delete EBS snapshots. In this demo we will delete two AWS EBS snapshots which already exists in AWS account. Lets get started.
- Create a folder on your desktop and under that create file delete-ebs-snapshots.sh
#!/usr/bin/env bash # To check if access key is setup in your system if ! grep -q aws_access_key_id ~/.aws/config; then if ! grep -q aws_access_key_id ~/.aws/credentials; then echo "AWS config not found or CLI not installed. Please run \"aws configure\"." exit 1 fi fi # To Fetch all the SNAPSHOT_ID with Tag Name=myEBSvolumesnapshot SNAPSHOTS_ID=$(aws ec2 describe-snapshots --filters Name=tag:Name,Values="myEBSvolumesnapshot" --output text | cut -f 6) echo $SNAPSHOTS_ID # Using For Loop Delete all Snapshots with Tag Name=myEBSvolumesnapshot for id in $SNAPSHOTS_ID; do aws ec2 delete-snapshot --snapshot-id "$id" echo "Successfully deleted snapshot $id" done
- Now open visual studio code and open the location of file delete-ebs-snapshots.sh and choose terminal as Bash
- Now run the script
- Script ran successfully , now lets verify if AWS EBS Snapshots with Tag Name=myEBSvolumesnapshot successfully got deleted by going on AWS account.
In this tutorial, we demonstrated what is an AWS EBS , AWS EBS Snapshots and learnt many things about storage types and learnt how to delete EBS snapshots using shell script on AWS step by step. AWS EBS is your pendrive for instances, always use it when necessary and share with other instances.
Hope this tutorial will help you in understanding the shell script and working with AWS EBS on Amazon cloud. Please share with your friends
2 thoughts on “How to Delete EBS Snapshots from AWS account using Shell script”
I’m not sure if AWS changed the output of their describe-snapshots, but I had to update it to use “aws ec2 describe-snapshots –filters Name=tag:Name,Values=”myEBSvolumesnapshot” –output text | cut -f 7″ instead of “cut -f 6” because it was grabbing the wrong value.
Thank you for your message Weez. It could be , could you try cut -f 7 again to double check this?