Why does sam build command display warning "ServerlessRestApi" is a reservered logical ID?
Asked Answered
W

2

10

I created the sample app with sam init. When I the run sam build, I get the warning:
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.
The template does not have this logical ID. Why does build generate this warning?

Steps to reproduce

❯ 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

Contents of resulting template.yaml

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
Wame answered 14/3, 2022 at 13:23 Comment(1)
@yg-abhi Sure, I have updated my question with the steps I used to create the template and the contents of the template file it generated. The warning I am asking about appears right after the sam build command.Wame
B
9

Try creating a resource for the API and refer it your handler.

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
Basting answered 24/3, 2022 at 15:11 Comment(2)
ref: github.com/aws/serverless-application-model/blob/master/docs/…Nasho
It does not make sense that the SAM CLI complains about a resource it generates. This is done for convenience and brevity, I don't think someone should have to define the Api if they do not need to. docs.aws.amazon.com/serverless-application-model/latest/…Petiole
M
1

"sam local invoke --debug" this command debug your code and share the problem.

Mccarthyism answered 17/8, 2022 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.