为什么 sam build 命令显示警告“ServerlessRestApi”是保留的逻辑 ID?

Mat*_*and 10 aws-sam-cli aws-sam

我使用创建了示例应用程序sam init。当我运行时sam build,我收到警告:
\n Your template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.
\n模板没有此逻辑 ID。为什么构建会生成此警告?

\n

重现步骤

\n
\xe2\x9d\xaf sam init  \n\nWhich template source would you like to use?  \n        1 - AWS Quick Start Templates  \n        2 - Custom Template Location  \nChoice: 1  \n\nChoose an AWS Quick Start application template  \n        1 - Hello World Example  \n        2 - Multi-step workflow  \n        ...  \nTemplate: 1  \n\n Use the most popular runtime and package type? (Python and zip) [y/N]:  \n N  \n\nWhich runtime would you like to use?  \n        1 - dotnet6  \n        2 - dotnet5.0  \n        ...  \nRuntime: 1\n\nWhat package type would you like to use?  \n        1 - Zip  \n        2 - Image  \nPackage type: 1  \n\nBased on your selections, the only dependency manager available is cli-package.  \nWe will proceed copying the template using cli-package.  \n\nProject name [sam-app]:  \n\nCloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)  \n\n    -----------------------  \n    Generating application:  \n    -----------------------  \n    Name: sam-app  \n    Runtime: dotnet6  \n    ...  \n\xe2\x9d\xaf cd .\\sam-app  \n\xe2\x9d\xaf sam build  \nYour template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.\nBuilding codeuri: C:\\repos\\sam-app\\src\\HelloWorld runtime: dotnet6 metadata: {} architecture: x86_64 functions: [\'HelloWorldFunction\']\nRunning DotnetCliPackageBuilder:GlobalToolInstall\n
Run Code Online (Sandbox Code Playgroud)\n

生成的 template.yaml 的内容

\n
\xe2\x9d\xaf sam init  \n\nWhich template source would you like to use?  \n        1 - AWS Quick Start Templates  \n        2 - Custom Template Location  \nChoice: 1  \n\nChoose an AWS Quick Start application template  \n        1 - Hello World Example  \n        2 - Multi-step workflow  \n        ...  \nTemplate: 1  \n\n Use the most popular runtime and package type? (Python and zip) [y/N]:  \n N  \n\nWhich runtime would you like to use?  \n        1 - dotnet6  \n        2 - dotnet5.0  \n        ...  \nRuntime: 1\n\nWhat package type would you like to use?  \n        1 - Zip  \n        2 - Image  \nPackage type: 1  \n\nBased on your selections, the only dependency manager available is cli-package.  \nWe will proceed copying the template using cli-package.  \n\nProject name [sam-app]:  \n\nCloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)  \n\n    -----------------------  \n    Generating application:  \n    -----------------------  \n    Name: sam-app  \n    Runtime: dotnet6  \n    ...  \n\xe2\x9d\xaf cd .\\sam-app  \n\xe2\x9d\xaf sam build  \nYour template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.\nBuilding codeuri: C:\\repos\\sam-app\\src\\HelloWorld runtime: dotnet6 metadata: {} architecture: x86_64 functions: [\'HelloWorldFunction\']\nRunning DotnetCliPackageBuilder:GlobalToolInstall\n
Run Code Online (Sandbox Code Playgroud)\n

小智 9

尝试为 API 创建资源并将其引用到您的处理程序。

Resources:
  MyAPI:
      Type: AWS::Serverless::Api
      Properties:
        StageName: Prod
        Cors:
          AllowMethods: "'GET,POST,OPTIONS'"
          AllowHeaders: "'content-type'"
          AllowOrigin: "'*'"
          AllowCredentials: false



  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: ./src/HelloWorld/
      Handler: HelloWorld::HelloWorld.Function::FunctionHandler
      Runtime: dotnet6
      Architectures:
        - x86_64
      MemorySize: 256
      Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
        Variables:
          PARAM1: VALUE
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get
            RestApiId: !Ref MyAPI # **** new property here ****

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${MyAPI}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" # *** change here {MyAPI}
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn
Run Code Online (Sandbox Code Playgroud)

  • SAM CLI 抱怨它生成的资源是没有意义的。这样做是为了方便和简洁,我认为如果不需要,就不必定义 Api。https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specation- generated-resources-function.html#sam-specation- generated-resources-function-api (3认同)