aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

sam local invoke throws "Error: Error building docker image"

Open SriAishwaryaMasinaTR opened this issue 5 years ago • 17 comments

Description:

We are encountering below error while locally invoking a lambda in AWS code build machine

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

Build machine specs:

AWS CodeBuild - Linux env SAM CLI --version: 1.27.2 AWS region: us-east-1

Steps to reproduce:

Observed result:

Expected result:

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS:
  2. sam --version:
  3. AWS region:

Add --debug flag to command you are running

SriAishwaryaMasinaTR avatar Sep 30 '21 12:09 SriAishwaryaMasinaTR

We are facing same issue while invoking a lambda in AWS code build machine-

Image was not found.
188 | Building image...............
189 | Failed to build Docker Image
190 | NoneType: None
191 | Exception on /2015-03-31/functions/*****/invocations [POST]
192 | Traceback (most recent call last):
193 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
194 | response = self.full_dispatch_request()
195 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
196 | rv = self.handle_user_exception(e)
197 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
198 | reraise(exc_type, exc_value, tb)
199 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
200 | raise value
201 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
202 | rv = self.dispatch_request()
203 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
204 | return self.view_functions[rule.endpoint](**req.view_args)
205 | 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
206 | self.lambda_runner.invoke(function_name, request_data, stdout=stdout_stream_writer, stderr=self.stderr)
207 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/commands/local/lib/local_lambda.py", line 130, in invoke
208 | self.local_runtime.invoke(
209 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py", line 217, in wrapped_func
210 | return_value = func(*args, **kwargs)
211 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/lambdafn/runtime.py", line 176, in invoke
212 | container = self.create(function_config, debug_context, container_host, container_host_interface)
213 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/lambdafn/runtime.py", line 73, in create
214 | container = LambdaContainer(
215 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/docker/lambda_container.py", line 87, in __init__
216 | image = LambdaContainer._get_image(lambda_image, runtime, packagetype, imageuri, layers)
217 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/docker/lambda_container.py", line 213, in _get_image
218 | return lambda_image.build(runtime, packagetype, image, layers)
219 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/docker/lambda_image.py", line 144, in build
220 | self._build_image(image if image else image_name, image_tag, downloaded_layers, stream=stream_writer)
221 | File "/root/.pyenv/versions/3.8.10/lib/python3.8/site-packages/samcli/local/docker/lambda_image.py", line 245, in _build_image
222 | raise ImageBuildException("Error building docker image: {}".format(log["error"]))
223 | 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

SAM CLI, version 1.27.2 AWS region: us-east-1 AWS CodeBuild - Linux env

RichaSharmaTR avatar Sep 30 '21 12:09 RichaSharmaTR

@RichaSharmaTR and @SriAishwaryaMasinaTR thanks for reporting this issue. Can you please try to reproduce this issue with latest version of sam-cli. If you are able to reproduce the issue please mention the steps for me to reproduce.

Thanks

c2tarun avatar Sep 30 '21 15:09 c2tarun

Same issue

logs

Invoking index.handler (nodejs14.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

SAM CLI, version 1.27.2 AWS region: us-east-1 Build on Gitpod.io (Linux)

VrajGohil avatar Oct 02 '21 07:10 VrajGohil

Same issue

logs

Initializing the lambda functions containers.
--
252 | Image was not found.
253 | Building image...........................................................................................................................
254 | Failed to build Docker Image
255 | NoneType: None
256 | Lambda functions containers initialization failed because of Error building docker image: The command '/bin/sh -c chmod +x /var/rapid/aws-lambda-rie' returned a non-zero code: 1
257 | Traceback (most recent call last):
258 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/commands/local/cli_common/invoke_context.py", line 264, in _initialize_all_functions_containers
259 | async_context.run_async(default_executor=False)
260 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/lib/utils/async_utils.py", line 130, in run_async
261 | return run_given_tasks_async(self._async_tasks, event_loop, self.executor)
262 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/lib/utils/async_utils.py", line 90, in run_given_tasks_async
263 | return event_loop.run_until_complete(_run_given_tasks_async(tasks, event_loop, executor))
264 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
265 | return future.result()
266 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/lib/utils/async_utils.py", line 58, in _run_given_tasks_async
267 | raise result
268 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/concurrent/futures/thread.py", line 52, in run
269 | result = self.fn(*self.args, **self.kwargs)
270 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/commands/local/cli_common/invoke_context.py", line 255, in initialize_function_container
271 | self.lambda_runtime.run(
272 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/local/lambdafn/runtime.py", line 123, in run
273 | container = self.create(function_config, debug_context, container_host, container_host_interface)
274 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/local/lambdafn/runtime.py", line 381, in create
275 | container = super().create(function_config, debug_context, container_host, container_host_interface)
276 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/local/lambdafn/runtime.py", line 73, in create
277 | container = LambdaContainer(
278 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/local/docker/lambda_container.py", line 87, in __init__
279 | image = LambdaContainer._get_image(lambda_image, runtime, packagetype, imageuri, layers)
280 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/local/docker/lambda_container.py", line 213, in _get_image
281 | return lambda_image.build(runtime, packagetype, image, layers)
282 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/local/docker/lambda_image.py", line 144, in build
283 | self._build_image(image if image else image_name, image_tag, downloaded_layers, stream=stream_writer)
284 | File "/root/.pyenv/versions/3.9.5/lib/python3.9/site-packages/samcli/local/docker/lambda_image.py", line 245, in _build_image
285 | raise ImageBuildException("Error building docker image: {}".format(log["error"]))
286 | 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

SAM CLI, version 1.27.2 AWS region: ca-central-1 AWS CodeBuild - Linux env

jshamyaws avatar Oct 04 '21 12:10 jshamyaws

Upgrading the sam cli version to 1.32 worked for us. But we are not sure why we are facing this issue suddenly...Could you please let us know the root cause of this issue?

SriAishwaryaMasinaTR avatar Oct 04 '21 12:10 SriAishwaryaMasinaTR

@SriAishwaryaMasinaTR, Thanks for confirming. I am not fully sure about the root cause. Some changes has been made since 1.27.2 regarding image repositories but to my knowledge they were all backwards compatible. Sadly none of the logs above include image name which failed when building. I might be able to share more insight if you can reproduce the issue and attach log after running command with --debug flag. I am marking this issue closed for now, please feel free to re-open if you have more questions or want us to take a look at your debug logs.

Thanks Tarun

c2tarun avatar Oct 04 '21 15:10 c2tarun

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Oct 04 '21 15:10 github-actions[bot]

Hello @c2tarun @jfuss @awood45 @mgrandis @xazhao - It appears this issue has cropped back up in release 1.33.0. After experiencing the issue in version 1.27.2, I upgraded to version 1.33.0 and still got the issue. I can only get around it in version 1.32.0 specifically, which appears to not use the aws-lambda-rie docker image possibly?

I'm proposing we re-open this and dig into what is going on since it has resurfaced. Let me know if I can provide any helpful information. I've similarly posted about this in the #samdev Slack Channel.

Some Logs, all run from CodeBuild Linux Environment Image: aws/codebuild/standard:5.0 with Privileged set to True: AWS SAM Version 1.27.2 (Current Default SAM Install in the CodeBuild Image)

277 | [Container] 2021/10/04 20:10:27 Running command pip list
--
278 | Package                           Version
279 | --------------------------------- ---------
280 | arrow                             1.1.1
281 | attrs                             21.2.0
282 | aws-lambda-builders               1.5.0
283 | aws-sam-cli                       1.27.2
284 | aws-sam-translator                1.38.0
285 | backports.entry-points-selectable 1.1.0
286 | binaryornot                       0.4.4
287 | boto3                             1.18.6
288 | botocore                          1.21.6
...
552 | [Container] 2021/10/04 18:45:52 Running command nohup sam local start-lambda --warm-containers EAGER --port 3000 --env-vars env.json &
--
553 |  
554 | [Container] 2021/10/04 18:45:52 Running command sleep 50
555 | Initializing the lambda functions containers.
556 | Image was not found.
557 | Building image......................................................................................................................................
558 | Failed to build Docker Image
559 | NoneType: None
560 | Lambda functions containers initialization failed because of Error building docker image: The command '/bin/sh -c chmod +x /var/rapid/aws-lambda-rie' returned a non-zero code: 1
561 | Traceback (most recent call last):
...

AWS SAM Version 1.33.0 (Current Most Recent Release)

345 | [Container] 2021/10/04 20:21:53 Running command pip list
--
346 | Package                           Version
347 | --------------------------------- ---------
348 | arrow                             1.1.1
349 | attrs                             21.2.0
350 | aws-lambda-builders               1.8.1
351 | aws-sam-cli                       1.33.0
352 | aws-sam-translator                1.39.0
353 | backports.entry-points-selectable 1.1.0
354 | binaryornot                       0.4.4
355 | boto3                             1.18.6
356 | botocore                          1.21.6
...
619 | [Container] 2021/10/04 20:22:25 Running command nohup sam local start-lambda --warm-containers EAGER --port 3000 --env-vars env.json &
--
620 |  
621 | [Container] 2021/10/04 20:22:25 Running command sleep 50
622 | Initializing the lambda functions containers.
623 | Image was not found.
624 | Removing rapid images for repo public.ecr.aws/sam/emulation-nodejs14.x
625 | Building image................................................................................................................................................
626 | Failed to build Docker Image
627 | NoneType: None
628 | Lambda functions containers initialization failed because of Error building docker image: The command '/bin/sh -c mv /var/rapid/aws-lambda-rie-arm64 /var/rapid/aws-lambda-rie && chmod +x /var/rapid/aws-lambda-rie' returned a non-zero code: 1
629 | Traceback (most recent call last):
...

AWS SAM Version 1.32.0 (Only Working Release I've Found)

...
69 | [Container] 2021/10/04 20:43:43 Running command pip list
--
70 | Package                           Version
71 | --------------------------------- ---------
72 | arrow                             1.1.1
73 | attrs                             21.2.0
74 | aws-lambda-builders               1.7.0
75 | aws-sam-cli                       1.32.0
76 | aws-sam-translator                1.38.0
77 | backports.entry-points-selectable 1.1.0
78 | binaryornot                       0.4.4
79 | boto3                             1.18.6
80 | botocore                          1.21.6
...
348 | [Container] 2021/10/04 20:44:12 Running command nohup sam local start-lambda --warm-containers EAGER --port 3000 --env-vars env.json &
--
349 |  
350 | [Container] 2021/10/04 20:44:12 Running command sleep 50
351 | Initializing the lambda functions containers.
352 | Image was not found.
353 | Removing rapid images for repo public.ecr.aws/sam/emulation-nodejs14.x
354 | Building image..............................................................................................................................................
355 | Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs14.x:rapid-1.32.0.
356 |  
357 | Mounting /codebuild/output/src389130331/src/lambda-loader/.aws-sam/build/LambdaLoaderFunction as /var/task:ro,delegated inside runtime container
358 | Containers Initialization is done.
359 | Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint.
360 | 2021-10-04 20:44:30  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
361 |  
362 | [Container] 2021/10/04 20:45:02 Running command npm run integration
...

I can post Stack Traces if that is helpful at all.

cooper3330 avatar Oct 04 '21 21:10 cooper3330

@cooper3330 , thanks for reporting this. Just to clarify you are seeing failure during local start-lambda command?

c2tarun avatar Oct 04 '21 21:10 c2tarun

Yep, that is correct! This command, executed in CodeBuild, to be specific nohup sam local start-lambda --warm-containers EAGER --port 3000 --env-vars env.json &

cooper3330 avatar Oct 04 '21 21:10 cooper3330

@cooper3330 I am not able to reproduce this on my dev machine. Before I move ahead with setting up CodeBuild I wanted to confirm if you are seeing this issue only on CodeBuild or on local as well?

Local logs:

Initializing the lambda functions containers.
Image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-nodejs14.x
Building image....................................................................................................................................................................................................................................................
Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs14.x:rapid-1.33.0-x86_64.

Mounting /workplace/issues/sam-cli-3322/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
Containers Initialization is done.
Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint.
2021-10-04 14:30:17  * Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)
Invoking app.lambdaHandler (nodejs14.x)
Reuse the created warm container for Lambda function 'HelloWorldFunction'
Lambda function 'HelloWorldFunction' is already running
START RequestId: a00632a5-d5e0-4292-8921-a804fa3e48a6 Version: $LATEST
END RequestId: a00632a5-d5e0-4292-8921-a804fa3e48a6
REPORT RequestId: a00632a5-d5e0-4292-8921-a804fa3e48a6	Init Duration: 0.29 ms	Duration: 175.71 ms	Billed Duration: 176 ms	Memory Size: 128 MB	Max Memory Used: 128 MB
2021-10-04 14:30:20 127.0.0.1 - - [04/Oct/2021 14:30:20] "POST /2015-03-31/functions/HelloWorldFunction/invocations HTTP/1.1" 200 -

c2tarun avatar Oct 04 '21 21:10 c2tarun

Also can you please confirm what is the Architecture you are using for your lambda function?

c2tarun avatar Oct 04 '21 21:10 c2tarun

It does appear isolated to only CodeBuild. Or at least it's running fine locally on my Mac machine.

We have not yet started explicitly definiting an Architecture in our Lambda function, so it's defaulting to x86_64 Architecture.

Local Logs:

$ sam local start-lambda --warm-containers EAGER --port 3000 --env-vars env.json
Initializing the lambda functions containers.
Image was not found.
Removing rapid images for repo public.ecr.aws/sam/emulation-nodejs14.x
Building image.....................................................................................................................................................................................
Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs14.x:rapid-1.33.0-x86_64.

Mounting /dev/test-lambda-repo/.aws-sam/build/TestLambdaFunction as /var/task:ro,delegated inside runtime container
Containers Initialization is done.
Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint.
2021-10-04 17:43:17  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

cooper3330 avatar Oct 04 '21 21:10 cooper3330

@cooper3330, There was an issue with CodeBuild which is fixed now. I am also able to do local invoke on CodeBuild.

Can you please confirm if the issue is resolved for you?

Thanks Tarun

c2tarun avatar Oct 11 '21 15:10 c2tarun

Yep, it looks good from my end now as well @c2tarun!

70 | [Container] 2021/10/11 16:15:23 Running command pip list
--
71 | Package                           Version
72 | --------------------------------- ---------
73 | arrow                             1.1.1
74 | attrs                             21.2.0
75 | aws-lambda-builders               1.5.0
76 | aws-sam-cli                       1.27.2
77 | aws-sam-translator                1.38.0
78 | backports.entry-points-selectable 1.1.0
79 | binaryornot                       0.4.4
80 | boto3                             1.18.6
81 | botocore                          1.21.6
...
1004 | [Container] 2021/10/11 16:16:48 Running command nohup sam local start-lambda --warm-containers EAGER --port 3000 --env-vars env.json &
--
1005 |  
1006 | [Container] 2021/10/11 16:16:48 Running command sleep 50
1007 | Initializing the lambda functions containers.
1008 | Image was not found.
1009 | Building image..............................................................................................................................
1010 | Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-nodejs14.x:rapid-1.27.2.
1011 |  
1012 | Mounting /codebuild/output/src878631177/src/lambda-loader/.aws-sam/build/LambdaLoaderFunction as /var/task:ro,delegated inside runtime container
1013 | Containers Initialization is done.
1014 | Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint.
1015 | 2021-10-11 16:17:16  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
...

cooper3330 avatar Oct 11 '21 16:10 cooper3330

It looks to me the issue is resolved. Closing it for now. Feel free to re-open if there's still any issue

hawflau avatar Oct 14 '21 21:10 hawflau

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Oct 14 '21 21:10 github-actions[bot]