Open jcald1 opened 5 years ago
What does your handler look like?
Having this issue with local invoke
and Go
Handler:
package main
import (
"context"
"errors"
"github.com/aws/aws-lambda-go/lambda"
)
func HandleRequest(ctx context.Context, ev struct{}) (string, error) {
return "", errors.New("FAIL")
}
func main() {
lambda.Start(HandleRequest)
}
$ sam --version
SAM CLI, version 0.15.0
$ git clone git@github.com:michaelgruber/aws-sam-cli-non-zero.git && cd aws-sam-cli-non-zero
$ make
$ echo "{}" | sam local invoke AWSSAMCLINonZero
2019-04-23 11:07:35 Reading invoke payload from stdin (you can also pass it from file with --event)
2019-04-23 11:07:35 Found credentials in shared credentials file: ~/.aws/credentials
2019-04-23 11:07:35 Invoking main (go1.x)
2019-04-23 11:07:35 Decompressing /Users/mgruber/Desktop/aws-sam-cli-non-zero/main.zip
Fetching lambci/lambda:go1.x Docker container image......
2019-04-23 11:07:36 Mounting /private/var/folders/6p/r4n9zccj1sl028w4_yym43g00000gn/T/tmp2majhmef as /var/task:ro,delegated inside runtime container
START RequestId: 496f2129-51a7-1e03-e917-edfaff706f09 Version: $LATEST
END RequestId: 496f2129-51a7-1e03-e917-edfaff706f09
REPORT RequestId: 496f2129-51a7-1e03-e917-edfaff706f09 Duration: 2.31 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 6 MB
{
"errorMessage": "FAIL",
"errorType": "errorString"
}
$ echo $?
0
There is some grey area here. The command didn't actually fail, the invoke of your function did. Currently, we treat this as a successful command (as SAM CLI didn't crash or error out). There are defiantly two views to this so marking this as needs-feedback.
As a sidenote, with the above example, running
$ echo '{}' | sam local invoke AWSSAMCLINonZero 2>err.log 1>out.log
Produces an empty out.log
and the following err.log
2019-04-23 12:05:12 Reading invoke payload from stdin (you can also pass it from file with --event)
2019-04-23 12:05:12 Found credentials in shared credentials file: ~/.aws/credentials
2019-04-23 12:05:12 Invoking main (go1.x)
2019-04-23 12:05:12 Decompressing /Users/mgruber/Desktop/aws-sam-cli-non-zero/main.zip
Fetching lambci/lambda:go1.x Docker container image......
2019-04-23 12:05:13 Mounting /private/var/folders/6p/r4n9zccj1sl028w4_yym43g00000gn/T/tmpox_4l9xp as /var/task:ro,delegated inside runtime container
[32mSTART RequestId: a561bbf3-c715-16a3-881c-128dd5edd3f3 Version: $LATEST[0m
[32mEND RequestId: a561bbf3-c715-16a3-881c-128dd5edd3f3[0m
[32mREPORT RequestId: a561bbf3-c715-16a3-881c-128dd5edd3f3 Duration: 3.33 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 6 MB [0m
[31m{
"errorMessage": "FAIL",
"errorType": "errorString"
}[0m
With the handler changed to
package main
import (
"context"
"github.com/aws/aws-lambda-go/lambda"
)
func HandleRequest(ctx context.Context, ev struct{}) (string, error) {
return `{"success":true}`, nil
}
func main() {
lambda.Start(HandleRequest)
}
out.log
:
"{\"success\":true}"
err.log
:
2019-04-23 12:08:23 Reading invoke payload from stdin (you can also pass it from file with --event)
2019-04-23 12:08:23 Found credentials in shared credentials file: ~/.aws/credentials
2019-04-23 12:08:23 Invoking main (go1.x)
2019-04-23 12:08:23 Decompressing /Users/mgruber/Desktop/aws-sam-cli-non-zero/main.zip
Fetching lambci/lambda:go1.x Docker container image......
2019-04-23 12:08:24 Mounting /private/var/folders/6p/r4n9zccj1sl028w4_yym43g00000gn/T/tmp6y6nv_b9 as /var/task:ro,delegated inside runtime container
[32mSTART RequestId: b6ffb7fd-99b2-10b1-ad6d-d712e76b739e Version: $LATEST[0m
[32mEND RequestId: b6ffb7fd-99b2-10b1-ad6d-d712e76b739e[0m
[32mREPORT RequestId: b6ffb7fd-99b2-10b1-ad6d-d712e76b739e Duration: 2.53 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 6 MB [0m
If both are considered successful shouldn't the error JSON get sent to stdout?
I'm having trouble with this same behavior in a different context. I have a python lambda that raises an error. When I deploy the lambda to AWS and use the boto3
client to invoke the deployed function I receive details about the error in the response Payload
. However, when I use the boto3
client to invoke the same function locally the Payload
property is empty.
# BoomFunction
def lambda_handler(event, context):
raise Exception('BOOM!')
remote = boto3.client('lambda')
remote_res = remote.invoke(
FunctionName='BoomFunction',
InvocationType='RequestResponse',
Payload='{}'
)
remote_payload = str(remote_res['Payload'].read(), 'utf-8')
print(remote_payload)
# {"errorMessage": "BOOM!", "errorType": "Exception", "stackTrace": [" File \"/var/task/app.py\", line 37, in lambda_handler\n raise Exception('BOOM!')\n"]}
local = boto3.client('lambda', **{
'endpoint_url': 'http://127.0.0.1:3001'
})
local_res = local.invoke(
FunctionName='BoomFunction',
InvocationType='RequestResponse',
Payload='{}'
)
local_payload = str(local_res['Payload'].read(), 'utf-8')
print(local_payload)
#
I do see the error message in the sam local
logs.
2019-04-25 17:12:37 127.0.0.1 - - [25/Apr/2019 17:12:37] "POST /2015-03-31/functions/BoomFunction/invocations HTTP/1.1" 200 -
2019-04-25 17:15:43 Invoking app.lambda_handler (python3.7)
Fetching lambci/lambda:python3.7 Docker container image......
2019-04-25 17:15:45 Mounting /<redacted>/.aws-sam/build/BoomFunction as /var/task:ro inside runtime container
START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST
raise Exception('BOOM!')ine 37, in lambda_handler
END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72
REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Duration: 8.08 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 23 MB
{
"errorType": "Exception",
"errorMessage": "BOOM!",
"stackTrace": [
" File \"/var/task/app.py\", line 37, in lambda_handler\n raise Exception('BOOM!')\n"
]
}
This is also causing an issue for us when using sam local invoke in combination with other tooling.
It would be very helpful to get a Non-Zero exit code from sam local invoke
in case the invoked function crashes with Non-Zero code.
I was hoping to use sam local invoke
in CI with known input for a basic sanity check. I was disappointed that I can't seem to get sam to give me a failing return code.
So for now I've stitched together a fragile hack:
! sam local invoke --event events/event.json 2>&1 |grep -i error
It's not great, but with sam 1.18 and my current nodejs-based lambda this can catch unexpected failures.
Could we get something like sam local invoke --fail-on-error
?
Description
When a Lambda throws an error, the SAM tool prints the exception, but a zero exit code is returned.
Steps to reproduce
Create a Lambda that throws an Error. Call the Lambda with SAM, e.g.: sam local invoke "lambda1"
Observed result
Expected result
An exit code of 1 or greater
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
--debug