AWS script to delete snapshot longer than 7 days

Asked

Viewed 155 times

0

I need to make a script to delete my snapshots in mine AWS longer than 7 days.

To perform the creation use this script, I hope it helps.

AWS ec2 create-image --no-reboot --instance-id i-131e8e06 --name "SITELINK-BKP-DIARIO-%date:~7,2%-%date:~4,2%-%date:~10,4%" --description "SITELINK-BKP-DIARIO"
  • This script works or is in trouble?

  • So man, this scrip that I talked about is what I use to accomplish the creation of the image. I need one to delete these created images longer than 7 days

  • That doesn’t look like a powershell, I could be wrong

  • This in dos os the script to delete can be in powershell or Docs has no problem, you know ?

1 answer

0

Just so as not to confuse anyone else (as you confused me when reading the title), it is AMI’s and not snapshots (EBS Snapshots from EC2).

That said, I can suggest the use of tags:

  1. add tags to the AMI after it is created, using ec2 create-tags id_da_nova_ami --tag timestamp=timestamp_de_agora --tag=tipoDeAMI=SITELINK_BKP
  2. when it comes to finding the obsolete Amis, do a search by filtering the tag tipoDeAMI, thus: ec2 describe-images --filter "tag-tipoDeAMI=SITELINK_BKP"
  3. process the returned Amis and see which ones have the tag timestamp smaller than the minimum value you want to keep. For these timestamps I would use something standard like Unix epoch (number of seconds or days since 1/1/1970)

To be honest, I usually write my scripts for AWS using Python with the SDK Boto (official). I wouldn’t risk trying to show you how to do what I listed above using bash or powershell.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.