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

#initializeCloudformation

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



37
38
39
40
41
42
43
# File 'lib/minimal_pipeline/cloudformation.rb', line 37

def initialize
  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)
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

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/minimal_pipeline/cloudformation.rb', line 81

def deploy_stack(stack_name, stack_parameters)
  unless @client.describe_stacks(stack_name: stack_name).stacks.empty?
    puts 'Updating the existing stack'
    @client.update_stack(stack_parameters)
    @client.wait_until(:stack_update_complete, stack_name: stack_name)
  end
rescue Aws::CloudFormation::Errors::ValidationError => error
  if error.to_s.include? 'No updates are to be performed.'
    puts "Nothing to do."
  elsif error.to_s.include? 'Template error'
    raise error
  else
    puts 'Creating a new stack'
    @client.create_stack(stack_parameters)
    @client.wait_until(:stack_create_complete, stack_name: stack_name)
  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



49
50
51
52
53
54
55
# File 'lib/minimal_pipeline/cloudformation.rb', line 49

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/minimal_pipeline/cloudformation.rb', line 62

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