Real-time DevOps AWS Tasks: Deleting AWS AMI and Snapshot using shell script

Task: Recently I got a task for deleting old un-used AMIs from our AWS account. The total count is around 1500+ after validation. If I delete them manually then definitely it will take more than 5–8 hours. And one more thing is, we have to delete the snapshots again after deregistering the AMIs.

So I have decided to create a shell script (of course, we can use other languages too like Python with boto3 library). The first thing I have checked is the AWS CLI documentation about the AMI deregistration. But the problem is, we need to delete the respective snapshots as well after deregistering the AMI. Hence we need to get the list of snapshots associated with these AMIs so that we can delete them as well using AWS CLI.

Our script needs to be like this

  1. Read the AMI ids line by line from a file
  2. Get the list of snapshots that are associated with this AMI
  3. Deregister the AMI
  4. Delete the snapshots which we got from step 2
  5. Repeat steps 2,3 and 4 respectively

The final code will be like this:

sample ami_list.txt content:

$cat ami_list.txt
ami-1234555
ami-9876554

Thanks for reading this post. will write about another real-time task shortly.