为什么 AWS SAM CLI (aws sam cli) 在使用全局部分时构建失败?

Why does AWS SAM CLI (awssamcli) fail to build when using Global section?

我刚刚发现 Global section,它似乎对清理 AWS SAM 云形成模板的 template.yaml 非常有用。完成全局部分后,我从我的 lambda 中删除运行时(因为它现在应该从全局部分继承运行时)。但是一旦我这样做,我就会出错。

这是我的 template.yaml:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Global:
  Function:
    Handler: app.lambda_handler
    Runtime: python3.8
    Timeout: 900
    MemorySize: 10240
    VpcConfig:
      SecurityGroupIds:
        - !Ref SecurityGroupABC
      SubnetIds:
        - subnet-abc
        - subnet-xyz
        - subnet-rrr   
    Environment:
      Variables:
        ENV_VAR: abc123  
Resources:
   lambdaName:
    Type: AWS::Serverless::Function 
    Role: 'arn:aws:iam::1234...'
    #Runtime: python3.8
    Properties:
      CodeUri: lambda/lambdaName/

这是我在 运行 sam build:

之后看到的错误
  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 829, in __call__
    return self.main(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1259, 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 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\telemetry\metric.py", line 153, 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 122, 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 90, in wrapper
    return func(*args, **kwargs)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\build\command.py", line 210, in cli
    do_cli(
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\build\command.py", line 315, in do_cli
    artifacts = builder.build()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\app_builder.py", line 139, in build
    build_graph = self._get_build_graph(self._container_env_var, self._container_env_var_file)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\app_builder.py", line 206, in _get_build_graph
    build_graph.clean_redundant_definitions_and_update(not self._is_building_specific_resource)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\build_graph.py", line 256, in clean_redundant_definitions_and_update
    self._write()
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\build_graph.py", line 296, in _write
    build_definition_as_table = _function_build_definition_to_toml_table(function_build_definition)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\build_graph.py", line 53, in _function_build_definition_to_toml_table
    toml_table[RUNTIME_FIELD] = function_build_definition.runtime
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\tomlkit\items.py", line 1022, in __setitem__
    value = item(value)
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\tomlkit\items.py", line 107, in item
    raise ValueError("Invalid type {}".format(type(value)))
ValueError: Invalid type <class 'NoneType'>

如果我将 lambda 的运行时留在它的部分,我就不会收到错误。但保留它会破坏建立这些全球价值观以减少冗余的目的。

在您的模板中,Global 应重命名为 Globals

请参考您已经分享的Globals Sectionlink