Skip to content

Instantly share code, notes, and snippets.

@vatshat
Created January 25, 2019 08:49
Show Gist options
  • Save vatshat/4013b49a5ef47416136fc25ebb7d649b to your computer and use it in GitHub Desktop.
Save vatshat/4013b49a5ef47416136fc25ebb7d649b to your computer and use it in GitHub Desktop.
Update multiple Cloudwatch alarms with a new SNS topic
#!/usr/bin/env bash
install_error="Please install jq before launching the script - https://stedolan.github.io/jq/download/"
type jq >/dev/null 2>&1 || { echo >&2 $install_error; exit 1; }
hash jq 2>/dev/null || { echo >&2 $install_error; exit 1; }
if [[ ! $# -eq 2 ]]; then
echo "Please provide CloudWatch alarm and 1 valid SNS topic"
exit 1
fi
if [[ ! $2 =~ (arn:aws:sns:([a-z0-9-]+):([0-9]{9,13}):([a-zA-Z0-9-]+)) ]]; then
echo "Please add valid arn of SNS topic as the 2nd parameter"
exit 1
: << 'COMMENT'
validing regex using JQ
echo $2 | jq -r ' .[] | test("(?<sns_arn>arn:aws:sns:(?<region>[a-z0-9-]+):(?<account_id>[0-9]{9,13}):(?<topic_name>[a-zA-Z0-9-]+))")'
Note: Bash doesn't support named groups && JQ doesn't support \w, \d or escaping of characters
COMMENT
fi
list_topic=$(aws sns list-topics --query "Topics[].TopicArn" | jq -r --arg arn $2 '. | index($arn)')
if [[ $list_topic == "null" ]]; then
echo "The SNS topic ARN provided doesn't exist please provide correct SNS Topic ARN"
exit 1
fi
describe_alarms=$(aws cloudwatch describe-alarms --alarm-names $1 --query "MetricAlarms[0]")
if [[ $describe_alarms == "null" ]]; then
echo "The CloudWatch Alarm name provided doesn't exist, please provide correct CloudWatch Alarm name"
exit 1
fi
alarm_name=$(echo $describe_alarms | jq -r '.AlarmName')
eval_period=$(echo $describe_alarms | jq -r '.EvaluationPeriods')
threshold=$(echo $describe_alarms | jq -r '.Threshold')
comparison=$(echo $describe_alarms | jq -r '.ComparisonOperator')
metric_name=$(echo $describe_alarms | jq -r '.MetricName')
description=$(echo $describe_alarms | jq -r '.AlarmDescription')
statistic=$(echo $describe_alarms | jq -r '.Statistic')
namespace=$(echo $describe_alarms | jq -r '.Namespace')
period=$(echo $describe_alarms | jq -r '.Period')
dimensions=$(echo $describe_alarms | jq -r '[.Dimensions[] | "Name=\(.Name),Value=\(.Value)"] | join(" ")')
alarm_actions=$(echo $describe_alarms | jq -r --arg arn $2 '.AlarmActions | . + [$arn] | join(" ")')
aws cloudwatch put-metric-alarm \
--namespace $namespace \
--alarm-name $alarm_name \
--alarm-description $description \
--evaluation-periods $eval_period \
--alarm-actions $alarm_actions \
--dimensions "$dimensions" \
--threshold $eval_period \
--actions-enabled \
--period $period \
--statistic $statistic \
--comparison-operator $comparison \
--metric-name $metric_name &&
printf "Alarm \"$alarm_name\" has been created\n\n" &&
aws cloudwatch describe-alarms --alarm-names $alarm_name
: << 'COMMENT'
echo $eval_period $alarm_name $threshold $comparison $metric_name $statistic $namespace $period $description
COMMENT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment