Forked from brauliobo/delete_unused_launch_configurations.sh
Last active
May 15, 2018 20:41
-
-
Save StevenACoffman/7ca30bb6cafb61ae2f327e687fbce375 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
#!/bin/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 | |
} | |
# Get all launch configuration names that have been created for this AWS account | |
allconfigs="$(aws autoscaling describe-launch-configurations --query 'LaunchConfigurations[].LaunchConfigurationName' --output text )" | |
IFS=$'\t' read -r -a configs <<< "$allconfigs" | |
# Get all active launch configurations names that are currently associated with running instances | |
allinstances="$(aws autoscaling describe-auto-scaling-instances --query 'AutoScalingInstances[].LaunchConfigurationName' --output text )" | |
IFS=$'\t' read -r -a instances <<< "$allinstances" | |
# Get all active launch configuration names that are currently associated with launch configuration groups | |
allgroups="$(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[].LaunchConfigurationName' --output text )" | |
IFS=$'\t' read -r -a groups <<< "$allgroups" | |
# merge group configs and active instances configs into one array. We need to keep them, and remove the rest | |
IFS=$'\t' read -r -a groupsandinstances <<< "$(for R in "${instances[@]}" "${groups[@]}" ; do echo "$R" ; done | sort -du | tr '\n' '\t')" | |
#Loop through all configs and check against active ones to determine whether they need to be deleted | |
for i in "${configs[@]}" | |
do | |
if in_array $i "${groupsandinstances[@]}" ; 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