Skip to content

Instantly share code, notes, and snippets.

@hartfordfive
Last active December 22, 2015 09:42
Show Gist options
  • Select an option

  • Save hartfordfive/6cdf671204d92d4032cd to your computer and use it in GitHub Desktop.

Select an option

Save hartfordfive/6cdf671204d92d4032cd to your computer and use it in GitHub Desktop.
Script to automatically cleanup EC2 instances created by Test Kitchen
#!/bin/bash
<<COMMENT
Dependencies:
1. This script assumes that Test Kitchen provisioned instances have name prefixes begining with "kitchen".
2. You must have jq installed
3. You must have the python aws-cli tool installed
4. You must have the appropriate IAM role assigned to the machine that will be running this script if you dont want to provide the access key and secret key
Sample Policy Document:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ec2:DescribeInstances",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ec2:TerminateInstances"
],
"Resource": [
"arn:aws:ec2:[REGION]:[ACCOUNT_NUMBER]:instance/*"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:DeleteVolume"
],
"Resource": "arn:aws:ec2:[REGION]:[ACCOUNT_NUMBER]:volume/*"
}
]
}
COMMENT
datediff() {
d1=$(date +%s) # Now
d2=$(date -d "$1" +%s) # Instance create time
DAYS_OLD=$(( (d1 - d2) / 86400 ))
}
REGION=us-east-1
INSTANCES=`aws --output json --region $REGION ec2 describe-instances --filter "Name=instance-state-name,Values=running" "Name=tag-key,Values=Name" "Name=tag-value,Values=kitchen*"`
if [ "$INSTANCES" == "" ]; then
TOTAL_INSTANCES=0
else
TOTAL_INSTANCES=$(echo $INSTANCES | jq '.Reservations | length')
fi
IFS='
'
DAYS_THRESHOLD=0
if [ $TOTAL_INSTANCES -eq 0 ]; then
echo "[$(date)] No kitchen instances to cleanup!"
exit 0
fi
for (( I=0; I<$TOTAL_INSTANCES; I++ ))
do
DATE_CREATED=$(echo $INSTANCES | jq ".Reservations[$I].Instances[0].LaunchTime" | sed 's/"//g')
datediff $DATE_CREATED
dt=$(date)
if [ $DAYS_OLD -lt $DAYS_THRESHOLD ]; then
continue
else
INSTANCE_ID=$(echo $INSTANCES | jq ".Reservations[$I].Instances[0].InstanceId" | sed 's/"//g')
INSTANCE_NAME=$(echo $INSTANCES | jq ".Reservations[$I].Instances[0].Tags[] | select(.Key == \"Name\") | .Value" | sed 's/"//g')
VOLUMES=$(echo $INSTANCES | jq ".Reservations[$I].Instances[0].BlockDeviceMappings[].Ebs.VolumeId" | sed 's/"//g')
echo "[$(date)] Deleting $DAYS_OLD day old instance ($INSTANCE_NAME - $INSTANCE_ID)"
# 1 Delete instance
RES_INSTANCE=`aws --output json --region $REGION ec2 terminate-instances --instance-ids $INSTANCE_ID`
MIN_RES=$(echo $RES_INSTANCE | jq -c '.')
echo "[$(date)] JSON_RESP:$MIN_RES"
# 2 Delete attached volumes
for V in $VOLUMES
do
echo "[$(date)] Deleting attached volume $V"
RES_VOLUME=`aws --output json --region $REGION ec2 delete-volume --volume-id $V`
MIN_RES=$(echo $RES_VOLUME | jq -c '.')
echo "[$(date)] JSON_RESP:$MIN_RES"
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment