Class: AWS::CloudFormation::Helper::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_cloudformation_helper/response.rb

Overview

Handles sending a response to CloudFormation

Constant Summary collapse

FAILURE_STATUS =
'FAILED'
HTTP_MAX_RETRIES =
3
SUCCESS_STATUS =
'SUCCESS'

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



17
18
19
# File 'lib/aws_cloudformation_helper/response.rb', line 17

def initialize
  @http_retries = 1
end

Instance Method Details

#failure(reason = '') ⇒ Object



21
22
23
24
25
26
# File 'lib/aws_cloudformation_helper/response.rb', line 21

def failure(reason = '')
  Helper.logger.info("Sending #{FAILURE_STATUS} response to CloudFormation")
  status_code = send_response('PUT', Event.instance.response_url, provider_response(FAILURE_STATUS, reason))
  err_msg = "Failed to send failure message to CloudFormation pre-signed S3 URL. RC: #{status_code}"
  raise err_msg if status_code > 400
end

#send_response(method, response_url, body = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aws_cloudformation_helper/response.rb', line 28

def send_response(method, response_url, body = nil)
  uri = ::URI.parse(response_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  request = http_request(method, uri, body)

  Helper.logger.debug("Sending #{method} request to URL #{response_url}...")
  response = http.request(request)

  Helper.logger.debug("HTTP Response: #{response.inspect}")
  response.code.to_i
rescue StandardError => e
  err_msg = 'Failed to send response to CloudFormation pre-signed S3 URL. '\
  "(Attempt #{@http_retries} of #{HTTP_MAX_RETRIES}) Error Details: #{e}"
  Helper.logger.error(err_msg)
  retry if (@http_retries += 1) <= HTTP_MAX_RETRIES
  @http_retries = 1
  err_msg = 'Reached max retry attempts and failed to send message to CloudFormation pre-signed S3 URL.'
  Helper.logger.error(err_msg)
  raise e
end

#success(reason = '') ⇒ Object



50
51
52
53
54
55
# File 'lib/aws_cloudformation_helper/response.rb', line 50

def success(reason = '')
  Helper.logger.info("Sending #{SUCCESS_STATUS} response to CloudFormation")
  status_code = send_response('PUT', Event.instance.response_url, provider_response(SUCCESS_STATUS, reason))
  err_msg = "Failed to send success message to CloudFormation pre-signed S3 URL. RC: #{status_code}"
  raise err_msg if status_code > 400
end