Class: LambdaLoadout::AlarmConfig
- Inherits:
-
Object
- Object
- LambdaLoadout::AlarmConfig
- Defined in:
- lib/lambda_loadout/errors.rb
Overview
Alarm configuration helper
Helps define CloudWatch Alarms for metrics created via EMF. Note: This generates CloudFormation/SAM template snippets, actual alarm creation should be done via IaC (CloudFormation, Terraform, etc.)
Instance Attribute Summary collapse
-
#comparison_operator ⇒ Object
readonly
Returns the value of attribute comparison_operator.
-
#evaluation_periods ⇒ Object
readonly
Returns the value of attribute evaluation_periods.
-
#metric_name ⇒ Object
readonly
Returns the value of attribute metric_name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
-
#statistic ⇒ Object
readonly
Returns the value of attribute statistic.
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
Instance Method Summary collapse
-
#initialize(metric_name:, namespace:, threshold: 1, statistic: 'Sum', evaluation_periods: 1, period: 60, comparison_operator: 'GreaterThanOrEqualToThreshold') ⇒ AlarmConfig
constructor
Initialize alarm configuration.
-
#to_cloudformation(alarm_name: "#{metric_name}Alarm", sns_topic_arn: nil, dimensions: {}) ⇒ String
Generate CloudFormation YAML for alarm.
-
#to_terraform(resource_name: metric_name.downcase, sns_topic_arn: nil, dimensions: {}) ⇒ String
Generate Terraform HCL for alarm.
Constructor Details
#initialize(metric_name:, namespace:, threshold: 1, statistic: 'Sum', evaluation_periods: 1, period: 60, comparison_operator: 'GreaterThanOrEqualToThreshold') ⇒ AlarmConfig
Initialize alarm configuration
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/lambda_loadout/errors.rb', line 125 def initialize( metric_name:, namespace:, threshold: 1, statistic: 'Sum', evaluation_periods: 1, period: 60, comparison_operator: 'GreaterThanOrEqualToThreshold' ) @metric_name = metric_name @namespace = namespace @threshold = threshold @statistic = statistic @evaluation_periods = evaluation_periods @period = period @comparison_operator = comparison_operator end |
Instance Attribute Details
#comparison_operator ⇒ Object (readonly)
Returns the value of attribute comparison_operator.
113 114 115 |
# File 'lib/lambda_loadout/errors.rb', line 113 def comparison_operator @comparison_operator end |
#evaluation_periods ⇒ Object (readonly)
Returns the value of attribute evaluation_periods.
113 114 115 |
# File 'lib/lambda_loadout/errors.rb', line 113 def evaluation_periods @evaluation_periods end |
#metric_name ⇒ Object (readonly)
Returns the value of attribute metric_name.
113 114 115 |
# File 'lib/lambda_loadout/errors.rb', line 113 def metric_name @metric_name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
113 114 115 |
# File 'lib/lambda_loadout/errors.rb', line 113 def namespace @namespace end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
113 114 115 |
# File 'lib/lambda_loadout/errors.rb', line 113 def period @period end |
#statistic ⇒ Object (readonly)
Returns the value of attribute statistic.
113 114 115 |
# File 'lib/lambda_loadout/errors.rb', line 113 def statistic @statistic end |
#threshold ⇒ Object (readonly)
Returns the value of attribute threshold.
113 114 115 |
# File 'lib/lambda_loadout/errors.rb', line 113 def threshold @threshold end |
Instance Method Details
#to_cloudformation(alarm_name: "#{metric_name}Alarm", sns_topic_arn: nil, dimensions: {}) ⇒ String
Generate CloudFormation YAML for alarm
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/lambda_loadout/errors.rb', line 149 def to_cloudformation(alarm_name: "#{metric_name}Alarm", sns_topic_arn: nil, dimensions: {}) cf = { alarm_name => { 'Type' => 'AWS::CloudWatch::Alarm', 'Properties' => { 'AlarmName' => alarm_name, 'AlarmDescription' => "Alarm for #{metric_name} in #{namespace}", 'MetricName' => metric_name, 'Namespace' => namespace, 'Statistic' => statistic, 'Period' => period, 'EvaluationPeriods' => evaluation_periods, 'Threshold' => threshold, 'ComparisonOperator' => comparison_operator, 'TreatMissingData' => 'notBreaching' } } } # Add dimensions if provided unless dimensions.empty? dims = dimensions.map { |k, v| { 'Name' => k.to_s, 'Value' => v.to_s } } cf[alarm_name]['Properties']['Dimensions'] = dims end # Add SNS notification if provided cf[alarm_name]['Properties']['AlarmActions'] = [sns_topic_arn] if sns_topic_arn require 'yaml' YAML.dump(cf) end |
#to_terraform(resource_name: metric_name.downcase, sns_topic_arn: nil, dimensions: {}) ⇒ String
Generate Terraform HCL for alarm
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/lambda_loadout/errors.rb', line 187 def to_terraform(resource_name: metric_name.downcase, sns_topic_arn: nil, dimensions: {}) hcl = <<~HCL resource "aws_cloudwatch_metric_alarm" "#{resource_name}" { alarm_name = "#{resource_name}" alarm_description = "Alarm for #{metric_name} in #{namespace}" comparison_operator = "#{comparison_operator}" evaluation_periods = #{evaluation_periods} metric_name = "#{metric_name}" namespace = "#{namespace}" period = #{period} statistic = "#{statistic}" threshold = #{threshold} treat_missing_data = "notBreaching" HCL # Add dimensions if provided unless dimensions.empty? hcl += "\n dimensions = {\n" dimensions.each do |k, v| hcl += " #{k} = \"#{v}\"\n" end hcl += " }\n" end # Add SNS notification if provided hcl += "\n alarm_actions = [\"#{sns_topic_arn}\"]\n" if sns_topic_arn hcl += "}\n" hcl end |