Forked from brauliobo/delete_unused_launch_configurations.sh
Last active
December 1, 2017 00:37
-
-
Save tedivm/750075f8ceabc188fcfabe85cd9fa49e to your computer and use it in GitHub Desktop.
Delete unused Launch Configuration. Based on http://www.markomedia.com.au/delete-launch-config-programatically-aws/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Check if a value exists in an array | |
# @param $1 mixed Needle | |
# @param $2 array Haystack | |
# @return Success (0) if value exists, Failure (1) otherwise | |
# Usage: in_array "$needle" "${haystack[@]}" | |
# See: http://fvue.nl/wiki/Bash:_Check_if_array_element_exists | |
in_array() { | |
local hay needle=$1 | |
shift | |
for hay; do | |
[[ $hay == $needle ]] && return 0 | |
done | |
return 1 | |
} | |
IFS=$'\n' | |
# Get all launch configuration names that have been created for this AWS account | |
allconfigs=$(aws autoscaling describe-launch-configurations | jq '.LaunchConfigurations[].LaunchConfigurationName' ) | |
configs=($allconfigs) | |
# Get all active launch configurations names that are currently associated with running instances | |
allinstances=$(aws autoscaling describe-auto-scaling-instances | jq '.AutoScalingInstances[].LaunchConfigurationName' | sed s/\"//g | grep -v null) | |
instances=($allinstances) | |
# Get all active launch configuration names that are currently associated with launch configuration groups | |
allgroups=$(aws autoscaling describe-auto-scaling-groups | jq '.AutoScalingGroups[].LaunchConfigurationName' | sed s/\"//g | grep -v null) | |
groups=($allgroups) | |
# merge group configs and active instances configs into one array. We need to keep them, and remove the rest | |
groupsandinstances=(`for R in "${instances[@]}" "${groups[@]}" ; do echo "$R" ; done | sort -du`) | |
#Loop through all configs and check against active ones to determine whether they need to be deleted | |
for ((x = 0; x < ${#configs[@]}; x++)) | |
do | |
i=$(echo "${configs[$x]}" | sed s/\"//g | grep -v null) | |
if in_array $i "${groupsandinstances[@]}" && echo active ${i} | |
then | |
echo active ${i} | |
else | |
echo deleting ${i} | |
aws autoscaling delete-launch-configuration --launch-configuration-name ${i}` | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment