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.
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)
Description:
When a
AWS::Serverless::Function
is defined without aHandler
property, attempting tosam build
crashes out with a traceback.I'm aware that
Handler
is a required property whenPackageType
isZip
(the default), but this crash should be avoided and be more descriptive of what the problem is.Steps to reproduce:
template.yml
like so:sam build
.#Handler
line for initialisation to succeed.Observed result:
Expected result:
SAM not to crash and display a readable explanation such as:
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
Add --debug flag to command you are running