Class: CfnResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn_response.rb,
lib/cfn_response/base.rb,
lib/cfn_response/sender.rb,
lib/cfn_response/builder.rb,
lib/cfn_response/version.rb

Defined Under Namespace

Classes: Base, Builder, Error, Sender

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(event, context = nil) ⇒ CfnResponse

Returns a new instance of CfnResponse.



11
12
13
# File 'lib/cfn_response.rb', line 11

def initialize(event, context=nil)
  @event, @context = event, context
end

Instance Method Details

#failed(input = {}) ⇒ Object



54
55
56
57
# File 'lib/cfn_response.rb', line 54

def failed(input={})
  input[:Status] = "FAILED"
  send_to_cloudformation(input)
end

#pause_for_cloudwatchObject



59
60
61
62
# File 'lib/cfn_response.rb', line 59

def pause_for_cloudwatch
  return if ENV['CFN_RESPONSE_TEST'] || ENV['CFN_RESPONSE_SEND']
  sleep 10 # a little time for logs to be sent to CloudWatch
end

#responseObject Also known as: respond



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cfn_response.rb', line 15

def response
  show_event if show_event?
  result = yield if block_given?
  success unless @finished
  pause_for_cloudwatch
  result
rescue Exception => e
  puts "ERROR #{e.message}"
  puts "BACKTRACE:\n#{e.backtrace.join("\n")}"
  pause_for_cloudwatch
  failed
end

#send_to_cloudformation(input = {}) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/cfn_response.rb', line 40

def send_to_cloudformation(input={})
  builder = Builder.new(@event, @context)
  response_data = builder.call(input)
  sender = Sender.new(@event, @context)
  result = sender.call(response_data) unless ENV['CFN_RESPONSE_SEND'] == '0'
  @finished = true
  result
end

#show_eventObject



33
34
35
36
37
38
# File 'lib/cfn_response.rb', line 33

def show_event
  puts("event['RequestType'] #{@event['RequestType']}")
  puts("event: #{JSON.dump(@event)}")
  puts("context: #{JSON.dump(@context)}")
  puts("context.log_stream_name #{@context.log_stream_name.inspect}")
end

#show_event?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/cfn_response.rb', line 29

def show_event?
  ENV['CFN_RESPONSE_VERBOSE'].nil? ? true : ENV['CFN_RESPONSE_VERBOSE'] == '1'
end

#success(input = {}) ⇒ Object



49
50
51
52
# File 'lib/cfn_response.rb', line 49

def success(input={})
  input[:Status] = "SUCCESS"
  send_to_cloudformation(input)
end