Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAM INVOKE FAILS #5421

Closed
Sadagopan88 opened this issue Jun 26, 2023 · 10 comments
Closed

SAM INVOKE FAILS #5421

Sadagopan88 opened this issue Jun 26, 2023 · 10 comments
Labels
area/dependencies Updates a dependency area/local/invoke sam local invoke command blocked-dependency

Comments

@Sadagopan88
Copy link

Sadagopan88 commented Jun 26, 2023

Description:

Steps to reproduce:

SAM CLI, version 1.88.0

run SAM local invoke

Observed result:

Error: a bytes-like object is required, not 'str'
Traceback:
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/click/core.py", line 1055, in main
   rv = self.invoke(ctx)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
   return _process_result(sub_ctx.command.invoke(sub_ctx))
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
   return _process_result(sub_ctx.command.invoke(sub_ctx))
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
   return ctx.invoke(self.callback, **ctx.params)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/click/core.py", line 760, in invoke
   return __callback(*args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/click/decorators.py", line 84, in new_func
   return ctx.invoke(f, obj, *args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/click/core.py", line 760, in invoke
   return __callback(*args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py", line 184, in wrapped
   raise exception  # pylint: disable=raising-bad-type
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py", line 149, in wrapped
   return_value = func(*args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/lib/utils/version_checker.py", line 42, in wrapped
   actual_result = func(*args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/cli/main.py", line 95, in wrapper
   return func(*args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/commands/local/invoke/cli.py", line 100, in cli
   do_cli(
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/commands/local/invoke/cli.py", line 206, in do_cli
   context.local_lambda_runner.invoke(
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/commands/local/lib/local_lambda.py", line 144, in invoke
   self.local_runtime.invoke(
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py", line 324, in wrapped_func
   return_value = func(*args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/local/lambdafn/runtime.py", line 189, in invoke
   container = self.create(function_config, debug_context, container_host, container_host_interface)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/local/lambdafn/runtime.py", line 85, in create
   container = LambdaContainer(
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/local/docker/lambda_container.py", line 94, in __init__
   image = LambdaContainer._get_image(
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/local/docker/lambda_container.py", line 237, in _get_image
   return lambda_image.build(runtime, packagetype, image, layers, architecture, function_name=function_name)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/local/docker/lambda_image.py", line 196, in build
   self._check_base_image_is_current(base_image)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/local/docker/lambda_image.py", line 470, in _check_base_image_is_current
   if self.is_base_image_current(image_name):
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/local/docker/lambda_image.py", line 494, in is_base_image_current
   return self.get_local_image_digest(image_name) == self.get_remote_image_digest(image_name)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/samcli/local/docker/lambda_image.py", line 510, in get_remote_image_digest
   remote_info = self.docker_client.images.get_registry_data(image_name)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/docker/models/images.py", line 356, in get_registry_data
   attrs=self.client.api.inspect_distribution(name, auth_config),
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/docker/utils/decorators.py", line 34, in wrapper
   return f(self, *args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/docker/utils/decorators.py", line 19, in wrapped
   return f(self, resource_id, *args, **kwargs)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/docker/api/image.py", line 287, in inspect_distribution
   return self._result(
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/docker/api/client.py", line 274, in _result
   self._raise_for_status(response)
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/docker/api/client.py", line 270, in _raise_for_status
   raise create_api_error_from_http_exception(e) from e
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/docker/errors.py", line 34, in create_api_error_from_http_exception
   if any(fragment in explanation_msg
 File "/usr/local/Cellar/aws-sam-cli/1.88.0/libexec/lib/python3.8/site-packages/docker/errors.py", line 34, in <genexpr>
   if any(fragment in explanation_msg

An unexpected error was encountered while executing "sam local invoke".  

Expected result:

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

  1. OS: MAC OC
  2. sam --version:1.88
  3. AWS region:
# output of `sam --info`
{
  "version": "1.88.0",
  "system": {
    "python": "3.8.17",
    "os": "macOS-12.3-x86_64-i386-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "4.5.1",
    "aws_cdk": "Not available",
    "terraform": "1.5.1"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Add --debug flag to command you are running

@Sadagopan88 Sadagopan88 added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Jun 26, 2023
@sriram-mv
Copy link
Contributor

Hey @Sadagopan88 thanks for your report! What does your template look like? Have you tested this with the latest docker engine?

However, I agree on graceful degradation and not throwing a stack trace here.

@Sadagopan88
Copy link
Author

Sadagopan88 commented Jun 27, 2023

@sriram-mv Yes im using latest docker engine Docker version 24.0.2, build cb74dfcd85

Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      Timeout: 300
      MemorySize: 2048
      Handler: com.xyz.Handler::handleRequest
      CodeUri: test-handler
      Runtime: java11
      Architectures:
        - x86_64

@hnnasit
Copy link
Contributor

hnnasit commented Jul 3, 2023

Hi @Sadagopan88, I downloaded sam cli using brew and used the sam init java11 hello world templates to try and reproduce this behavior but could not reproduce it. I used docker_engine: 24.0.2 as well.

@hnnasit hnnasit added stage/bug-repro The issue/bug needs to be reproduced and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Jul 3, 2023
@hnnasit
Copy link
Contributor

hnnasit commented Jul 4, 2023

Hi @Sadagopan88, based on your sam --info output it looks like you are using podman (4.5.1) with sam cli. We are able to reproduce the error with podman. We recently upgraded dockerpy and it looks like docker changed the error which is thrown to handle the docker API call. More details about this can be found in docker issue. The issue happens when sam local invoke tries to check the remote image version which was error handled in this PR. Since the error thrown changed with the docker version upgrade, sam local invoke started failing with podman. As a workaround, can you try running sam local invoke --skip-pull-image command and check if it works? I will mark this as a bug since the root cause is known.

@hnnasit hnnasit added type/bug area/local/invoke sam local invoke command and removed stage/bug-repro The issue/bug needs to be reproduced labels Jul 4, 2023
@japfohl
Copy link

japfohl commented Jul 26, 2023

@hnnasit I'm experiencing the exact same issue (Podman / SAM). I tried sam local invoke --skip-pull-image and here are my results.

sam --info

{
  "version": "1.93.0",
  "system": {
    "python": "3.8.8",
    "os": "Windows-10-10.0.22621-SP0"
  },
  "additional_dependencies": {
    "docker_engine": "4.5.0",
    "aws_cdk": "Not available",
    "terraform": "1.5.3"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

sam local invoke --skip-pull-image

Invoking app.lambdaHandler (nodejs18.x)
Requested to skip pulling images ...

Mounting C:\path\to\project\apps\functions\.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated, inside runtime container

Error: 500 Server Error for http+docker://localnpipe/v1.35/containers/create: Internal Server Error ("make cli opts(): making volume mountpoint for volume /path/to/project/apps/functions/.aws-sam/build/HelloWorldFunction: mkdir /c: permission denied")
Traceback:
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\decorators.py", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\telemetry\metric.py", line 184, in wrapped
    raise exception  # pylint: disable=raising-bad-type
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\telemetry\metric.py", line 149, in wrapped
    return_value = func(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\utils\version_checker.py", line 42, in wrapped
    actual_result = func(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\cli\main.py", line 95, in wrapper
    return func(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\local\invoke\cli.py", line 100, in cli
    do_cli(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\local\invoke\cli.py", line 206, in do_cli
    context.local_lambda_runner.invoke(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\local\lib\local_lambda.py", line 144, in invoke
    self.local_runtime.invoke(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\telemetry\metric.py", line 324, in wrapped_func
    return_value = func(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\local\lambdafn\runtime.py", line 189, in invoke
    container = self.create(function_config, debug_context, container_host, container_host_interface)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\local\lambdafn\runtime.py", line 104, in create
    self._container_manager.create(container)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\local\docker\manager.py", line 95, in create
    container.create()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\local\docker\container.py", line 210, in create
    real_container = self.docker_client.containers.create(self._image, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\models\containers.py", line 917, in create
    resp = self.client.api.create_container(**create_kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\api\container.py", line 431, in create_container
    return self.create_container_from_config(config, name, platform)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\api\container.py", line 448, in create_container_from_config
    return self._result(res, True)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\api\client.py", line 274, in _result
    self._raise_for_status(response)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\api\client.py", line 270, in _raise_for_status
    raise create_api_error_from_http_exception(e) from e
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\errors.py", line 39, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation) from e

The above looks like more of a permissions error on my end so I'll dig into that but if there's any chance it's helpful then I figured I'd share.

@LoganB99
Copy link

LoganB99 commented Aug 8, 2023

--skip-pull-image works for me on MacOS + podman. but still have other podman related issues with sam cli, i think all are documented though

@mndeveci
Copy link
Contributor

mndeveci commented Oct 4, 2023

Hi there,

We found the issue which is originating from dockerpy library that SAM CLI uses under the hood for communicating with Docker APIs. We've raised a PR on that repository (docker/docker-py#3156) which is merged. We are now waiting a new release of the library, and once that is released we are going to update our dependency which should resolve the issue.

We will update this issue once we got the newer version of the library.

Thanks

@mndeveci mndeveci added area/dependencies Updates a dependency blocked-dependency and removed type/bug labels Oct 4, 2023
@januaryjon
Copy link

januaryjon commented Oct 31, 2023

Mounting C:\path\to\project\apps\functions.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated, inside runtime container

Error: 500 Server Error for http+docker://localnpipe/v1.35/containers/create: Internal Server Error ("make cli opts(): making volume mountpoint for volume /path/to/project/apps/functions/.aws-sam/build/HelloWorldFunction: mkdir /c: permission denied")

Turns out that on Windows, it looks like SAM does a simple string replace of the host path for the volume mount, eg: C: -> /c. This breaks in podman as the podman machine in WSL2 uses the more common WSL2 convention of mounting all the windows local disks to /mnt.

I found a quick-and-dirty workaround for this by logging into an interactive session of the podman machine, then creating the respective symlinks, for eg: /c -> /mnt/c

The commands I ran should give you a better idea on what I'm talking about.

wsl -d podman-machine-default -e bash -li
cd /
sudo ln -s /mnt/c c
sudo ln -s /mnt/d d
...

@hnnasit
Copy link
Contributor

hnnasit commented Mar 8, 2024

The PR that @mndeveci had raised has been merged and released and SAM CLI has updated the docker version. The original issue should now be resolved. Closing since the fix has been released already, feel free to open a new issue if you are still facing issues or have questions.

@hnnasit hnnasit closed this as completed Mar 8, 2024
Copy link
Contributor

github-actions bot commented Mar 8, 2024

⚠️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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dependencies Updates a dependency area/local/invoke sam local invoke command blocked-dependency
Projects
None yet
Development

No branches or pull requests

7 participants