Monday, 7 August 2017

Delete old AMI images and snapshots

Many AWS customers have old AMI images or snapshots that are many years old that could potentially be deleted to save costs. It is a good cost saving to delete all unused AMIs and snapshots.

If we really want to be conservative one could save them to Glacier first before deleting AMIs/snapshots. This could be done with following pseudo code:
for s in `cat snapshots`
do
    aws ec2 create-volume --snapshot-id $s  > createvol.out
    tempvol=`grep VolumeId createvol.out`
    aws ec2 attach-volume --volume-id $tempvol --instance-id $myinstanceid --device /dev/xvdh
    dd if=/dev/xvdh  conv=sync,noerror bs=64K | gzip -c > /OneTBfilesystem/backup-$s.img.gz
    aws glacier upload-archive -archive-description "backup-$s.img.gz " --body /OneTBfilesystem/backup-$s.img.gz
    rm /OneTBfilesystem/backup-$s.img.gz   
    aws ec2 detach-volume --volume-id $tempvol
    aws ec2 delete-volume --volume-id $tempvol
done

Storage pricing in N Virginia region:
  • EBS snapshots: $0.05 per GB / month
  • Glacier: $0.004 per GB / month


An alternative is open source snap-to-s3 tool can be used to migrate snapshots to S3, this could be used with S3 lifecycle policy to automatically moves S3 objects to Glacier after one day:

No comments:

Post a Comment