aws-sam-cli: `sam local invoke` (Using HelloWorld template throws "Error: Error building docker image"
Description:
When invoking sam build docker fails to build the image
Steps to reproduce:
- pip install aws-sam-cli
- mkdir test; cd test
- sam init; Choose HelloWorld
- sam build
- sam local invoke
Observed result:
$ sam local invoke
Invoking app.lambdaHandler (nodejs12.x)
Image was not found.
Building image...................
Failed to build Docker Image
NoneType: None
Error: Error building docker image: The command '/bin/sh -c chmod +x /var/rapid/aws-lambda-rie' returned a non-zero code: 1
Docker says:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59e236baf8e8 7f5c2fbde043 "/bin/sh -c 'chmod +…" About a minute ago Exited (1) About a minute ago distracted_bhabha
dcdf8949474a 7f5c2fbde043 "/bin/sh -c 'chmod +…" 4 days ago Exited (1) 4 days ago adoring_einstein
50a9c04dea71 7f5c2fbde043 "/bin/sh -c 'chmod +…" 4 days ago Exited (1) 4 days ago musing_raman
559bb77ddac1 7f5c2fbde043 "/bin/sh -c 'chmod +…" 4 days ago Exited (1) 4 days ago busy_hawking
Expected result:
Mounting /home/kingram/WORK/tmp/samtest/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container START RequestId: 67bb22f1-747e-4655-92ad-c681f5482447 Version: $LATEST END RequestId: 67bb22f1-747e-4655-92ad-c681f5482447 REPORT RequestId: 67bb22f1-747e-4655-92ad-c681f5482447 Init Duration: 0.08 ms Duration: 58.41 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 128 MB No Content-Type given. Defaulting to ‘application/json’. 2021-08-06 22:01:33 192.168.1.161 - - [06/Aug/2021 22:01:33] “GET /hello HTTP/1.1” 200 - 2021-08-06 22:01:34 192.168.1.161 - - [06/Aug/2021 22:01:34] “GET /favicon.ico HTTP/1.1” 403 -
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: Ubuntu 20.04
sam --version
: 1.27.2- AWS region: us-west
Add --debug flag to command you are running
$ sam local invoke --debug
2021-08-12 22:47:16,261 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-08-12 22:47:16,263 | Using config file: samconfig.toml, config environment: default
2021-08-12 22:47:16,264 | Expand command line arguments to:
2021-08-12 22:47:16,264 | --template_file=/home/kingram/WORK/test/sam-app/.aws-sam/build/template.yaml --no_event --layer_cache_basedir=/home/kingram/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2021-08-12 22:47:16,264 | local invoke command is called
2021-08-12 22:47:16,282 | No Parameters detected in the template
2021-08-12 22:47:16,375 | 2 stacks found in the template
2021-08-12 22:47:16,375 | No Parameters detected in the template
2021-08-12 22:47:16,464 | 2 resources found in the stack
2021-08-12 22:47:16,464 | No Parameters detected in the template
2021-08-12 22:47:16,549 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2021-08-12 22:47:16,550 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to /home/kingram/WORK/test/sam-app/.aws-sam/build/template.yaml
2021-08-12 22:47:16,569 | Found one Lambda function with name 'HelloWorldFunction'
2021-08-12 22:47:16,569 | Invoking app.lambdaHandler (nodejs12.x)
2021-08-12 22:47:16,569 | No environment variables found for function 'HelloWorldFunction'
2021-08-12 22:47:16,569 | Environment variables overrides data is standard format
2021-08-12 22:47:16,569 | Loading AWS credentials from session with profile 'None'
2021-08-12 22:47:16,611 | Resolving code path. Cwd=/home/kingram/WORK/test/sam-app/.aws-sam/build, CodeUri=/home/kingram/WORK/test/sam-app/.aws-sam/build/HelloWorldFunction
2021-08-12 22:47:16,611 | Resolved absolute path to code is /home/kingram/WORK/test/sam-app/.aws-sam/build/HelloWorldFunction
2021-08-12 22:47:16,611 | Code /home/kingram/WORK/test/sam-app/.aws-sam/build/HelloWorldFunction is not a zip/jar file
2021-08-12 22:47:16,618 | Image was not found.
Building image...................
2021-08-12 22:47:21,356 | Failed to build Docker Image
NoneType: None
2021-08-12 22:47:21,362 | Cleaning all decompressed code dirs
2021-08-12 22:47:21,364 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '897aba07-bb21-48ce-853d-060c8473e075', 'installationId': '6e07d42b-6906-4f1c-b030-85891cf49146', 'sessionId': 'f8e81d61-11e9-4e26-91b8-5afda75aaeb6', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.7.9', 'samcliVersion': '1.27.2', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local invoke', 'duration': 5099, 'exitReason': 'ImageBuildException', 'exitCode': 1}}]}
2021-08-12 22:47:21,733 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: Error building docker image: The command '/bin/sh -c chmod +x /var/rapid/aws-lambda-rie' returned a non-zero code: 1
Other command:
$ sam local start-api --host 192.168.1.83 --debug
2021-08-12 22:50:33,828 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-08-12 22:50:33,830 | Using config file: samconfig.toml, config environment: default
2021-08-12 22:50:33,830 | Expand command line arguments to:
2021-08-12 22:50:33,831 | --template_file=/home/kingram/WORK/test/sam-app/.aws-sam/build/template.yaml --host=192.168.1.83 --port=3000 --static_dir=public --layer_cache_basedir=/home/kingram/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2021-08-12 22:50:34,095 | local start-api command is called
2021-08-12 22:50:34,112 | No Parameters detected in the template
2021-08-12 22:50:34,204 | 2 stacks found in the template
2021-08-12 22:50:34,205 | No Parameters detected in the template
2021-08-12 22:50:34,289 | 2 resources found in the stack
2021-08-12 22:50:34,289 | No Parameters detected in the template
2021-08-12 22:50:34,374 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2021-08-12 22:50:34,374 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to /home/kingram/WORK/test/sam-app/.aws-sam/build/template.yaml
2021-08-12 22:50:34,384 | No Parameters detected in the template
2021-08-12 22:50:34,472 | No Parameters detected in the template
2021-08-12 22:50:34,557 | Found '1' API Events in Serverless function with name 'HelloWorldFunction'
2021-08-12 22:50:34,557 | Detected Inline Swagger definition
2021-08-12 22:50:34,557 | Lambda function integration not found in Swagger document at path='/hello' method='get'
2021-08-12 22:50:34,558 | Found '0' APIs in resource 'ServerlessRestApi'
2021-08-12 22:50:34,558 | Removed duplicates from '0' Explicit APIs and '1' Implicit APIs to produce '1' APIs
2021-08-12 22:50:34,558 | 1 APIs found in the template
2021-08-12 22:50:34,570 | Mounting HelloWorldFunction at http://192.168.1.83:3000/hello [GET]
2021-08-12 22:50:34,570 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2021-08-12 22:50:34,570 | Localhost server is starting up. Multi-threading = True
2021-08-12 22:50:34 * Running on http://192.168.1.83:3000/ (Press CTRL+C to quit)
2021-08-12 22:50:46,958 | Constructed String representation of Event to invoke Lambda. Event: {"body": null, "headers": {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-US,en;q=0.9", "Cache-Control": "max-age=0", "Connection": "keep-alive", "Dnt": "1", "Host": "192.168.1.83:3000", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "X-Forwarded-Port": "3000", "X-Forwarded-Proto": "http"}, "httpMethod": "GET", "isBase64Encoded": false, "multiValueHeaders": {"Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Accept-Encoding": ["gzip, deflate"], "Accept-Language": ["en-US,en;q=0.9"], "Cache-Control": ["max-age=0"], "Connection": ["keep-alive"], "Dnt": ["1"], "Host": ["192.168.1.83:3000"], "Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"], "X-Forwarded-Port": ["3000"], "X-Forwarded-Proto": ["http"]}, "multiValueQueryStringParameters": null, "path": "/hello", "pathParameters": null, "queryStringParameters": null, "requestContext": {"accountId": "123456789012", "apiId": "1234567890", "domainName": "192.168.1.83:3000", "extendedRequestId": null, "httpMethod": "GET", "identity": {"accountId": null, "apiKey": null, "caller": null, "cognitoAuthenticationProvider": null, "cognitoAuthenticationType": null, "cognitoIdentityPoolId": null, "sourceIp": "192.168.1.161", "user": null, "userAgent": "Custom User Agent String", "userArn": null}, "path": "/hello", "protocol": "HTTP/1.1", "requestId": "650c92be-2f04-4c10-964e-95f99bfd7deb", "requestTime": "12/Aug/2021:22:50:34 +0000", "requestTimeEpoch": 1628808634, "resourceId": "123456", "resourcePath": "/hello", "stage": "Prod"}, "resource": "/hello", "stageVariables": null, "version": "1.0"}
2021-08-12 22:50:46,960 | Found one Lambda function with name 'HelloWorldFunction'
2021-08-12 22:50:46,961 | Invoking app.lambdaHandler (nodejs12.x)
2021-08-12 22:50:46,961 | No environment variables found for function 'HelloWorldFunction'
2021-08-12 22:50:46,961 | Environment variables overrides data is standard format
2021-08-12 22:50:46,962 | Loading AWS credentials from session with profile 'None'
2021-08-12 22:50:47,006 | Resolving code path. Cwd=/home/kingram/WORK/test/sam-app/.aws-sam/build, CodeUri=/home/kingram/WORK/test/sam-app/.aws-sam/build/HelloWorldFunction
2021-08-12 22:50:47,006 | Resolved absolute path to code is /home/kingram/WORK/test/sam-app/.aws-sam/build/HelloWorldFunction
2021-08-12 22:50:47,006 | Code /home/kingram/WORK/test/sam-app/.aws-sam/build/HelloWorldFunction is not a zip/jar file
2021-08-12 22:50:47,014 | Image was not found.
Building image...................
2021-08-12 22:50:51,571 | Failed to build Docker Image
NoneType: None
2021-08-12 22:50:51,576 | Cleaning all decompressed code dirs
2021-08-12 22:50:51,577 | Exception on /hello [GET]
Traceback (most recent call last):
File "/PUBLIC/DOWNLOADS/.venv/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/PUBLIC/DOWNLOADS/.venv/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/PUBLIC/DOWNLOADS/.venv/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/PUBLIC/DOWNLOADS/.venv/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/PUBLIC/DOWNLOADS/.venv/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/PUBLIC/DOWNLOADS/.venv/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/local/apigw/local_apigw_service.py", line 317, in _request_handler
self.lambda_runner.invoke(route.function_name, event, stdout=stdout_stream_writer, stderr=self.stderr)
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/commands/local/lib/local_lambda.py", line 137, in invoke
container_host_interface=self.container_host_interface,
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/lib/telemetry/metric.py", line 217, in wrapped_func
return_value = func(*args, **kwargs)
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/local/lambdafn/runtime.py", line 176, in invoke
container = self.create(function_config, debug_context, container_host, container_host_interface)
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/local/lambdafn/runtime.py", line 86, in create
container_host_interface=container_host_interface,
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/local/docker/lambda_container.py", line 87, in __init__
image = LambdaContainer._get_image(lambda_image, runtime, packagetype, imageuri, layers)
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/local/docker/lambda_container.py", line 213, in _get_image
return lambda_image.build(runtime, packagetype, image, layers)
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/local/docker/lambda_image.py", line 144, in build
self._build_image(image if image else image_name, image_tag, downloaded_layers, stream=stream_writer)
File "/PUBLIC/DOWNLOADS/aws-sam-cli/samcli/local/docker/lambda_image.py", line 245, in _build_image
raise ImageBuildException("Error building docker image: {}".format(log["error"]))
samcli.commands.local.cli_common.user_exceptions.ImageBuildException: Error building docker image: The command '/bin/sh -c chmod +x /var/rapid/aws-lambda-rie' returned a non-zero code: 1
2021-08-12 22:50:51 192.168.1.161 - - [12/Aug/2021 22:50:51] "GET /hello HTTP/1.1" 502 -
2021-08-12 22:50:51 192.168.1.161 - - [12/Aug/2021 22:50:51] "GET /favicon.ico HTTP/1.1" 403 -
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 24 (6 by maintainers)
@kingram6865 Looks like you are attempting to build/run an x86 image on an ARM machine. Have you configured qemu? https://github.com/multiarch/qemu-user-static#getting-started
This command should be the one to use:
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
Let me know how it goes
I have fixed this issue by running this command
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
Do that and run
In my case, the solution turned out to be simple. Needed to change the
template.yaml
file fromto
See this thread on StackOverflow
still have the issue even after running @ninjasujan command !: MacBook-Pro:lambda-local jamin$ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes Unable to find image ‘multiarch/qemu-user-static:latest’ locally latest: Pulling from multiarch/qemu-user-static 205dae5015e7: Pull complete 816739e52091: Pull complete 30abb83a18eb: Pull complete 0657daef200b: Pull complete 30c9c93f40b9: Pull complete Digest: sha256:fe60359c92e86a43cc87b3d906006245f77bfc0565676b80004cc666e4feb9f0 Status: Downloaded newer image for multiarch/qemu-user-static:latest WARNING: The requested image’s platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested Error while loading /qemu-binfmt-conf.sh: Exec format error
This issue seems to be recurring now. We are encountering below error while locally invoking a lambda in AWS code build machine
<html> <body>Image was not found.
842 | Building image… 843 | Failed to build Docker Image 844 | NoneType: None 845 | Exception on /2015-03-31/functions/FunctionName/invocations [POST] 846 | Traceback (most recent call last): 847 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py”, line 2447, in wsgi_app 848 | response = self.full_dispatch_request() 849 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py”, line 1952, in full_dispatch_request 850 | rv = self.handle_user_exception(e) 851 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py”, line 1821, in handle_user_exception 852 | reraise(exc_type, exc_value, tb) 853 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/_compat.py”, line 39, in reraise 854 | raise value 855 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py”, line 1950, in full_dispatch_request 856 | rv = self.dispatch_request() 857 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py”, line 1936, in dispatch_request 858 | return self.view_functionsrule.endpoint 859 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/lambda_service/local_lambda_invoke_service.py”, line 151, in _invoke_request_handler 860 | self.lambda_runner.invoke(function_name, request_data, stdout=stdout_stream_writer, stderr=self.stderr) 861 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/commands/local/lib/local_lambda.py”, line 130, in invoke 862 | self.local_runtime.invoke( 863 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py”, line 217, in wrapped_func 864 | return_value = func(*args, **kwargs) 865 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/lambdafn/runtime.py”, line 176, in invoke 866 | container = self.create(function_config, debug_context, container_host, container_host_interface) 867 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/lambdafn/runtime.py”, line 73, in create 868 | container = LambdaContainer( 869 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/docker/lambda_container.py”, line 87, in init 870 | image = LambdaContainer._get_image(lambda_image, runtime, packagetype, imageuri, layers) 871 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/docker/lambda_container.py”, line 213, in _get_image 872 | return lambda_image.build(runtime, packagetype, image, layers) 873 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/docker/lambda_image.py”, line 144, in build 874 | self._build_image(image if image else image_name, image_tag, downloaded_layers, stream=stream_writer) 875 | File “/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/docker/lambda_image.py”, line 245, in _build_image 876 | raise ImageBuildException(“Error building docker image: {}”.format(log[“error”])) 877 | samcli.commands.local.cli_common.user_exceptions.ImageBuildException: Error building docker image: The command ‘/bin/sh -c chmod +x /var/rapid/aws-lambda-rie’ returned a non-zero code: 1
</body> </html>Build machine specs:
AWS CodeBuild - Linux env SAM CLI --version: 1.27.2 AWS region: us-east-1
It worked for me. I am running on a Mac M1 with colima instead of conventional docker
Fixed from @ninjasujan worked for me too. I migrated from intel to M1 and then back to Intel…