aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.52k stars 1.17k forks source link

Bug: sam build - DockerException #6090

Open lrbn86 opened 1 year ago

lrbn86 commented 1 year ago

Description:

I am following the AWS SAM hello world app tutorial and I am stuck on step 2 of using the 'sam build'.

Steps to reproduce:

  1. Follow the two prerequisites on the tutorial page: Setup AWS CLI and Install SAM CLI
  2. Choose a directory via Git Bash terminal to use 'sam init'
  3. cd into chosen directory ('aws_projects'), then run 'sam init'
  4. Follow the 'sam init' interactive flow, but choose to use the runtime nodejs18.x instead with the package type of zip
  5. Denied the options for the X-ray tracing and monitoring using CloudWatch Application Insights
  6. Gave the project name of 'hello-world'
  7. cd into the 'hello-world' directory
  8. run 'sam build'
  9. Error: Install pypiwin32 package to enable npipe:// support

Observed result:

sam build --debug
2023-10-16 16:54:37,445 | Config file location: C:\Users\Brandon\Desktop\projects\aws_projects\hello-world\samconfig.toml
2023-10-16 16:54:37,452 | Loading configuration values from [default.['build'].parameters] (env.command_name.section) in config file at 'C:\Users\Brandon\Desktop\projects\aws_projects\hello-world\samconfig.toml'...
2023-10-16 16:54:37,455 | Configuration values successfully loaded.
2023-10-16 16:54:37,456 | Configuration values are: {'stack_name': 'hello-world', 'cached': True, 'parallel': True}
2023-10-16 16:54:37,467 | Using SAM Template at C:\Users\Brandon\Desktop\projects\aws_projects\hello-world\template.yaml
2023-10-16 16:54:37,536 | Using config file: samconfig.toml, config environment: default
2023-10-16 16:54:37,538 | Expand command line arguments to:
2023-10-16 16:54:37,540 | --template_file=C:\Users\Brandon\Desktop\projects\aws_projects\hello-world\template.yaml --parallel --mount_with=READ --build_dir=.aws-sam\build --cache_dir=.aws-sam\cache --cached
2023-10-16 16:54:38,011 | 'build' command is called
2023-10-16 16:54:38,013 | Starting Build use cache
2023-10-16 16:54:38,023 | No Parameters detected in the template
2023-10-16 16:54:38,093 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2023-10-16 16:54:38,095 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2023-10-16 16:54:38,099 | 0 stacks found in the template
2023-10-16 16:54:38,100 | No Parameters detected in the template
2023-10-16 16:54:38,150 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2023-10-16 16:54:38,152 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2023-10-16 16:54:38,154 | 2 resources found in the stack
2023-10-16 16:54:38,156 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello-world/'
2023-10-16 16:54:38,158 | --base-dir is not presented, adjusting uri hello-world/ relative to C:\Users\Brandon\Desktop\projects\aws_projects\hello-world\template.yaml
2023-10-16 16:54:38,169 | 2 resources found in the stack
2023-10-16 16:54:38,171 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello-world/'
2023-10-16 16:54:38,174 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-10-16 16:54:38,362 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-10-16 16:54:38,364 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '11f529b9-2826-4ddc-a6c4-70ff7c360831', 'installationId': '904f9871-19eb-4829-b907-6ffbd3e7e3ba', 'sessionId':
'f93e65b4-48e4-4c47-8572-51e91b7770c7', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.8', 'samcliVersion': '1.98.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam    
build', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin': None, 'projectName': 'afa27b44d43b02a9fea41d13cedc2e4016cfcf87c5dbf990e593669aa8ce286d', 'initialCommit': None}, 'duration': 638, 'exitReason':
'DockerException', 'exitCode': 255}}]}
2023-10-16 16:54:38,364 | Unable to find Click Context for getting session_id.
2023-10-16 16:54:38,374 | Sending Telemetry: {'metrics': [{'events': {'requestId': '247fd743-848f-4351-97bf-d82942aa71ff', 'installationId': '904f9871-19eb-4829-b907-6ffbd3e7e3ba', 'sessionId':
'f93e65b4-48e4-4c47-8572-51e91b7770c7', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.8', 'samcliVersion': '1.98.0', 'commandName': 'sam build', 'metricSpecificAttributes': {'events': [{'event_name':
'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': 'f6d6f4be9c854725a8343e3b75d78cec', 'time_stamp': '2023-10-16 20:54:37.445', 'exception_name': None}, {'event_name': 'SamConfigFileExtension', 'event_value':      
'.toml', 'thread_id': '4852cb55fa5e44ad8a3a9bfbda5c3d19', 'time_stamp': '2023-10-16 20:54:37.536', 'exception_name': None}]}}}]}
2023-10-16 16:54:38,805 | Telemetry response: 200
2023-10-16 16:54:38,809 | Telemetry response: 200

Error: Install pypiwin32 package to enable npipe:// support
Traceback:
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1688, 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 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\cli\cli_config_file.py", line 297, in wrapper
    return func(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 783, 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\build\command.py", line 171, in cli
    do_cli(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\build\command.py", line 260, in do_cli
    ctx.run()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\build\build_context.py", line 256, in run
    builder = ApplicationBuilder(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\app_builder.py", line 162, in __init__
    self._docker_client = docker_client if docker_client else docker.from_env(version=DOCKER_MIN_API_VERSION)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\client.py", line 96, in from_env
    return cls(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\client.py", line 45, in __init__
    self.api = APIClient(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\api\client.py", line 164, in __init__
    raise DockerException(

An unexpected error was encountered while executing "sam build".
Search for an existing issue:
https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+Bug%3A%20sam%20build%20-%20DockerException
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20build%20-%20DockerException

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

  1. OS: Windows 10
  2. sam --version: SAM CLI, version 1.98.0
  3. AWS region: us-east-2
sam --info --debug
Traceback (most recent call last):
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\api\client.py", line 159, in __init__
    self._custom_adapter = NpipeHTTPAdapter(
NameError: name 'NpipeHTTPAdapter' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "runpy.py", line 194, in _run_module_as_main
  File "runpy.py", line 87, in _run_code
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\__main__.py", line 12, in <module>
    cli(prog_name="sam")
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1077, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 943, in make_context
    self.parse_args(ctx, args)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1644, in parse_args
    rest = super().parse_args(ctx, args)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1408, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 2400, in handle_parse_result
    value = self.process_value(ctx, value)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 2362, in process_value
    value = self.callback(ctx, self, value)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\cli\main.py", line 58, in print_info
    "additional_dependencies": gather_additional_dependencies_info(),
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\utils\system_info.py", line 37, in gather_additional_dependencies_info
    "docker_engine": _gather_docker_info(),
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\utils\system_info.py", line 60, in _gather_docker_info
    with contextlib.closing(docker.from_env(version=DOCKER_MIN_API_VERSION)) as client:
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\client.py", line 96, in from_env
    return cls(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\client.py", line 45, in __init__
    self.api = APIClient(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\api\client.py", line 164, in __init__
    raise DockerException(
docker.errors.DockerException: Install pypiwin32 package to enable npipe:// support

Add --debug flag to command you are running

mndeveci commented 1 year ago

Hi there,

It seems like this issue has been reported before and this was the fix for the previous customer: https://github.com/aws/aws-sam-cli/issues/2899#issuecomment-848944826

I've tried to reproduce on my Windows machine with 1.98.0 version, but I was able to run build without any problems. Can you provide more details of your setup so that we can re-produce on our side?

Thanks

lrbn86 commented 1 year ago

Hi,

I have Python 3.12.0 and I do not have Docker installed. I installed SAM CLI via the msi download. I have turned off the App execution aliases for Python and set the PATH for Python . I installed pywin32 via pip:

pip list
Package Version
------- -------
pip     23.2.1
pywin32 306

I did the following on Command Prompt on admin:

C:\Users\Brandon\AppData\Local\Programs\Python\Python312\Scripts>py pywin32_postinstall.py -install

Parsed arguments are: Namespace(install=True, remove=False, wait=None, silent=False, quiet=False, destination='C:\\Users\\Brandon\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages')
Copied pythoncom312.dll to C:\WINDOWS\system32\pythoncom312.dll
Copied pywintypes312.dll to C:\WINDOWS\system32\pywintypes312.dll
Registered: Python.Interpreter
Registered: Python.Dictionary
Registered: Python
-> Software\Python\PythonCore\3.12\Help[None]=None
-> Software\Python\PythonCore\3.12\Help\Pythonwin Reference[None]='C:\\Users\\Brandon\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\PyWin32.chm'
Registered help file
Pythonwin has been registered in context menu
Shortcut for Pythonwin created
Shortcut to documentation created
The pywin32 extensions were successfully installed.

On Git Bash, I made a new project via sam init with the same config on my desktop and still received the same error with sam build:

sam-app> sam.cmd build

Error: Install pypiwin32 package to enable npipe:// support
Traceback:
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1688, 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 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 783, 
in invoke
    return __callback(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\cli\cli_config_file.py", line 297, in wrapper
    return func(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 783, 
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\build\command.py", line 171, in cli
    do_cli(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\build\command.py", line 260, in do_cli
    ctx.run()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\build\build_context.py", line 256, in run
    builder = ApplicationBuilder(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\app_builder.py", line 162, in __init__
    self._docker_client = docker_client if docker_client else docker.from_env(version=DOCKER_MIN_API_VERSION)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\client.py", line 96, in from_env
    return cls(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\client.py", line 45, in __init__
    self.api = APIClient(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\docker\api\client.py", line 164, in __init__
    raise DockerException(

An unexpected error was encountered while executing "sam build".
Search for an existing issue:
https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+Bug%3A%20sam%20build%20-%20DockerException
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20build%20-%20DockerException

This is my project structure after running sam build in Command Prompt:

C:\Users\Brandon\Desktop\sam-app>tree /f /a
|   .gitignore
|   README.md
|   template.yaml
|
+---.aws-sam
|   \---build
+---events
|       event.json
|
\---hello-world
    |   .npmignore
    |   app.mjs
    |   package.json
    |
    \---tests
        \---unit
                test-handler.mjs
bhaveshachhada commented 9 months ago

I'm also facing similar problem. Any updates on this ?

mattsinfield commented 4 weeks ago

Hi - downgrade to SAMCLI v.1.114.0 (others may work but I've not tested). I had the same error after I'd uplifted from Python 3.9 to 3.12 and wasted a lot of time on pypiwin32 versioning to align correctly. On closer reading of the stack trace it appeared the error was wholly contained within the SAMCLI install which reminded me that at the same time I'd also upgraded SAMCLI to the latest version (1.124.0). When I reverted to 1.114.0 sam build worked fine again.

cvargas-xbrein commented 4 weeks ago

Hola, actualicé a SAMCLI v.1.114.0 (es posible que funcionen otras versiones, pero no las he probado). Tuve el mismo error después de actualizar de Python 3.9 a 3.12 y perdí mucho tiempo en el control de versiones de pypiwin32 para alinearlo correctamente. Al leer más de cerca el seguimiento de la pila, parecía que el error estaba completamente contenido dentro de la instalación de SAMCLI, lo que me recordó que al mismo tiempo también había actualizado SAMCLI a la última versión (1.124.0). Cuando volví a 1.114.0, la compilación de sam funcionó bien nuevamente.

Hi , how can install a custom version ? only with pip ?

Nalagami commented 3 weeks ago

If you want to install custom version, you can use winget.

winget uninstall --id Amazon.SAM-CLI
winget show --id Amazon.SAM-CLI --version
# Maybe you can show installable version list.
winget install --id Amazon.SAM-CLI --version 1.114.0

Sorry if I make any mistakes, I don’t use English that often.

el-ko commented 3 weeks ago

I faced this issue too on SAM-CLI (version 1.126.0) with both build and deploy commands. Removing and reinstalling older release as per @Nalagami worked for me too.

I installed one version prior too latest release -> 1.125.0.

A small comment on winget commands from @Nalagami winget show -id Amazon.SAM-CLI --versions --versions is the right option too list available versions.

cvargas-xbrein commented 3 weeks ago

Thanks guys, it finally works. I have a problem with privileges on Windows, but after uninstalling aws cli and reinstalling it works fine, with version 1.114.0 for me.

rupe120 commented 3 weeks ago

I just installed 1.126.0 and ran into the same issue as well.

Uninstalling and installing 1.125.0 does resolve the issue.

winget uninstall --id Amazon.SAM-CLI
winget install --id Amazon.SAM-CLI --version 1.125.0
hnnasit commented 2 weeks ago

SAM CLI supports installation through the native MSI installer found here or any version of choice. Could you check if you run into the same issues while installing through the native installer? More info on the installation steps can be found here.