Skip to content

Instantly share code, notes, and snippets.

@neal6940
Forked from wys1203/aws_cloudwatch_alarm_rename.py
Last active December 1, 2020 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neal6940/7d93826c6fc5b681d564f7864fd0d5a8 to your computer and use it in GitHub Desktop.
Save neal6940/7d93826c6fc5b681d564f7864fd0d5a8 to your computer and use it in GitHub Desktop.
AWS CloudWatch alarm rename script
# 2020-11-30
#
# code from gist fork:
#
# update alarm names from 3 cli args:
# - old alarm name
# - new alarm name
# - new alarm threshold
#
import sys
import boto3
def rename_alarm(alarm_name, new_alarm_name, new_alarm_threshold = ''):
client = boto3.client('cloudwatch')
alarm = client.describe_alarms(AlarmNames=[alarm_name])
if not alarm:
raise Exception("Alarm '%s' not found" % alarm_name)
alarm = alarm['MetricAlarms'][0]
if new_alarm_threshold = '':
new_alarm_threshold = alarm['Threshold']
client.put_metric_alarm(
AlarmName=new_alarm_name,
ActionsEnabled=alarm['ActionsEnabled'],
OKActions=alarm['OKActions'],
AlarmActions=alarm['AlarmActions'],
InsufficientDataActions=alarm['InsufficientDataActions'],
MetricName=alarm['MetricName'],
Namespace=alarm['Namespace'],
Statistic=alarm['Statistic'],
Dimensions=alarm['Dimensions'],
Period=alarm['Period'],
EvaluationPeriods=alarm['EvaluationPeriods'],
Threshold=alarm['Threshold'],
ComparisonOperator=alarm['ComparisonOperator']
)
# update actually creates a new alarm because the name has changed, so
# we have to manually delete the old one
client.delete_alarms(AlarmNames=[alarm_name])
if __name__ == '__main__':
alarm_name, new_alarm_name, new_alarm_threshold = sys.argv[1:3]
rename_alarm(alarm_name, new_alarm_name, new_alarm_threshold)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment