Class: LambdaLoadout::AlarmConfig

Inherits:
Object
  • Object
show all
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.)

Examples:

Generate alarm configuration

alarm = LambdaLoadout::AlarmConfig.new(
  metric_name: "LambdaError",
  namespace: "MyApp",
  threshold: 1,
  evaluation_periods: 1
)

puts alarm.to_cloudformation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric_name:, namespace:, threshold: 1, statistic: 'Sum', evaluation_periods: 1, period: 60, comparison_operator: 'GreaterThanOrEqualToThreshold') ⇒ AlarmConfig

Initialize alarm configuration

Parameters:

  • metric_name (String)

    CloudWatch metric name

  • namespace (String)

    CloudWatch metric namespace

  • threshold (Numeric) (defaults to: 1)

    Alarm threshold

  • statistic (String) (defaults to: 'Sum')

    Statistic type (Sum, Average, Maximum, etc.)

  • evaluation_periods (Integer) (defaults to: 1)

    Number of periods to evaluate

  • period (Integer) (defaults to: 60)

    Period in seconds

  • comparison_operator (String) (defaults to: 'GreaterThanOrEqualToThreshold')

    Comparison operator



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_operatorObject (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_periodsObject (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_nameObject (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

#namespaceObject (readonly)

Returns the value of attribute namespace.



113
114
115
# File 'lib/lambda_loadout/errors.rb', line 113

def namespace
  @namespace
end

#periodObject (readonly)

Returns the value of attribute period.



113
114
115
# File 'lib/lambda_loadout/errors.rb', line 113

def period
  @period
end

#statisticObject (readonly)

Returns the value of attribute statistic.



113
114
115
# File 'lib/lambda_loadout/errors.rb', line 113

def statistic
  @statistic
end

#thresholdObject (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

Parameters:

  • alarm_name (String) (defaults to: "#{metric_name}Alarm")

    CloudFormation logical name

  • sns_topic_arn (String) (defaults to: nil)

    SNS topic ARN for notifications (optional)

  • dimensions (Hash) (defaults to: {})

    Metric dimensions

Returns:

  • (String)

    CloudFormation YAML snippet



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

Parameters:

  • resource_name (String) (defaults to: metric_name.downcase)

    Terraform resource name

  • sns_topic_arn (String) (defaults to: nil)

    SNS topic ARN for notifications (optional)

  • dimensions (Hash) (defaults to: {})

    Metric dimensions

Returns:

  • (String)

    Terraform HCL snippet



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