Quantcast
Channel: linuxadmin: Expanding Linux SysAdmin knowledge
Viewing all articles
Browse latest Browse all 17904

BASH script to clean up AWS snapshots/AMIs, hitting some issues...

$
0
0

so bash scripting has never been my strong suit, and although i can usually fumble through basic stuff, i've hit an issue that i may be making more complex than I need to.

on AWS, you can make images of your instances (these are called AMI) and each AMI has a number of snapshots associated with it (one per EBS volume.) you can interact with AWS using CLI tools, but don't have access to things like mtime, atime, etc.

i have scripts that create AMIs at regular intervals and need to write one to delete AMIs that are older than 30 days. normally this would be pretty simple, as I could use a command to gather information on the AMIs, use awk and cut to strip out the info that i need and a for loop to delete the AMIs, but some things about the AWS environment are screwing me up.

i have a command i can run to generate a text file with a list of ami-id and if i then remove the most recent ones from that list, and feed that file to my script, it does the cleanup i need. i want to automate that part as well, and create the list to also include the name of the AMI, which contains a timestamp.

here's the issue: the list i create is pared down to 2 columns, the first has the ami-id and the second the ami-name. i need to run a for loop using both of those variables. i know how to do that for a single variable:

for VARIABLE in `command to generate list`; do command-you-want-to-apply-to $VARIABLE done 

is there a way to do this with 2 variables, or do i have to create a variable with each string of 2 values, then awk/cut that into 2 variables with 2 for loops within a for loop? essentially this:

for VARIABLE1 VARIABLE2 in `command to generate list`; do if [ "$VARIABLE2" < "$THIRTYDAYS" ]; then command-you-want-to-apply-to $VARIABLE1 fi done 

i'm asking this from a throwaway because i am embarrassed that i don't know this already. also is this the sort of thing that python is way better for than, say, bash? i really need to get onto learning python.

submitted by ishould_know_this
[link][12 comments]

Viewing all articles
Browse latest Browse all 17904

Trending Articles