Alarms Deletion at server termination
Whenever Autoscaling server terminates it leaves its alarms which later goes into insufficient state. This blog will guide you how to delete all those alarms of autoscaling server when it terminates. In order to do so, I’ve written a small shell script, I’m assuming that you already have aws cli installed if not you can click here to follow amazon documentation to install it
[js]#!/bin/bash ########### she bang
INST_ID=`ec2metadata –instance-id` ############## to store instance id of a autoscaling server in a variable INST_ID
alarm_name=`aws cloudwatch describe-alarms –query ‘MetricAlarms[*].[Dimensions[0].Value,AlarmName]’ |awk ‘/’"$INST_ID"’/ {print $2}’` ### to store alarm names that are attached to the server
for i in `alarm_name`
do
aws cloudwatch delete-alarms –alarm-names $i ##### to delete alarms
done
[/js]
Now move this script to /etc/init.d/ and create a symbolic link to /etc/rc0.d/. The symlink name should begin with a “S” or “K”.
Files that begin with K are run to terminate (kill) a system service. Files that begin with S are run to start a system service.
If you analyze scripts in /etc/rcX.d/ directory, they starts with “S” or “K” followed by a number. These numbers decide their priority in which these scripts will run, for example, S23 runs before S34.
I’ve created a script in /etc/init.d/delete_alarms
and then made its symbolic link in /etc/rc0.d/
ln -s /etc/init.d/delete_alarms /etc/rc0.d/K00delete_alarms
Please pick you priority number carefully as wrong priority number might crash your system and it wont boot again. My script name has started with K00 as autoscaling server has only one life, so I don’t have to be worried about starting that again
What happens in the case my server is shutdown with init 0 and then started. In that case wouldn’t it delete my alarms