Automate Disabling And Re-enabling AWS Cloudwatch Alarms During Maintenance Window
Play this article
In this blog we are going to create a python script in which will disable all active cloudwatch alarms before maintenance/deployment window and enable it back again after maintenance/deployment window.
Boto3 is the name of the Python SDK for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts.
Disable All Active CloudWatch Alarms
- Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
import boto3
- We will invoke the client for Cloudwatch
client = boto3.client('cloudwatch')
- Now we will invoke "describe_alarms()" to get information of all cloudwtach alarms. Checkout the official documentation here
response = client.describe_alarms()
- Now we will traverse through the response and check alarms which are enabled.
for alarm in response['MetricAlarms']: status = alarm['ActionsEnabled']
- Let's attach If..Else statement to this for loop. Using If..Else we will check first if the alarm is in enabled state. If yes we will proceed with next steps else program will exit
for alarm in response['MetricAlarms']: status = alarm['ActionsEnabled'] if status: else:
- If the alarm is in enable state we will first fetch alarm name. Using this name we will invoke "disable_alarm_actions" which will disable the alarm and will print the alarm name that we disabled. Checkout the official documentation here
for alarm in response['MetricAlarms']: status = alarm['ActionsEnabled'] if status: name = alarm['AlarmName'] disable_alarm = client.disable_alarm_actions(AlarmNames=[name]) print("Alarm {} is disabled".format(name)) else: print("No enabled Alarms")
- Once we will disable the alarm we will have to make sure to copy alarm name to text file so these alarm names can be used in our next script to enable these alarms again.
To view entire github code please click herefor alarm in response['MetricAlarms']: status = alarm['ActionsEnabled'] if status: name = alarm['AlarmName'] print(name) disable_alarm = client.disable_alarm_actions(AlarmNames=[name]) print("Alarm {} is disabled".format(name)) file1 = open("myfile.txt", "a") file1.write(name+"\n") else: print("No enabled Alarms")
Enable All CloudWatch Alarms Which Were Disabled
- Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
import boto3
- We will invoke the client for Cloudwatch
client = boto3.client('cloudwatch')
- We will now open our file where disabled alarm names is stored and store it in list.
file1 = open("myfile.txt", "r") enable_alarm = file1.readlines()
- file1.readlines() copies all file contents with whitespace characters (newlines and spaces) from the end of each line. To remove these whitespace characters we will invoke "line.rstrip()" which will read the entire file into memory and remove all whitespace characters (newlines and spaces) from the end of each line. Checkout the official documentation here
file1 = open("myfile.txt", "r") enable_alarm = file1.readlines() enable_alarm = [line.rstrip() for line in enable_alarm]
- At the end we will pass this list as agrument to "enable_alarm_actions()" and re-enable the alarms that we have disabled above. Checkout the official documentation here
To view entire github code please click hereresponse = client.enable_alarm_actions(AlarmNames=enable_alarm)
Conclusion
By using Boto3 provided inbuild methods for AWS CloudWatch resources many task can be automated. Current code snippet can be used to automate disabling and re-enabling AWS Cloudwatch alarms during maintenance windows
Stay tuned for my next blog.....
So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks.
ย