To delete launch config in AWS programatically is something I left out in one of my previous posts – Scaling WordPress in Amazon Cloud. In that post I explained how I create an AMI snapshot of a WordPress instance after each deployment. This ensures that I have an up to date AMI (frozen pizza model) to spin up when I need to scale horizontally. However, every time I create a new AMI I need to create a new launch configuration and update the existing auto scaling group with it. That leaves a handful of old launch configurations that can pile up unnecessarily in your AWS account. At the time of writing this post, AWS has a limit of 100 launch configurations, after which creating new ones is not possible. To prevent your account from accumulating all these unnecessary launch configs, you can write a cron job that will cleanup your configs nightly.
This particular bash script does the job for me:
Delete Launch Config Programatically
For this script to work you will need to install and configure the following tools:
#!/bin/bash export AWS_CONFIG_FILE=/root/awsconfig export PATH=$PATH:/usr/local/bin # 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 | jq '.LaunchConfigurations[].LaunchConfigurationName' | sed s/\"//g) configs=($allconfigs) # Get all active launch configurations names that are currently associated with running instances allinstances=$(aws autoscaling describe-auto-scaling-instances | jq '.LaunchConfigurations[].LaunchConfigurationName' | sed s/\"//g) instances=($allinstances) # Get all active launch configuration names that are currently associated with launch configuration groups allgroups=$(aws autoscaling describe-auto-scaling-groups | jq '.LaunchConfigurations[].LaunchConfigurationName' | sed s/\"//g) 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 i in "${configs[@]}" do #echo $i in_array $i "${groupsandinstances[@]}" && echo active ${i} || echo deleting ${i} `aws autoscaling delete-launch-configuration --launch-configuration-name ${i}` done
Now you can set up this script to run nightly at, say, 3:30am. To do that you simply create a cron job:
sudo nano /etc/cron.d/cleanup_launch_configs
# Cleanup Launch configs that are generated by deployment procedures 30 3 * * * root /path_to_your/cleanup_launch_configs.sh
As always, before you implement this in your production environment test it out thoroughly and make sure you understand what you’re doing.
Marko