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

Bug: "sam build - TypeError" When building Docker Image from documentation #7186

Open
michaelnicol opened this issue Jun 20, 2024 · 1 comment
Labels
blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. question

Comments

@michaelnicol
Copy link

Description:

I am trying to build a Docker image for AWS SAM. The issue is that the code from the AWS SAM documentation doesn't work and crashes.

Steps to reproduce:

Go here and marvel at the four year old documentation which talks about a sam init that no longer supports docker: https://aws.amazon.com/blogs/compute/using-container-image-support-for-aws-lambda-with-aws-sam/

Create these three files:

app.py:

import json

def lambda_handler(event, context):
    response = {
        "statusCode": 200,
        "body": json.dumps({
            "message": "Hello, World!"
        }),
    }
    return response

Dockerfile:

FROM public.ecr.aws/lambda/nodejs:12

COPY app.js package.json ./

RUN npm install

CMD ["app.lambdaHandler"]

template.yml:

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
    Metadata:
      DockerTag: nodejs12.x-v1
      DockerContext: ./hello-world
      Dockerfile: Dockerfile

Observed result:

C:\Users\micha\OneDrive\Documents\Github\pyopensky-sam, Image, , 8053703e-9fca-4250-b89f-6c378fa4eafd, {'DockerTag': 'nodejs12.x-v1', 'DockerContext':
'C:\\Users\\micha\\OneDrive\\Documents\\Github\\pyopensky-sam\\hello-world', 'Dockerfile': 'Dockerfile'}, {}, x86_64, []), Function:
Function(function_id='HelloWorldFunction', name='HelloWorldFunction', functionname='HelloWorldFunction', runtime=None, memory=None, timeout=None, handler=None,      
imageuri=None, packagetype='Image', imageconfig=None, codeuri='C:\\Users\\micha\\OneDrive\\Documents\\Github\\pyopensky-sam', environment=None, rolearn=None,        
layers=[], events={'HelloWorld': {'Type': 'Api', 'Properties': {'Path': '/hello', 'Method': 'get', 'RestApiId': 'ServerlessRestApi'}}}, metadata={'DockerTag':       
'nodejs12.x-v1', 'DockerContext': 'C:\\Users\\micha\\OneDrive\\Documents\\Github\\pyopensky-sam\\hello-world', 'Dockerfile': 'Dockerfile', 'SamResourceId':
'HelloWorldFunction'}, inlinecode=None, codesign_config_arn=None, architectures=None, function_url_config=None,
function_build_info=<FunctionBuildInfo.BuildableImage: ('BuildableImage', 'Regular IMAGE function which can be build with SAM CLI')>, stack_path='',
runtime_management_config=None, logging_config=None))
2024-06-20 13:07:22,192 | Building codeuri: C:\Users\micha\OneDrive\Documents\Github\pyopensky-sam runtime: None metadata: {'DockerTag': 'nodejs12.x-v1',
'DockerContext': 'C:\\Users\\micha\\OneDrive\\Documents\\Github\\pyopensky-sam\\hello-world', 'Dockerfile': 'Dockerfile'} architecture: x86_64 functions:
HelloWorldFunction
2024-06-20 13:07:22,194 | Building to following folder C:\Users\micha\OneDrive\Documents\Github\pyopensky-sam\.aws-sam\build\HelloWorldFunction
2024-06-20 13:07:22,195 | Building image for HelloWorldFunction function
2024-06-20 13:07:22,202 | Setting DockerBuildArgs: {} for HelloWorldFunction function
2024-06-20 13:07:22,203 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-06-20 13:07:22,314 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-06-20 13:07:22,315 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '4f966c3d-d855-464a-8605-233bf58d9df0', 'installationId':
'6ebfe541-4282-4fae-8cf8-8559a00d734c', 'sessionId': '9b67fa2c-06a7-4b0b-aa3f-c147017dc4b2', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.11.8',      
'samcliVersion': '1.117.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'metricSpecificAttributes':
{'projectType': 'CFN', 'gitOrigin': None, 'projectName': 'd6ca6d08f149625ea626c2c64ef61b5f061b90564637fcba92ca4aea7fa52de4', 'initialCommit': None}, 'duration': 239,
'exitReason': 'TypeError', 'exitCode': 255}}]}
2024-06-20 13:07:22,315 | Unable to find Click Context for getting session_id.
2024-06-20 13:07:22,320 | Sending Telemetry: {'metrics': [{'events': {'requestId': '3110ddd6-6945-44a6-9dba-0df498a7bfaf', 'installationId':
'6ebfe541-4282-4fae-8cf8-8559a00d734c', 'sessionId': '9b67fa2c-06a7-4b0b-aa3f-c147017dc4b2', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.11.8',      
'samcliVersion': '1.117.0', 'commandName': 'sam build', 'metricSpecificAttributes': {'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml',     
'thread_id': '695d254949944b358b2e85ec811d0178', 'time_stamp': '2024-06-20 17:07:21.940', 'exception_name': None}, {'event_name': 'SamConfigFileExtension',
'event_value': '.toml', 'thread_id': '8ba61c2f5d114c52b7a1e201cc2efdef', 'time_stamp': '2024-06-20 17:07:21.964', 'exception_name': None}]}}}]}
2024-06-20 13:07:23,080 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
2024-06-20 13:07:23,081 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)

Error: You must specify a directory to build in path
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 347, 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 185, 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 150, in wrapped
    return_value = func(*args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\utils\version_checker.py", line 43, 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 169, in cli
    do_cli(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\commands\build\command.py", line 258, in do_cli
    ctx.run()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\commands\build\build_context.py", line 284, in run
    self._build_result = builder.build()
                         ^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\build\app_builder.py", line 226, in build
    return ApplicationBuildResult(build_graph, build_strategy.build())
                                               ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\build\build_strategy.py", line 83, in build
    result.update(self._build_functions(self._build_graph))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\build\build_strategy.py", line 93, in _build_functions
    function_build_results.update(self.build_single_function_definition(build_definition))
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\build\build_strategy.py", line 168, in build_single_function_definition
    result = self._build_function(
             ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\build\app_builder.py", line 669, in _build_function
    return self._build_lambda_image(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\build\app_builder.py", line 429, in _build_lambda_image
    (build_image, build_logs) = self._docker_client.images.build(**build_args)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\docker\models\images.py", line 296, in build
    resp = self.client.api.build(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\docker\api\build.py", line 152, in build
    raise TypeError("You must specify a directory to build in path")

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-%20TypeError
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20build%20-%20TypeError

Expected result:

It to build as the documentation states.

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

{
  "version": "1.117.0",
  "system": {
    "python": "3.11.8",
    "os": "Windows-10-10.0.22621-SP0"
  },
  "additional_dependencies": {
    "docker_engine": "26.1.4",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "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"
  ]
}
@michaelnicol michaelnicol added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Jun 20, 2024
@mndeveci
Copy link
Contributor

Hey there,

There are some issues with the example that you shared. It seems like you are trying to build an application which is written with python (app.py) in a nodejs image. You should be using python images for your application https://gallery.ecr.aws/lambda/python

For easiest start, I will recommend running sam init and making selections for your preference. In that way, you should have a valid and running project setup ready.

@mndeveci mndeveci added blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days question and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. question
Projects
None yet
Development

No branches or pull requests

2 participants