Class: MinimalPipeline::Cloudformation

Inherits:
Object
  • Object
show all
Defined in:
lib/minimal_pipeline/cloudformation.rb

Overview

Here is an example of how to use this class to deploy CloudFormation Stacks.

“‘ cloudformation = MinimalPipeline::Cloudformation.new

cloudformation_parameters =

'Vpc' => 'vpc-123456',
'AsgSubnets' => %w[sg-one sg-two sg-three],
'ElbSecurityGroup' => 'sg-123456',
'CertName' => 'example'

stack_parameters =

stack_name: stack_name,
template_body: File.read('provisioning/elb.json'),
capabilities: ['CAPABILITY_IAM'],
parameters: cloudformation.params(cloudformation_parameters)

cloudformation.deploy_stack(‘EXAMPLE_ELB’, stack_parameters) name = cloudformation.stack_output(stack_name, ‘LoadBalancerName’) “‘

You will need the following environment variables to be present:

  • ‘AWS_REGION` or `region`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wait_max_attempts = 120, wait_delay = 30) ⇒ Cloudformation

Sets up ‘Aws::CloudFormation::Client` Requires environment variable `AWS_REGION` or `region` to be set.

stack create or update is complete. a stack’s status

Parameters:

  • wait_max_attempts (Fixnum) (defaults to: 120)

    Number of attempts to wait until all

  • wait_delay (Fixnum) (defaults to: 30)

    The sleep interval for checking the status of



44
45
46
47
48
49
50
51
52
# File 'lib/minimal_pipeline/cloudformation.rb', line 44

def initialize(wait_max_attempts = 120, wait_delay = 30)
  raise 'You must set env variable AWS_REGION or region.' \
    if ENV['AWS_REGION'].nil? && ENV['region'].nil?

  region = ENV['AWS_REGION'] || ENV['region']
  @client = Aws::CloudFormation::Client.new(region: region)
  @wait_max_attempts = wait_max_attempts
  @wait_delay = wait_delay
end

Instance Attribute Details

#clientObject (readonly)

Instance of ‘Aws::CloudFormation::Client`



33
34
35
# File 'lib/minimal_pipeline/cloudformation.rb', line 33

def client
  @client
end

#wait_delayObject

Returns the value of attribute wait_delay.



35
36
37
# File 'lib/minimal_pipeline/cloudformation.rb', line 35

def wait_delay
  @wait_delay
end

#wait_max_attemptsObject

Returns the value of attribute wait_max_attempts.



34
35
36
# File 'lib/minimal_pipeline/cloudformation.rb', line 34

def wait_max_attempts
  @wait_max_attempts
end

Instance Method Details

#deploy_stack(stack_name, stack_parameters) ⇒ Object

Parameters:

  • stack_name (String)

    The name of the CloudFormation stack

  • stack_parameters (Hash)

    Parameters to be passed into the stack



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/minimal_pipeline/cloudformation.rb', line 90

def deploy_stack(stack_name, stack_parameters)
  wait_options = {
    max_attempts: @wait_max_attempts,
    delay: @wait_delay
  }

  unless @client.describe_stacks(stack_name: stack_name).stacks.empty?
    puts 'Updating the existing stack' if ENV['DEBUG']
    @client.update_stack(stack_parameters)
    @client.wait_until(:stack_update_complete, { stack_name: stack_name },
                       wait_options)
  end
rescue Aws::CloudFormation::Errors::ValidationError => error
  if error.to_s.include? 'No updates are to be performed.'
    puts 'Nothing to do.' if ENV['DEBUG']
  elsif error.to_s.include? 'Template error'
    raise error
  else
    puts 'Creating a new stack' if ENV['DEBUG']
    @client.create_stack(stack_parameters)
    @client.wait_until(:stack_create_complete, { stack_name: stack_name },
                       wait_options)
  end
end

#params(parameters) ⇒ Hash

Converts a parameter Hash into a CloudFormation friendly structure

Parameters:

  • parameters (Hash)

    Key value pair of parameters for a CFN stack.

Returns:

  • (Hash)

    CloudFormation friendly data structure of parameter



58
59
60
61
62
63
64
# File 'lib/minimal_pipeline/cloudformation.rb', line 58

def params(parameters)
  parameter_list = []
  parameters.each do |k, v|
    parameter_list.push(parameter_key: k, parameter_value: v)
  end
  parameter_list
end

#stack_output(stack, output) ⇒ String

Retrieves the CloudFormation stack output of a single value

Parameters:

  • stack (String)

    The name of the CloudFormation stack

  • output (String)

    The name of the output to fetch the value of

Returns:

  • (String)

    The value of the output for the CloudFormation stack



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/minimal_pipeline/cloudformation.rb', line 71

def stack_output(stack, output)
  resp = @client.describe_stacks(stack_name: stack)

  raise "#{stack.upcase} stack does not exist!" if resp.stacks.empty?

  resp.stacks.first.outputs.each do |stack_output|
    zero_output = stack_output.output_key.casecmp(output).zero?
    return stack_output.output_value if zero_output
  end

  raise "#{stack.upcase} stack does not have a(n) '#{output}' output!"
end