Class: CfnGuardian::Resource::Ec2Instance

Inherits:
Base
  • Object
show all
Defined in:
lib/cfnguardian/resources/ec2_instance.rb

Instance Method Summary collapse

Methods inherited from Base

#default_checks, #default_events, #default_metric_filters, #get_alarms, #get_checks, #get_cost, #get_event_subscriptions, #get_events, #get_metric_filters, #initialize

Methods included from Logging

colors, included, logger, #logger, logger=

Constructor Details

This class inherits a constructor from CfnGuardian::Resource::Base

Instance Method Details

#default_alarmsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cfnguardian/resources/ec2_instance.rb', line 7

def default_alarms    
  alarm = CfnGuardian::Models::Ec2InstanceAlarm.new(@resource)
  alarm.name = 'CPUUtilizationHigh'
  alarm.metric_name = 'CPUUtilization'
  alarm.statistic = 'Minimum'
  alarm.threshold = 90
  alarm.evaluation_periods = 10
  @alarms.push(alarm)
  
  alarm = CfnGuardian::Models::Ec2InstanceAlarm.new(@resource)
  alarm.name = 'StatusCheckFailed'
  alarm.metric_name = 'StatusCheckFailed'
  alarm.threshold = 0
  alarm.evaluation_periods = 1
  alarm.comparison_operator = 'GreaterThanThreshold'
  @alarms.push(alarm)

  alarm = CfnGuardian::Models::Ec2InstanceAlarm.new(@resource)
  alarm.name = 'CPUCreditBalanceLow'
  alarm.metric_name = 'CPUCreditBalance'
  alarm.comparison_operator = 'LessThanThreshold'
  alarm.statistic = 'Minimum'
  alarm.threshold = 100
  alarm.evaluation_periods = 5
  alarm.treat_missing_data = 'notBreaching'
  alarm.datapoints_to_alarm = 5
  @alarms.push(alarm)
end

#default_event_subscriptionsObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/cfnguardian/resources/ec2_instance.rb', line 36

def default_event_subscriptions()
  event_subscription = CfnGuardian::Models::Ec2InstanceEventSubscription.new(@resource)
  event_subscription.name = 'InstanceTerminated'
  event_subscription.detail_type = 'EC2 Instance State-change Notification'
  event_subscription.detail = {
    'instance-id' => [@resource['Id']],
    'state' => ['terminated']
  }
  @event_subscriptions.push(event_subscription)
end

#resource_exists?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/cfnguardian/resources/ec2_instance.rb', line 47

def resource_exists?
  client = Aws::EC2::Client.new
  resource = Aws::EC2::Resource.new(client: client)
  instance = resource.instance(@resource['Id'])
  return instance.exists?
end