AWS script to delete snapshot greater than 7 days

0

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

To make 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"
    
asked by anonymous 09.12.2015 / 13:04

1 answer

0

Just to not confuse anyone else (as I got confused by reading the title), these are AMI's rather than snapshots (EBS Snapshots from EC2).

That said, I can suggest using tags:

  • add tags to AMI after it is created, using ec2 create-tags id_da_nova_ami --tag timestamp=timestamp_de_agora --tag=tipoDeAMI=SITELINK_BKP
  • When finding AMIs obsolete, do a search by filtering through the tipoDeAMI tag, like this: ec2 describe-images --filter "tag-tipoDeAMI=SITELINK_BKP"
  • Process the returned AMIs and see which ones have the tag timestamp less than the minimum value you want to keep. For these timestamps I would use something 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 Boto (official) SDK. I would not risk trying to show how to do what I listed above using bash or powershell.

        
    30.12.2015 / 21:50