Skip to content

Commit

Permalink
Merge pull request #724 from bugsnag/aws-lambda-response
Browse files Browse the repository at this point in the history
Fix issue with AWS lambda response output regex not being restrictive enough
  • Loading branch information
Cawllec authored Feb 19, 2025
2 parents 5a839d9 + 1c07118 commit fd8a0cd
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 9.23.1 - 2025/02/19

## Fixes

- Fix issue with the method used to parse the lambda response line [724](https://github.com/bugsnag/maze-runner/pull/724)

# 9.23.0 - 2025/02/18

## Enhancements
Expand Down
2 changes: 1 addition & 1 deletion lib/maze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# providing an alternative to the proliferation of global variables or singletons.
module Maze

VERSION = '9.23.0'
VERSION = '9.23.1'

class << self
attr_accessor :check, :driver, :internal_hooks, :mode, :start_time, :dynamic_retry, :public_address,
Expand Down
13 changes: 5 additions & 8 deletions lib/maze/aws/sam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def build_invoke_command(lambda, event)
#
# @return [Hash]
def parse(output)

unless valid?(output)
message = <<-WARNING
The lambda function did not successfully complete.
Expand All @@ -76,14 +77,10 @@ def parse(output)
end

# Attempt to parse response line of the output.
# It's possible for a Lambda to output nothing,
# e.g. if it forcefully exited, so we allow JSON parse failures here
begin
response_line = output.find { |line| /{.*}/.match(line.strip) }
parsed_output = JSON.parse(response_line)
rescue JSON::ParserError
return {}
end
# We can assume that the output is the last line present that is JSON parsable
response_lines = output.find_all { |line| /^{.*}$/.match(line.strip) }
response_line = response_lines.last
parsed_output = response_line.nil? ? {} : JSON.parse(response_line)

# Error output has no "body" of additional JSON so we can stop here
return parsed_output unless parsed_output.key?('body')
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/aws-sam/features/fixtures/node-app/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ Resources:
Path: /process-exit
Method: get

ThrownErrorFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: thrown-error/
Handler: app.lambdaHandler
Runtime: nodejs20.x
Events:
ThrownError:
Type: Api
Properties:
Path: /thrown-error
Method: get

Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.lambdaHandler = (event, context, callback) => {
throw new Error('Oh no!')
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def lambda_handler(event:, context:)
{
statusCode: 200
}
end
12 changes: 12 additions & 0 deletions test/e2e/aws-sam/features/fixtures/ruby-app/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ Resources:
Properties:
Path: /hello
Method: get
NoResponseFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: no_response/
Handler: app.lambda_handler
Runtime: ruby3.3
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /no_response
Method: get

Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
Expand Down
15 changes: 14 additions & 1 deletion test/e2e/aws-sam/features/invoke.feature
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,22 @@ Scenario: Executing a lambda function that returns a HTML body
And the lambda response "statusCode" equals 200
And the SAM exit code equals 0

Scenario: Executing a lambda function that does not respond
Scenario: Executing a lambda function that returns nothing
Given I invoke the "NoResponseFunction" lambda in "features/fixtures/ruby-app"
And the lambda response "body" is null
And the lambda response "statusCode" equals 200
And the SAM exit code equals 0

Scenario: Executing a lambda function that fails with no internal errors
Given I invoke the "ProcessExitFunction" lambda in "features/fixtures/node-app"
Then the lambda response "errorMessage" contains "Error: Runtime exited with error: exit status 1"
And the lambda response "errorType" equals "Runtime.ExitError"
And the lambda response "body" is null
And the SAM exit code equals 0

Scenario: Executing a lambda function that fails with an internal error
Given I invoke the "ThrownErrorFunction" lambda in "features/fixtures/node-app"
Then the lambda response "errorMessage" contains "Oh no!"
And the lambda response "errorType" equals "Error"
And the lambda response "body" is null
And the SAM exit code equals 0

0 comments on commit fd8a0cd

Please sign in to comment.