Non-Zero Exit Code is Swallowed
See original GitHub issueDescription
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
{"errorMessage":"Test Error","errorType":"Error","stackTrace":["exports.handler (/var/task/index.js:24:9)"]}
✨ Done in 8.61s.
MLP-0U8HV2H:mediainfo user$ echo $?
0
Expected result
An exit code of 1 or greater
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
1. MacOS
2. `sam --version`:
SAM CLI, version 0.14.0
`Add --debug flag to command you are running`
–debug
2019-03-25 18:11:08 Using SAM Template at /Users/user/projects/connect5-lambda-mediainfo/lambdas/mediainfo/template.yml
2019-03-25 18:11:08 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-03-25 18:11:08 Changing event name from before-call.apigateway to before-call.api-gateway
2019-03-25 18:11:08 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-03-25 18:11:08 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-03-25 18:11:08 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-03-25 18:11:08 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-03-25 18:11:08 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-03-25 18:11:08 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-03-25 18:11:08 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-03-25 18:11:08 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-03-25 18:11:08 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-03-25 18:11:08 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-03-25 18:11:08 Changing event name from before-call.apigateway to before-call.api-gateway
2019-03-25 18:11:08 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-03-25 18:11:08 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-03-25 18:11:08 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-03-25 18:11:08 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-03-25 18:11:08 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-03-25 18:11:08 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-03-25 18:11:08 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-03-25 18:11:08 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-03-25 18:11:08 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-03-25 18:11:08 local invoke command is called
2019-03-25 18:11:08 No Parameters detected in the template
2019-03-25 18:11:08 1 resources found in the template
2019-03-25 18:11:08 Found Serverless function with name='tfdefaultroutefile' and CodeUri='.'
2019-03-25 18:11:08 Trying paths: ['/Users/user/.docker/config.json', '/Users/user/.dockercfg']
2019-03-25 18:11:08 Found file at path: /Users/user/.docker/config.json
2019-03-25 18:11:08 Found 'auths' section
2019-03-25 18:11:08 Auth data for https://index.docker.io/v1/ is absent. Client might be using a credentials store instead.
2019-03-25 18:11:08 http://localhost:None "GET /v1.35/_ping HTTP/1.1" 200 2
2019-03-25 18:11:08 Looking for credentials via: env
2019-03-25 18:11:08 Found credentials in environment variables.
2019-03-25 18:11:08 Loading JSON file: /usr/local/Cellar/aws-sam-cli/0.14.0/libexec/lib/python3.7/site-packages/botocore/data/endpoints.json
2019-03-25 18:11:08 Event choose-service-name: calling handler <function handle_service_name_alias at 0x10b4df840>
2019-03-25 18:11:08 Loading JSON file: /usr/local/Cellar/aws-sam-cli/0.14.0/libexec/lib/python3.7/site-packages/botocore/data/lambda/2015-03-31/service-2.json
2019-03-25 18:11:08 Event creating-client-class.lambda: calling handler <function add_generate_presigned_url at 0x10b4a4378>
2019-03-25 18:11:08 The s3 config key is not a dictionary type, ignoring its value of: None
2019-03-25 18:11:08 Setting lambda timeout as (60, 60)
2019-03-25 18:11:08 Loading JSON file: /usr/local/Cellar/aws-sam-cli/0.14.0/libexec/lib/python3.7/site-packages/botocore/data/_retry.json
2019-03-25 18:11:08 Registering retry handlers for service: lambda
2019-03-25 18:11:08 Trying paths: ['/Users/user/.docker/config.json', '/Users/user/.dockercfg']
2019-03-25 18:11:08 Found file at path: /Users/user/.docker/config.json
2019-03-25 18:11:08 Found 'auths' section
2019-03-25 18:11:08 Auth data for https://index.docker.io/v1/ is absent. Client might be using a credentials store instead.
2019-03-25 18:11:08 Found one Lambda function with name 'tfdefaultroutefile'
2019-03-25 18:11:08 Invoking index.handler (nodejs8.10)
2019-03-25 18:11:08 No environment variables found for function 'tfdefaultroutefile'
2019-03-25 18:11:08 Environment variables overrides data is standard format
2019-03-25 18:11:08 Loading AWS credentials from session with profile 'default'
2019-03-25 18:11:08 Resolving code path. Cwd=/Users/user/projects/connect5-lambda-mediainfo/lambdas/mediainfo, CodeUri=.
2019-03-25 18:11:08 Resolved absolute path to code is /Users/user/projects/connect5-lambda-mediainfo/lambdas/mediainfo
2019-03-25 18:11:08 Code /Users/user/projects/connect5-lambda-mediainfo/lambdas/mediainfo is not a zip/jar file
2019-03-25 18:11:08 Skipping building an image since no layers were defined
2019-03-25 18:11:08 Trying paths: ['/Users/user/.docker/config.json', '/Users/user/.dockercfg']
2019-03-25 18:11:08 Found file at path: /Users/user/.docker/config.json
2019-03-25 18:11:08 Found 'auths' section
2019-03-25 18:11:08 Auth data for https://index.docker.io/v1/ is absent. Client might be using a credentials store instead.
2019-03-25 18:11:08 http://localhost:None "GET /v1.35/images/lambci/lambda:nodejs8.10/json HTTP/1.1" 200 None
2019-03-25 18:11:08 Looking for auth config
2019-03-25 18:11:08 Looking for auth entry for 'docker.io'
2019-03-25 18:11:08 Found 'https://index.docker.io/v1/'
2019-03-25 18:11:08 No auth config found
2019-03-25 18:11:09 http://localhost:None "POST /v1.35/images/create?tag=nodejs8.10&fromImage=lambci%2Flambda HTTP/1.1" 200 None
Fetching lambci/lambda:nodejs8.10 Docker container image......
2019-03-25 18:11:09 Mounting /Users/user/projects/connect5-lambda-mediainfo/lambdas/mediainfo as /var/task:ro inside runtime container
2019-03-25 18:11:09 http://localhost:None "POST /v1.35/containers/create HTTP/1.1" 201 90
2019-03-25 18:11:09 http://localhost:None "GET /v1.35/containers/774deb4e3a106fb54afd93528ff3c1490d998a5d547923bfe306c21e7685c5d8/json HTTP/1.1" 200 None
2019-03-25 18:11:09 http://localhost:None "GET /v1.35/containers/774deb4e3a106fb54afd93528ff3c1490d998a5d547923bfe306c21e7685c5d8/json HTTP/1.1" 200 None
2019-03-25 18:11:10 http://localhost:None "POST /v1.35/containers/774deb4e3a106fb54afd93528ff3c1490d998a5d547923bfe306c21e7685c5d8/start HTTP/1.1" 204 0
2019-03-25 18:11:10 Starting a timer for 240 seconds for function 'tfdefaultroutefile'
2019-03-25 18:11:10 http://localhost:None "GET /v1.35/containers/774deb4e3a106fb54afd93528ff3c1490d998a5d547923bfe306c21e7685c5d8/json HTTP/1.1" 200 None
2019-03-25 18:11:10 http://localhost:None "POST /containers/774deb4e3a106fb54afd93528ff3c1490d998a5d547923bfe306c21e7685c5d8/attach?stdout=1&stderr=1&logs=1&stream=1&stdin=0 HTTP/1.1" 101 0
START RequestId: a532a815-fc34-1203-07d4-70179e0debb5 Version: $LATEST
2019-03-26T01:11:11.221Z a532a815-fc34-1203-07d4-70179e0debb5 {"errorMessage":"Test Error","errorType":"Error","stackTrace":["exports.handler (/var/task/index.js:24:9)"]}
END RequestId: a532a815-fc34-1203-07d4-70179e0debb5
REPORT RequestId: a532a815-fc34-1203-07d4-70179e0debb5 Duration: 977.93 ms Billed Duration: 1000 ms Memory Size: 128 MMax Memory Used: 42 MB
2019-03-25 18:11:11 http://localhost:None "GET /v1.35/containers/774deb4e3a106fb54afd93528ff3c1490d998a5d547923bfe306c21e7685c5d8/json HTTP/1.1" 200 None
2019-03-25 18:11:11 http://localhost:None "DELETE /v1.35/containers/774deb4e3a106fb54afd93528ff3c1490d998a5d547923bfe306c21e7685c5d8?v=False&link=False&force=True HTTP/1.1" 204 0
{"errorMessage":"Test Error","errorType":"Error","stackTrace":["exports.handler (/var/task/index.js:24:9)"]}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Non-Zero Exit Code is Swallowed #1081 - aws/aws-sam-cli
Description. When a Lambda throws an error, the SAM tool prints the exception, but a zero exit code is returned.
Read more >Swallowed Error Exit Code. I've recently come up against the… | by ...
When I run this so that cmd fails, the whole line will return a non-zero exit code. For example: $ var="$(cmd)" && printf...
Read more >Handle specific error in bash - Unix & Linux Stack Exchange
I want to wrap this command with something, that will swallow non-zero exit code if specific error appears in stdout, but to bubble...
Read more >exit from bash script that uses $() - Stack Overflow
The $() ends up swallowing everything because the command-substitution takes pace in a subshell. What is the bash rule about subshells? Nothing ...
Read more >Unbuffer swallows the exit status of killed process - Super User
The expect "unbuffer" command seems to swallow the exit status of its child if the process is killed. Is there something that can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
?