首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么sam命令显示警告"ServerlessRestApi“是一个已预订的逻辑ID?

为什么sam命令显示警告"ServerlessRestApi“是一个已预订的逻辑ID?
EN

Stack Overflow用户
提问于 2022-03-14 13:23:54
回答 2查看 3.7K关注 0票数 8

我用sam init创建了示例应用程序。当我运行sam build时,我会收到警告:

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.

模板没有这个逻辑ID。为什么生成此警告?

复制步骤

代码语言:javascript
复制
❯ sam init  

Which template source would you like to use?  
        1 - AWS Quick Start Templates  
        2 - Custom Template Location  
Choice: 1  

Choose an AWS Quick Start application template  
        1 - Hello World Example  
        2 - Multi-step workflow  
        ...  
Template: 1  

 Use the most popular runtime and package type? (Python and zip) [y/N]:  
 N  

Which runtime would you like to use?  
        1 - dotnet6  
        2 - dotnet5.0  
        ...  
Runtime: 1

What package type would you like to use?  
        1 - Zip  
        2 - Image  
Package type: 1  

Based on your selections, the only dependency manager available is cli-package.  
We will proceed copying the template using cli-package.  

Project name [sam-app]:  

Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)  

    -----------------------  
    Generating application:  
    -----------------------  
    Name: sam-app  
    Runtime: dotnet6  
    ...  
❯ cd .\sam-app  
❯ sam build  
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.
Building codeuri: C:\repos\sam-app\src\HelloWorld runtime: dotnet6 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
Running DotnetCliPackageBuilder:GlobalToolInstall

结果template.yaml的内容

代码语言:javascript
复制
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Sample SAM Template for sam-app

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 10

Resources:
  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

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://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  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
EN

回答 2

Stack Overflow用户

发布于 2022-03-24 15:11:14

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

代码语言:javascript
复制
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

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/"
  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
票数 7
EN

Stack Overflow用户

发布于 2022-08-17 22:11:03

“调试”此命令调试您的代码并共享问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71468496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档