Module: Cfnresponse

Defined in:
lib/cfnresponse.rb,
lib/cfnresponse/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.4.0"

Instance Method Summary collapse

Instance Method Details

#json_pretty(data) ⇒ Object

Useful for pretty json output on cloud9 and also CloudWatch logs



55
56
57
58
59
60
61
# File 'lib/cfnresponse.rb', line 55

def json_pretty(data)
  if ENV['C9_USER']
    JSON.pretty_generate(data)
  else
    JSON.dump(data) # one a single line for CloudWatch to reformat
  end
end

#send_response(event, context, response_status, response_data = {}, physical_id = "PhysicalId", reason = nil) ⇒ Object

Debugging puts kept to help debug custom resources



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
# File 'lib/cfnresponse.rb', line 10

def send_response(event, context, response_status, response_data={}, physical_id="PhysicalId", reason=nil)
  reason ||= "See the details in CloudWatch Log Group: #{context.log_group_name} Log Stream: #{context.log_stream_name}"

  body_data = {
    "Status" => response_status,
    "Reason" => reason,
    "PhysicalResourceId" => physical_id,
    "StackId" => event['StackId'],
    "RequestId" => event['RequestId'],
    "LogicalResourceId" => event['LogicalResourceId'],
    "Data" => response_data
  }

  puts "Response body:\n"
  puts json_pretty(body_data)

  response_body = JSON.dump(body_data) # response_body is a JSON string

  url = event['ResponseURL']
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = http.read_timeout = 30
  http.use_ssl = true if uri.scheme == 'https'

  # must used url to include the AWSAccessKeyId and Signature
  req = Net::HTTP::Put.new(url) # url includes query string and uri.path does not, must used url t
  req.body = response_body
  req.content_length = response_body.bytesize

  # set headers
  req['content-type'] = ''
  req['content-length'] = response_body.bytesize

  if ENV['CFNRESPONSE_TEST']
    puts "uri #{uri.inspect}"
    return body_data # early return to not send the request
  end

  res = http.request(req)
  puts "status code: #{res.code}"
  puts "headers: #{res.each_header.to_h.inspect}"
  puts "body: #{res.body}"
end