Skip to content

Instantly share code, notes, and snippets.

@wys1203
Last active August 24, 2022 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wys1203/4c0a1d3351be2cd2207bb7a3abd0bf46 to your computer and use it in GitHub Desktop.
Save wys1203/4c0a1d3351be2cd2207bb7a3abd0bf46 to your computer and use it in GitHub Desktop.
AWS CloudWatch alarm rename script
import sys
import boto3
def rename_alarm(alarm_name, new_alarm_name):
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]
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 = sys.argv[1:3]
rename_alarm(alarm_name, new_alarm_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment