Skip to content

Commit

Permalink
Update support for custom identifier through deployment.
Browse files Browse the repository at this point in the history
Provide error checking skip platform specific alarms when
the platform can't be identified from image.
Remove default alarm for CPUCreditBalance.
  • Loading branch information
knizami committed Oct 21, 2022
1 parent f9bd3c0 commit 2e636a8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
8 changes: 7 additions & 1 deletion CloudWatchAutoAlarms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Parameters:
Description: Enter the Amazon SNS Notification ARN for alarm notifications, leave blank to disable notifications.
Type: String
Default: ""
AlarmIdentifierPrefix:
Description: Enter the prefix that should be added to the beginning of each alarm created by the solution, (e.g. AutoAlarm-i-00e4f327736cb077f-CPUUtilization-GreaterThanThreshold-80-5m)
Type: String
Default: AutoAlarm


Conditions:
ConfigureAlarmNotifications: !Not [!Equals ["", !Ref AlarmNotificationARN]]
Expand All @@ -48,6 +53,7 @@ Resources:
ALARM_CPU_CREDIT_BALANCE_LOW_THRESHOLD: 100
ALARM_MEMORY_HIGH_THRESHOLD: 75
ALARM_DISK_PERCENT_LOW_THRESHOLD: 20
ALARM_IDENTIFIER_PREFIX: !Ref AlarmIdentifierPrefix
CLOUDWATCH_APPEND_DIMENSIONS: 'InstanceId, ImageId, InstanceType'

ALARM_LAMBDA_ERROR_THRESHOLD: 0
Expand Down Expand Up @@ -102,7 +108,7 @@ Resources:
- cloudwatch:DescribeAlarms
- cloudwatch:DeleteAlarms
- cloudwatch:PutMetricAlarm
Resource: !Sub "arn:${AWS::Partition}:cloudwatch:${AWS::Region}:${AWS::AccountId}:alarm:AutoAlarm-*"
Resource: !Sub "arn:${AWS::Partition}:cloudwatch:${AWS::Region}:${AWS::AccountId}:alarm:${AlarmIdentifierPrefix}-*"
- Effect: Allow
Action:
- cloudwatch:DescribeAlarms
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ The following list provides a description of the setting along with the environm
* You can add EC2 metric dimensions to all metrics collected by the CloudWatch agent. This environment variable aligns to your CloudWatch configuration setting for [**append_dimensions**](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html#CloudWatch-Agent-Configuration-File-Metricssection). The default setting includes all the supported dimensions: InstanceId, ImageId, InstanceType, AutoScalingGroupName
* **DEFAULT_ALARM_SNS_TOPIC_ARN**: arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:CloudWatchAutoAlarmsSNSTopic
* You can define an Amazon Simple Notification Service (Amazon SNS) topic that the Lambda function will specify as the notification target for created alarms. You provide the Amazon SNS Topic Amazon Resource Name (ARN) with the **AlarmNotificationARN** parameter when you deploy the CloudWatchAutoAlarms.yaml CloudFormation template.  If you leave the **AlarmNotificationARN** parameter value blank, then this environment variable is not set and created alarms won't use notifications.
* You can update the thresholds for the default alarms by updating the following environment variables:
* **ALARM_IDENTIFIER_PREFIX**: AutoAlarm
* The prefix name that is added to the beginning of each CloudWatch alarm created by the solution. (e.g. For "AutoAlarm": (e.g. AutoAlarm-i-00e4f327736cb077f-CPUUtilization-GreaterThanThreshold-80-5m)) You should update this variable via the **AlarmIdentifierPrefix** in the [CloudWatchAutoAlarms.yaml](./CloudWatchAutoAlarms.yaml) CloudFormation template so that the IAM policy is updated to align with your custom name.

You can update the thresholds for the default alarms by updating the following environment variables:


**For Amazon EC2**:
* **ALARM_CPU_HIGH_THRESHOLD**: 75
Expand Down Expand Up @@ -135,6 +139,15 @@ In order to create the default alarm set for an Amazon EC2 instance or AWS Lambd

For Amazon EC2 instances, you must add this tag during instance launch or you can add this tag at any time to an instance and then stop and start the instance in order to create the default alarm set as well as any custom, instance specific alarms.

You can also manually invoke the CloudWatchAutoAlarms lambda function with the following event payload to create / update EC2 alarms without having to stop and start your EC2 instances:

```json
{
"action": "scan"
}
```
You can do this with a test execution of the CloudWatchAUtoAlarms AWS Lambda function. Open the AWS Lambda Management Console and perform a test invocation from the **Test** tab with the payload provided here.

For AWS Lambda, you can add this tag to an AWS Lambda function at any time in order to create the default alarm set as well as any custom, function specific alarms.


Expand Down
8 changes: 5 additions & 3 deletions src/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,11 @@ def process_alarm_tags(instance_id, instance_info, default_alarms, metric_dimens
if create_default_alarms_flag == 'true':
for alarm_tag in default_alarms['AWS/EC2']:
create_alarm_from_tag(instance_id, alarm_tag, instance_info, metric_dimensions_map, sns_topic_arn, alarm_separator, alarm_identifier)

for alarm_tag in default_alarms[cw_namespace][platform]:
create_alarm_from_tag(instance_id, alarm_tag, instance_info, metric_dimensions_map, sns_topic_arn, alarm_separator, alarm_identifier)
if platform:
for alarm_tag in default_alarms[cw_namespace][platform]:
create_alarm_from_tag(instance_id, alarm_tag, instance_info, metric_dimensions_map, sns_topic_arn, alarm_separator, alarm_identifier)
else:
logger.warning("Skipping platform specific alarm creation for {}, unknown platform.".format(instance_id))
else:
logger.info("Default alarm creation is turned off")

Expand Down
11 changes: 4 additions & 7 deletions src/cw_auto_alarms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from os import getenv

logger = logging.getLogger()
log_level = getenv("LOGLEVEL", "INFO")
level = logging.getLevelName(log_level)
logger.setLevel(level)

create_alarm_tag = getenv("ALARM_TAG", "Create_Auto_Alarms")

Expand All @@ -14,7 +17,6 @@
append_dimensions = [dimension.strip() for dimension in append_dimensions.split(',')]

alarm_cpu_high_default_threshold = getenv("ALARM_CPU_HIGH_THRESHOLD", "75")
alarm_credit_balance_low_default_threshold = getenv("ALARM_CPU_CREDIT_BALANCE_LOW_THRESHOLD", "100")
alarm_memory_high_default_threshold = getenv("ALARM_MEMORY_HIGH_THRESHOLD", "75")
alarm_disk_space_percent_free_threshold = getenv("ALARM_DISK_PERCENT_LOW_THRESHOLD", "20")
alarm_disk_used_percent_threshold = 100 - int(alarm_disk_space_percent_free_threshold)
Expand All @@ -27,7 +29,7 @@
sns_topic_arn = getenv("DEFAULT_ALARM_SNS_TOPIC_ARN", None)

alarm_separator = '-'
alarm_identifier = 'AutoAlarm'
alarm_identifier = getenv("ALARM_IDENTIFIER_PREFIX", 'AutoAlarm')
# For Redhat, the default device is xvda2, xfs, for Ubuntu, the default fstype is ext4,
# for Amazon Linux, the default device is xvda1, xfs
default_alarms = {
Expand All @@ -38,11 +40,6 @@
'Key': alarm_separator.join(
[alarm_identifier, 'AWS/EC2', 'CPUUtilization', 'GreaterThanThreshold', '5m', 'Average', 'default1']),
'Value': alarm_cpu_high_default_threshold
},
{
'Key': alarm_separator.join(
[alarm_identifier, 'AWS/EC2', 'CPUCreditBalance', 'LessThanThreshold', '5m', 'Average', 'default1']),
'Value': alarm_credit_balance_low_default_threshold
}
],
'AWS/Lambda': [
Expand Down

0 comments on commit 2e636a8

Please sign in to comment.