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.5k stars 1.17k forks source link

Bug: crashes if Function is missing `Handler` property (`Error: Invalid type <class 'NoneType'>`) #6253

Closed davidjb closed 9 months ago

davidjb commented 10 months ago

Description:

When a AWS::Serverless::Function is defined without a Handler property, attempting to sam build crashes out with a traceback.

I'm aware that Handler is a required property when PackageType is Zip (the default), but this crash should be avoided and be more descriptive of what the problem is.

Steps to reproduce:

  1. Create a template.yml like so:
    AWSTemplateFormatVersion: "2010-09-09"
    Transform: AWS::Serverless-2016-10-31
    Resources:
      MyFunction:
        Type: AWS::Serverless::Function
        Metadata:
          BuildMethod: makefile
        Properties:
          CodeUri: foo
          #Handler: bootstrap
          Runtime: provided.al2
          Architectures: [ arm64 ]
  2. Run sam build.
  3. Obverse crash and traceback as below. Uncomment the #Handler line for initialisation to succeed.

Observed result:

# sam build --debug
2023-11-13 05:42:24,461 | No config file found in this directory.
2023-11-13 05:42:24,463 | OSError occurred while reading TOML file: [Errno 2] No such file or directory: '/samconfig.toml'
2023-11-13 05:42:24,464 | Config file location: /samconfig.toml
2023-11-13 05:42:24,465 | Config file '/samconfig.toml' does not exist
2023-11-13 05:42:24,466 | Using SAM Template at /template.yml
2023-11-13 05:42:24,476 | OSError occurred while reading TOML file: [Errno 2] No such file or directory: '/samconfig.toml'
2023-11-13 05:42:24,476 | Using config file: samconfig.toml, config environment: default
2023-11-13 05:42:24,477 | Expand command line arguments to:
2023-11-13 05:42:24,477 | --template_file=/template.yml --mount_with=READ --build_dir=.aws-sam/build --cache_dir=.aws-sam/cache
2023-11-13 05:42:24,506 | 'build' command is called
2023-11-13 05:42:24,507 | No Parameters detected in the template
2023-11-13 05:42:24,518 | There is no customer defined id or cdk path defined for resource MyFunction, so we will use the resource logical id as
the resource id
2023-11-13 05:42:24,519 | 0 stacks found in the template
2023-11-13 05:42:24,519 | No Parameters detected in the template
2023-11-13 05:42:24,528 | There is no customer defined id or cdk path defined for resource MyFunction, so we will use the resource logical id as
the resource id
2023-11-13 05:42:24,529 | 1 resources found in the stack
2023-11-13 05:42:24,529 | Found Serverless function with name='MyFunction' and CodeUri='foo'
2023-11-13 05:42:24,530 | --base-dir is not presented, adjusting uri foo relative to /template.yml
2023-11-13 05:42:24,531 | 1 resources found in the stack
2023-11-13 05:42:24,531 | Found Serverless function with name='MyFunction' and CodeUri='foo'
2023-11-13 05:42:24,532 | Instantiating build definitions
2023-11-13 05:42:24,532 | No previous build graph found, generating new one
2023-11-13 05:42:24,533 | Unique function build definition found, adding as new (Function Build Definition: BuildDefinition(provided.al2, /foo,
Zip, , ddaa1087-56bd-487e-ae40-a4b9d8d5553c, {'BuildMethod': 'makefile'}, {}, arm64, []), Function: Function(function_id='MyFunction',
name='MyFunction', functionname='MyFunction', runtime='provided.al2', memory=None, timeout=None, handler=None, imageuri=None, packagetype='Zip',
imageconfig=None, codeuri='/foo', environment=None, rolearn=None, layers=[], events=None, metadata={'BuildMethod': 'makefile', 'SamResourceId':
'MyFunction'}, inlinecode=None, codesign_config_arn=None, architectures=['arm64'], function_url_config=None,
function_build_info=<FunctionBuildInfo.BuildableZip: ('BuildableZip', 'Regular ZIP function which can be build with SAM CLI')>, stack_path='',
runtime_management_config=None))
2023-11-13 05:42:24,535 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-11-13 05:42:24,536 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-11-13 05:42:24,537 | Unable to find Click Context for getting session_id.

Error: Invalid type <class 'NoneType'>
Traceback:
  File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/samcli/cli/cli_config_file.py", line 347, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/telemetry/metric.py", line 184, in wrapped
    raise exception  # pylint: disable=raising-bad-type
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/telemetry/metric.py", line 149, in wrapped
    return_value = func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/utils/version_checker.py", line 42, in wrapped
    actual_result = func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/samcli/cli/main.py", line 95, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/samcli/commands/build/command.py", line 171, in cli
    do_cli(
  File "/usr/local/lib/python3.10/dist-packages/samcli/commands/build/command.py", line 260, in do_cli
    ctx.run()
  File "/usr/local/lib/python3.10/dist-packages/samcli/commands/build/build_context.py", line 281, in run
    self._build_result = builder.build()
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/build/app_builder.py", line 184, in build
    build_graph = self._get_build_graph(self._container_env_var, self._container_env_var_file)
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/build/app_builder.py", line 267, in _get_build_graph
    build_graph.clean_redundant_definitions_and_update(not self._is_building_specific_resource)
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/build/build_graph.py", line 324, in clean_redundant_definitions_and_update
    self._atomic_write()
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/build/build_graph.py", line 482, in _atomic_write
    self._write()
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/build/build_graph.py", line 455, in _write
    build_definition_as_table = _function_build_definition_to_toml_table(function_build_definition)
  File "/usr/local/lib/python3.10/dist-packages/samcli/lib/build/build_graph.py", line 77, in _function_build_definition_to_toml_table
    toml_table[HANDLER_FIELD] = function_build_definition.handler
  File "/usr/local/lib/python3.10/dist-packages/tomlkit/items.py", line 1525, in __setitem__
    value = item(value, _parent=self)
  File "/usr/local/lib/python3.10/dist-packages/tomlkit/items.py", line 228, in item
    raise _ConvertError(f"Invalid type {type(value)}")

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

Expected result:

SAM not to crash and display a readable explanation such as:

The function MyFunction is missing a Handler property. This is required when PackageType is unset or set to Zip.

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

{
  "version": "1.101.0",
  "system": {
    "python": "3.11.6",
    "os": "Linux-6.4.16-linuxkit-aarch64-with-glibc2.35"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.6",
    "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"
  ]
}

Add --debug flag to command you are running

hawflau commented 10 months ago

@davidjb thanks for catching this bug. I also got the same error when trying with your example

github-actions[bot] commented 9 months ago

Patch is released in v1.104.0. Closing