Skip to content

Commit

Permalink
module cloudwatch_metric_alarm support DatapointsToAlarm config
Browse files Browse the repository at this point in the history
  • Loading branch information
owan committed Jul 29, 2024
1 parent 3a0dc86 commit d6d1037
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- cloudwatch_metric_alarm - support DatapointsToAlarm config (https://github.com/ansible-collections/amazon.aws/pull/2196).
9 changes: 9 additions & 0 deletions plugins/modules/cloudwatch_metric_alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@
type: str
choices: ['ignore', 'evaluate']
version_added: 9.0.0
datapoints_to_alarm:
description:
- The number of data points that must be breaching to trigger the alarm.
required: false
type: int
version_added: 8.2.0
evaluation_periods:
description:
- The number of times in which the metric is evaluated before final calculation.
Expand Down Expand Up @@ -510,6 +516,7 @@ def create_metric_alarm(connection, module, params):
dimensions=result.get("Dimensions"),
extended_statistic=result.get("ExtendedStatistic"),
evaluate_low_sample_count_percentile=result.get("EvaluateLowSampleCountPercentile"),
datapoints_to_alarm=result.get("DatapointsToAlarm"),
evaluation_periods=result.get("EvaluationPeriods"),
insufficient_data_actions=result.get("InsufficientDataActions"),
last_updated=result.get("AlarmConfigurationUpdatedTimestamp"),
Expand Down Expand Up @@ -592,6 +599,7 @@ def main():
],
),
evaluate_low_sample_count_percentile=dict(type="str", choices=["ignore", "evaluate"]),
datapoints_to_alarm=dict(type="int"),
evaluation_periods=dict(type="int"),
extended_statistic=dict(type="str"),
description=dict(type="str"),
Expand Down Expand Up @@ -634,6 +642,7 @@ def main():
params["Threshold"] = module.params.get("threshold")
params["Period"] = module.params.get("period")
params["EvaluateLowSampleCountPercentile"] = module.params.get("evaluate_low_sample_count_percentile")
params["DatapointsToAlarm"] = module.params.get("datapoints_to_alarm")
params["EvaluationPeriods"] = module.params.get("evaluation_periods")
if module.params.get("unit"):
params["Unit"] = module.params.get("unit")
Expand Down
41 changes: 28 additions & 13 deletions tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
comparison: LessThanOrEqualToThreshold
threshold: 5.0
period: 300
datapoints_to_alarm: 2
evaluation_periods: 3
unit: Percent
description: This will alarm when an instance's cpu usage average is lower than 5% for 15 minutes
description: This will alarm when an instance's cpu usage average is lower than 5% for 10 of 15 minutes
check_mode: true
register: ec2_instance_metric_alarm_check

Expand Down Expand Up @@ -80,9 +81,10 @@
comparison: LessThanOrEqualToThreshold
threshold: 5.0
period: 300
datapoints_to_alarm: 2
evaluation_periods: 3
unit: Percent
description: This will alarm when an instance's cpu usage average is lower than 5% for 15 minutes
description: This will alarm when an instance's cpu usage average is lower than 5% for 10 of 15 minutes
register: ec2_instance_metric_alarm

- name: get info on alarms
Expand All @@ -104,6 +106,7 @@
- ec2_instance_metric_alarm.threshold == alarm_info.metric_alarms[0].threshold
- ec2_instance_metric_alarm.period == alarm_info.metric_alarms[0].period
- ec2_instance_metric_alarm.unit == alarm_info.metric_alarms[0].unit
- ec2_instance_metric_alarm.datapoints_to_alarm == alarm_info.metric_alarms[0].datapoints_to_alarm
- ec2_instance_metric_alarm.evaluation_periods == alarm_info.metric_alarms[0].evaluation_periods
- ec2_instance_metric_alarm.description == alarm_info.metric_alarms[0].alarm_description
- ec2_instance_metric_alarm.treat_missing_data == alarm_info.metric_alarms[0].treat_missing_data
Expand All @@ -121,9 +124,10 @@
comparison: LessThanOrEqualToThreshold
threshold: 5.0
period: 300
datapoints_to_alarm: 2
evaluation_periods: 3
unit: Percent
description: This will alarm when an instance's cpu usage average is lower than 5% for 15 minutes
description: This will alarm when an instance's cpu usage average is lower than 5% for 10 of 15 minutes
check_mode: true
register: ec2_instance_metric_alarm_idempotent_check

Expand Down Expand Up @@ -152,6 +156,7 @@
- threshold
- period
- unit
- datapoints_to_alarm
- evaluation_periods
- alarm_description
- treat_missing_data
Expand All @@ -169,26 +174,27 @@
comparison: LessThanOrEqualToThreshold
threshold: 5.0
period: 300
datapoints_to_alarm: 2
evaluation_periods: 3
unit: Percent
description: This will alarm when an instance's cpu usage average is lower than 5% for 15 minutes
description: This will alarm when an instance's cpu usage average is lower than 5% for 10 of 15 minutes
register: ec2_instance_metric_alarm_idempotent

- name: get info on alarms
amazon.aws.cloudwatch_metric_alarm_info:
alarm_names:
- "{{ alarm_full_name }}"
register: alarm_info_idempotent_check
register: alarm_info_idempotent

- name: Verify alarm does not register as changed after update in check mode
- name: Verify alarm does not register as changed after update in idempotent
ansible.builtin.assert:
that:
- not ec2_instance_metric_alarm_idempotent_check.changed
- not ec2_instance_metric_alarm_idempotent.changed

- name: Verify alarm did not change after updating in check mode
- name: Verify alarm did not change after updating in idempotent
ansible.builtin.assert:
that:
- alarm_info.metric_alarms[0][item] == alarm_info_idempotent_check.metric_alarms[0][item]
- alarm_info.metric_alarms[0][item] == alarm_info_idempotent.metric_alarms[0][item]
with_items:
- alarm_arn
- statistic
Expand All @@ -199,6 +205,7 @@
- threshold
- period
- unit
- datapoints_to_alarm
- evaluation_periods
- alarm_description
- treat_missing_data
Expand All @@ -215,10 +222,11 @@
comparison: GreaterThanOrEqualToThreshold
threshold: 5.0
period: 60
datapoints_to_alarm: 2
evaluation_periods: 3
evaluate_low_sample_count_percentile: "evaluate"
unit: Percent
description: This will alarm when an instance's cpu usage percentage is greater than or equal to 50% for 3 minutes
description: This will alarm when an instance's cpu usage percentage is greater than or equal to 50% for 2 of 3 minutes
check_mode: true
register: ec2_instance_metric_alarm_update_check

Expand All @@ -241,6 +249,7 @@
- ec2_instance_metric_alarm_update_check.comparison == alarm_info.metric_alarms[0].comparison_operator
- ec2_instance_metric_alarm_update_check.threshold == alarm_info.metric_alarms[0].threshold
- ec2_instance_metric_alarm_update_check.unit == alarm_info.metric_alarms[0].unit
- ec2_instance_metric_alarm_update_check.datapoints_to_alarm == alarm_info.metric_alarms[0].datapoints_to_alarm
- ec2_instance_metric_alarm_update_check.evaluation_periods == alarm_info.metric_alarms[0].evaluation_periods
- ec2_instance_metric_alarm_update_check.treat_missing_data == alarm_info.metric_alarms[0].treat_missing_data

Expand All @@ -256,6 +265,7 @@
comparison: GreaterThanOrEqualToThreshold
threshold: 5.0
period: 60
datapoints_to_alarm: 2
evaluation_periods: 3
evaluate_low_sample_count_percentile: "evaluate"
unit: Percent
Expand All @@ -281,6 +291,7 @@
- ec2_instance_metric_alarm_update.comparison == 'GreaterThanOrEqualToThreshold'
- ec2_instance_metric_alarm_update.threshold == alarm_info.metric_alarms[0].threshold
- ec2_instance_metric_alarm_update.unit == alarm_info.metric_alarms[0].unit
- ec2_instance_metric_alarm_update.datapoints_to_alarm == alarm_info.metric_alarms[0].datapoints_to_alarm
- ec2_instance_metric_alarm_update.evaluation_periods == alarm_info.metric_alarms[0].evaluation_periods
- ec2_instance_metric_alarm_update.treat_missing_data == alarm_info.metric_alarms[0].treat_missing_data

Expand Down Expand Up @@ -342,8 +353,9 @@
comparison: LessThanOrEqualToThreshold
threshold: 5.0
period: 300
datapoints_to_alarm: 2
evaluation_periods: 3
description: This will alarm when an instance's cpu usage average is lower than 5% for 15 minutes
description: This will alarm when an instance's cpu usage average is lower than 5% for 10 of 15 minutes
register: ec2_instance_metric_alarm_no_unit

- name: get info on alarms
Expand All @@ -365,6 +377,7 @@
- ec2_instance_metric_alarm_no_unit.threshold == alarm_info_no_unit.metric_alarms[0].threshold
- ec2_instance_metric_alarm_no_unit.period == alarm_info_no_unit.metric_alarms[0].period
- alarm_info_no_unit.metric_alarms[0].Unit is not defined
- ec2_instance_metric_alarm_no_unit.datapoints_to_alarm == alarm_info_no_unit.metric_alarms[0].datapoints_to_alarm
- ec2_instance_metric_alarm_no_unit.evaluation_periods == alarm_info_no_unit.metric_alarms[0].evaluation_periods
- ec2_instance_metric_alarm_no_unit.description == alarm_info_no_unit.metric_alarms[0].alarm_description
- ec2_instance_metric_alarm_no_unit.treat_missing_data == alarm_info_no_unit.metric_alarms[0].treat_missing_data
Expand Down Expand Up @@ -398,8 +411,9 @@
treat_missing_data: missing
comparison: LessThanOrEqualToThreshold
threshold: 5.0
datapoints_to_alarm: 2
evaluation_periods: 3
description: This will alarm when an instance's cpu usage average is lower than 5% for 15 minutes
description: This will alarm when an instance's cpu usage average is lower than 5% for 10 of 15 minutes
metrics:
- id: cpu
metric_stat:
Expand Down Expand Up @@ -471,8 +485,9 @@
comparison: LessThanOrEqualToThreshold
threshold: 5.0
period: 300
datapoints_to_alarm: 2
evaluation_periods: 3
description: This will alarm when an instance's cpu usage average is lower than 5% for 15 minutes
description: This will alarm when an instance's cpu usage average is lower than 5% for 10 of 15 minutes
metrics:
- id: cpu
metric_stat:
Expand Down

0 comments on commit d6d1037

Please sign in to comment.