Class: RenderCFN::AutoScalingGroup

Inherits:
AwsObject
  • Object
show all
Defined in:
lib/renderCFN/autoScalingGroup.rb

Instance Method Summary collapse

Methods inherited from AwsObject

#get, #name

Constructor Details

#initialize(arguments) ⇒ AutoScalingGroup

Returns a new instance of AutoScalingGroup.



5
6
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/renderCFN/autoScalingGroup.rb', line 5

def initialize( arguments)
  @name = "#{arguments[:name]}AutoScalingGroup"
  @awsObject = { 
    @name => {
      'Type' => 'AWS::AutoScaling::AutoScalingGroup',
      'Properties' => {
        'TerminationPolicies' => [ 'OldestInstance' ],
        'HealthCheckType' => arguments[:healthCheckEmergencyOverride] || arguments[:healthCheck],
        'HealthCheckGracePeriod' => arguments[:healthCheckGracePeriod],
        'LaunchConfigurationName' => { 'Ref' => "#{arguments[:name]}LaunchConfiguration" },
        'VPCZoneIdentifier' => Array.new, 
        'MinSize' => arguments[:minSize],
        'MaxSize' => arguments[:maxSize],
        'Cooldown' => 300,
        'Tags' => [
          { 'Key' => 'Name', 'Value' => "#{arguments[:serviceTitle]}", 'PropagateAtLaunch' => true },
          { 'Key' => 'stack', 'Value' => "#{@@environmentType}", 'PropagateAtLaunch' => true },
          { 'Key' => 'application', 'Value' => "#{arguments[:serviceTitle]}", 'PropagateAtLaunch' => true },
          { 'Key' => 'StackName', 'Value' => "#{@@stackName}", 'PropagateAtLaunch' => true },
          { 'Key' => 'PMData', 'Value' => "#{arguments[:pmData]}", 'PropagateAtLaunch' => true }
        ]
      },
      'DependsOn' => []
    }
  }

    #I do not know why I have to do it this way... shrug
    arguments[:subnetList].each do |subnet|
      @awsObject[@name]['Properties']['VPCZoneIdentifier'].push( subnet)
    end

    unless arguments[:noelb] then
      @awsObject[@name]['Properties']['LoadBalancerNames'] = Array.new
      @awsObject[@name]['Properties']['LoadBalancerNames'].push( 'Ref' => "#{arguments[:name]}ElasticLoadBalancer" )
      @awsObject[@name]['DependsOn'].push( "#{arguments[:name]}ElasticLoadBalancer")
    end

    @awsObject[@name]['DependsOn'].push( "#{arguments[:name]}LaunchConfiguration")

    if arguments[:autoScalingRollingUpdateEnabled]
      @awsObject[@name]['UpdatePolicy'] = {
        'AutoScalingRollingUpdate' => { 
          'MinInstancesInService' => arguments[:minInstancesInService],
          'MaxBatchSize' => arguments[:maxBatchSize],
          'PauseTime' => "PT#{90 + arguments[:healthCheckGracePeriod]}S"
        }
      }
    end
end

Instance Method Details

#addCpuScaling(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/renderCFN/autoScalingGroup.rb', line 55

def addCpuScaling( args)

    cpuMetrics = {'ExtendedStatistic' => args[:cpuPercentile] || 'p90'}

    if args[:cpuMetric] == 'Average' then
      cpuMetrics = {'Statistic' => 'Average'}
    end

    stuff = {
      "#{@name}ScaleUP" => {
        'Type' => 'AWS::AutoScaling::ScalingPolicy',
        'Properties' => {
          'AutoScalingGroupName' => { 
            'Ref' => @name,
          },
          'AdjustmentType' => 'ChangeInCapacity',
          'ScalingAdjustment' => args[:changeUP]
        }
      },
      "#{@name}ScaleDOWN" => {
        'Type' => 'AWS::AutoScaling::ScalingPolicy',
        'Properties' => {
          'AutoScalingGroupName' => { 
            'Ref' => @name,
          },
          'AdjustmentType' => 'ChangeInCapacity',
          'ScalingAdjustment' => args[:changeDOWN]
        }
      },
      "#{@name}AlarmUP" => {
        'Type' => 'AWS::CloudWatch::Alarm',
        'Properties' => {
          'ComparisonOperator' => 'GreaterThanOrEqualToThreshold',
          'EvaluationPeriods' => 2,
          'MetricName' => 'CPUUtilization',
          'Namespace' => 'AWS/EC2',
          'Period' => 300,
          'Threshold' => args[:cpuUP],
          'AlarmActions' => [
            'Ref' => "#{@name}ScaleUP"
          ],
          "Dimensions" =>  [{
            "Name" => "AutoScalingGroupName",
            "Value" => {
              "Ref" =>  @name
            }
          }]
          }.merge!(cpuMetrics)
        },
      "#{@name}AlarmDOWN" => {
        'Type' => 'AWS::CloudWatch::Alarm',
        'Properties' => {
          'ComparisonOperator' => 'LessThanOrEqualToThreshold',
          'EvaluationPeriods' => 8,
          'MetricName' => 'CPUUtilization',
          'Namespace' => 'AWS/EC2',
          'Period' => 300,
          'Threshold' => args[:cpuDOWN],
          'AlarmActions' => [
            'Ref' => "#{@name}ScaleDOWN"
          ],
          "Dimensions" =>  [{
            "Name" => "AutoScalingGroupName",
            "Value" => {
              "Ref" =>  @name
            }
          }]
          }.merge!(cpuMetrics)
        }
      }
  @awsObject.merge!( stuff)
end