lib/cfnresponse.rb
# Debugging puts kept to help debug custom resources
Debugging puts kept to help debug custom resources
Debugging puts kept to help debug custom resources
def send_response(event, context, response_status, response_data={}, physical_id="PhysicalId", reason=nil)
    reason ||= "No details available"
    body_data = {
      "Status" => response_status,
      "Reason" => reason,
      "PhysicalResourceId" => physical_id,
      "StackId" => event['StackId'],
      "RequestId" => event['RequestId'],
      "LogicalResourceId" => event['LogicalResourceId'],
      "Data" => response_data
    }
    response_body = JSON.dump(body_data) 
    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'
    req = Net::HTTP::Put.new(url) 
    req.body = response_body
    req.content_length = response_body.bytesize
    req['content-type'] = ''
    req['content-length'] = response_body.bytesize
    if ENV['CFNRESPONSE_TEST']
      puts "uri #{uri.inspect}"
      return body_data 
    end
    res = http.request(req)
    puts "status code: #{res.code}"
    puts "headers: #{res.each_header.to_h.inspect}"
    puts "body: #{res.body}"
    if ["403", "200"].include? res.code
      return true
    else
      raise StandardError.new("Unsuccessful Account Creation!")
    end
  end
def send_response ( event , context , response_status , response_data = { } , physical_id = "PhysicalId" , reason = nil ) reason ||= "No details available" body_data = { "Status" => response_status , "Reason" => reason , "PhysicalResourceId" => physical_id , "StackId" => event [ 'StackId' ] , "RequestId" => event [ 'RequestId' ] , "LogicalResourceId" => event [ 'LogicalResourceId' ] , "Data" => response_data } response_body = JSON . dump ( body_data ) 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' req = Net :: HTTP :: Put . new ( url ) req . body = response_body req . content_length = response_body . bytesize req [ 'content-type' ] = '' req [ 'content-length' ] = response_body . bytesize if ENV [ 'CFNRESPONSE_TEST' ] puts "uri #{uri.inspect}" return body_data end res = http . request ( req ) puts "status code: #{res.code}" puts "headers: #{res.each_header.to_h.inspect}" puts "body: #{res.body}" if [ "403" , "200" ] . include? res . code return true else raise StandardError . new ( "Unsuccessful Account Creation!" ) end end
Debugging puts kept to help debug custom resources
Debugging puts kept to help debug custom resources
# response_body is a JSON string # must used url to include the AWSAccessKeyId and Signature # url includes query string and uri.path does not, must used url t # set headers # early return to not send the request
event
<not_specific>
context
<not_specific>
response_status
<not_specific>
response_data
<not_specific>
physical_id
<not_specific>
reason
<not_specific>