I'm in the process of writing a Jenkins Pipeline job that will backup Jenkins to an AWS AMI, and then update plugins. I can create the AMI no problem, but ideally I'd like to store a capped number of these, deleting the oldest to keep, say, three at any one time.
This is a two-pronged question;
Is there a way to capture and sort through AMIs via Groovy? (or shell script which can then be integrated into Groovy.) We have dozens of AMIs stored so I'd need to be able to sift through for just those named a certain way, then sort them by date and then version number. I'm imagining a naming system along the lines of yyyymmdd_jenkinsbkup_##
where the ## is a version number (01, 02, etc)
Assuming yes, can I delete the oldest one and iterate to the next version number when creating the next AMI? Say I have the following three saved
20161201_jenkinsbkup_08
20161204_jenkinsbkup_09
20161212_jenkinsbkup_10
can I then somehow delete 20161201_jenkinsbkup_08
and create 20161215_jenkinsbkup_11
I am an AWS noob but got this far in my script to create an image. Note this is run inside a larger pipeline script.
sh "aws ec2 create-image --instance-id i-############# --name 'something to create the requested format' --region us-east-1 --no-reboot"
You can sort AMIs by providing filters to ec2:describeImages.
In your example, you'd probably filter by Name, then sorts them by date ( I don't put it in the name, because createdDate is a property already), create a snapshot of the newest, and then delete both the ami and the snapshot of the oldest. You might want to tag the snapshot with the ami id before you delete it so you can clean it up if you're interrupted before you manage to delete it, as the snapshots can't be deleted if the ami exists, and won't show the relationship to the AMI if its gone.
I don't know how to do it in groovy from jenkins, but there must be some way of executing ec2 commands, perhaps a java sdk if not a groovy. Or if you wanted you could implement it in lambda and just call out to the lambda, if that's better. That would be attractive if you wanted snapshots at other times or for other systems in addition to the pre-update snapshot.