Top Level Namespace

Includes:
Cfnresponse

Defined Under Namespace

Modules: ActiveRecord, ApplicationHelper, AwsLambda, Jets, Kernel Classes: ApplicationController, ApplicationItem, ApplicationJob, ApplicationRecord, BasePathMapping, BucketConfigurator, Thor

Constant Summary collapse

STAGE_NAME =
"<%= @stage_name %>"

Instance Method Summary collapse

Instance Method Details

#lambda_handler(event:, context:) ⇒ Object



6
7
8
9
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
# File 'lib/jets/internal/app/functions/jets/base_path.rb', line 6

def lambda_handler(event:, context:)
  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}")

  mimic = event['ResourceProperties']['Mimic']
  physical_id = event['ResourceProperties']['PhysicalId'] || "PhysicalId"

  puts "mimic: #{mimic}"
  puts "physical_id: #{physical_id}"

  if event['RequestType'] == 'Delete'
    if mimic == 'FAILED'
      send_response(event, context, "FAILED")
    else
      mapping = BasePathMapping.new(event)
      mapping.delete(true) if mapping.should_delete?
      send_response(event, context, "SUCCESS")
    end
    return # early return
  end

  mapping = BasePathMapping.new(event)
  mapping.update

  response_status = mimic == "FAILED" ? "FAILED" : "SUCCESS"
  response_data = { "Hello" => "World" }

  send_response(event, context, response_status, response_data, physical_id)

# We rescue all exceptions and send an message to CloudFormation so we dont have to
# wait for over an hour for the stack operation to timeout and rollback.
rescue Exception => e
  puts e.message
  puts e.backtrace
  sleep 10 # provide delete to make sure that the log gets sent to CloudWatch
  send_response(event, context, "FAILED")
end

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jets/internal/app/functions/jets/base_path.rb', line 46

def send_response(event, context, response_status, response_data={}, physical_id="PhysicalId")
  response_body = JSON.dump(
    Status: response_status,
    Reason: "See the details in CloudWatch Log Stream: #{context.log_stream_name.inspect}",
    PhysicalResourceId: physical_id,
    StackId: event['StackId'],
    RequestId: event['RequestId'],
    LogicalResourceId: event['LogicalResourceId'],
    Data: response_data
  )

  puts "RESPONSE BODY:\n"
  puts response_body

  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

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