Search code examples
amazon-web-servicesaws-lambdaserverlessaws-sam-cli

Property StageName: not defined for resource of type AWS::Serverless::Api


I have seen alot of post say that you can add OpenApiVersion: '2.0' to fix this problem but it does not change anything in my case. For some reason now that I am trying to add a Stage for the first time and run my function locally with sam local start-api I always get Error: [InvalidResourceException('ApiGateway', 'property StageName: not defined for resource of type AWS::Serverless::Api')] as an error. The reason for adding the new stage is because I need it for my new authorizer I am trying to implement. Any ideas why StageName is not defined? Its clearly needed per AWS documentation

template.json

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Description",
  "Transform": [
    "AWS::Serverless-2016-10-31"
  ],
  "Resources": {
    "RapidApiGateway": {
      "Type": "AWS::Serverless::Api",
      "Properties": {
        "StageName:": "Prod",
        "MethodSettings": [
          {
            "HttpMethod": "*",
            "ResourcePath": "/*",
            "ThrottlingRateLimit": 10,
            "ThrottlingBurstLimit": 10
          }
        ],
        "Auth": {
          "DefaultAuthorizer": "RapidAuthorizer",
          "Authorizers": {
            "RapidAuthorizer": {
              "Fn::GetAtt": [
                "RapidAuthFunction",
                "attributeName"
              ],
              "Identity": {
                "Headers": [
                  "X-RapidAPI-Proxy-Secret"
                ]
              }
            }
          }
        }
      }
    },
    "RapidAuthFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "CodeUri": "./authorizer",
        "Handler": "handler.authorizer",
        "Runtime": "nodejs14.x"
      }
    }
  },
  "Outputs": {
    "WebEndpoint": {
      "Description": "API Gateway endpoint URL for Prod stage",
      "Value": {
        "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
      }
    }
  }
}

Solution

  • Looks like you have a typo in "StageName:": when it should be "StageName":. Also replacing the hardcoded "Prod" string with "Ref": "AWS::StackName" is a good practice in CloudFormation templates. The reason is that this will make your stack more flexible and reusable because it allows the stage name to dynamically adjust based on the stack's name.

    "StageName": {
      "Ref": "AWS::StackName"
    },