class AwsStack::CloudFormation

AWS Session creation with profile

Public Class Methods

new(options) click to toggle source
# File lib/awsstack/cloudformation.rb, line 7
def initialize(options)
  @credentials = options[:credentials]
  create_cfn_client
  @operation = options[:operation].to_sym
  @stackname = options[:stackname]
  @templatefile = options[:templatefile]
  @paramfile = options[:paramfile]
  @environment = options[:environment]
  @debug = options[:debug]

  if public_methods.include?(@operation)
    public_send @operation
  else
    puts "Requested operation '#{@operation}' is not a public method."
  end
end

Public Instance Methods

check() click to toggle source
# File lib/awsstack/cloudformation.rb, line 24
def check
  raise unless validate_template
  puts "Template '#{@templatefile}' validated! OK."
end
create() click to toggle source
# File lib/awsstack/cloudformation.rb, line 29
def create
  check
  @params = read_param_file
  pp create_stack
end
delete() click to toggle source
# File lib/awsstack/cloudformation.rb, line 41
def delete
  delete_stack
  puts 'Delete started...'
end
operation_names() click to toggle source
# File lib/awsstack/cloudformation.rb, line 46
def operation_names
  pp @cfn.operation_names
end
update() click to toggle source
# File lib/awsstack/cloudformation.rb, line 35
def update
  check
  @params = read_param_file
  update_stack
end

Private Instance Methods

create_cfn_client() click to toggle source
# File lib/awsstack/cloudformation.rb, line 52
def create_cfn_client
  @cfn = Aws::CloudFormation::Client.new(credentials: @credentials)
end
create_change_set() click to toggle source
# File lib/awsstack/cloudformation.rb, line 130
def create_change_set
  @cfn.create_change_set
end
create_stack() click to toggle source
# File lib/awsstack/cloudformation.rb, line 98
def create_stack
  @cfn.create_stack(
    stack_name: @stackname, # required
    template_body: template.body,
    template_url: template.url,
    capabilities: ['CAPABILITY_IAM'],
    parameters: @params
  ).stack_id
rescue Aws::CloudFormation::Errors::AlreadyExistsException, Aws::CloudFormation::Errors::ValidationError => e
  puts "#{e.class} : #{e.message}"
  exit 1
end
delete_change_set() click to toggle source
# File lib/awsstack/cloudformation.rb, line 142
def delete_change_set
  @cfn.delete_change_set
end
delete_stack() click to toggle source
# File lib/awsstack/cloudformation.rb, line 124
def delete_stack
  @cfn.delete_stack(
    stack_name: @stackname, # required
  )
end
describe_change_set() click to toggle source
# File lib/awsstack/cloudformation.rb, line 134
def describe_change_set
  @cfn.describe_change_set
end
execute_change_set() click to toggle source
# File lib/awsstack/cloudformation.rb, line 138
def execute_change_set
  @cfn.execute_change_set
end
list_stacks() click to toggle source
# File lib/awsstack/cloudformation.rb, line 87
def list_stacks
  @cfn.list_stacks
end
prepare_params(params) click to toggle source
# File lib/awsstack/cloudformation.rb, line 77
def prepare_params(params)
  params.map do |key, value|
    {
      parameter_key: key,
      parameter_value: value,
      use_previous_value: false
    }
  end
end
read_file(file) click to toggle source
# File lib/awsstack/cloudformation.rb, line 56
def read_file(file)
  File.open(file, 'r').read
end
read_param_file() click to toggle source
# File lib/awsstack/cloudformation.rb, line 66
def read_param_file
  param_file = if @paramfile.class == NilClass
                 "output/params/#{@environment}/#{File.basename @templatefile}"
               else
                 @paramfile
               end
  return unless File.file? param_file
  param_string = read_file param_file
  prepare_params JSON.parse(param_string)['Parameters']
end
template() click to toggle source
# File lib/awsstack/cloudformation.rb, line 60
def template
  @template_instance || @template_instance = AwsStack::CfnTemplate.new(
    credentials: @credentials, templatefile: @templatefile, stackname: @stackname
  )
end
update_stack() click to toggle source
# File lib/awsstack/cloudformation.rb, line 111
def update_stack
  @cfn.update_stack(
    stack_name: @stackname, # required
    template_body: template.body,
    template_url: template.url,
    capabilities: ['CAPABILITY_IAM'],
    parameters: @params
  ).stack_id
rescue Aws::CloudFormation::Errors::ValidationError => e
  puts "#{e.class} : #{e.message}"
  exit 1
end
validate_template() click to toggle source
# File lib/awsstack/cloudformation.rb, line 91
def validate_template
  @cfn.validate_template(
    template_body: template.body,
    template_url: template.url
  )
end