AWS Serverless ApiKey UsagePlan cannot find gateway api reference

Viewed 238

I'm trying to build a serverless API that calls a lambda function with an ApiKey. AWS SDK in Visual Studio gives me an error stating that the reference is an invalid type.

I have compared my serverless template to several other working examples, but I must be overlooking some specific detail.

Any ideas?

Error message from VS 2019

Here is my serverless template... what am I missing?

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "An AWS Serverless Application.",
  "Resources": {
    "SendEmailFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "ServerlessTest3::ServerlessTest3.Functions::SendEmail",
        "Runtime": "dotnetcore3.1",
        "CodeUri": "",
        "MemorySize": 12,
        "Timeout": 30,
        "Role": null,
        "Policies": [
          "AWSLambdaBasicExecutionRole"
        ],
        "Events": {
            "ProxyResource": {
                "Type": "Api",
                "Properties": {
                    "Path": "/{proxy+}",
                    "Method": "POST",
                    "RestApiId": {
                        "Ref": "ApiGatewayApi"
                    }
                }
            },
            "RootResource": {
                "Type": "Api",
                "Properties": {
                    "Path": "/",
                    "Method": "POST",
                    "RestApiId": {
                        "Ref": "ApiGatewayApi"
                    }
                }
            }
        }
      }
    },    
    "ApiUsagePlan": {
        "Type": "AWS::ApiGateway::UsagePlan",
        "Properties": {
            "ApiStages": [
                {
                    "ApiId": {
                        "Ref": "ApiGatewayApi"
                    },
                    "Stage": "Prod"
                }
            ]
        }
    },
    "ApiUsagePlanKey": {
        "Type": "AWS::ApiGateway::UsagePlanKey",
        "Properties": {
            "KeyId": {
                "Ref": "ApiKey"
            },
            "KeyType": "API_KEY",
            "UsagePlanId": {
                "Ref": "ApiUsagePlan"
            }
        }
    },
    "ApiKey": {
        "Type": "AWS::ApiGateway::ApiKey",
        "Properties": {
            "Name": "my-api-key-name",
            "Enabled": "true",
            "StageKeys": [{
                "RestApiId": {
                    "Ref": "ApiGatewayApi"
                },
                "StageName": "Prod"
                }
            ]
        }
    },
    "ApiGatewayApi": {
        "Type": "AWS::Serverless::Api",
        "Properties": {
            "StageName": "Prod",
            "Auth": {
                "ApiKeyRequired": "true"
            }
        }
    }
  },
  "Outputs": {
    "ApiURL": {
      "Description": "API endpoint URL for Prod environment",
      "Value": {
        "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
      }
    }
  }
}
2 Answers

You are referring an AWS::Serverless::Api resource for your ApiId and the template is expecting a AWS::ApiGateway::RestApi resource. That's reason behind that error.

The error disappears once you update the type of your ApiGatewayApi

"ApiGatewayApi": {
    "Type": "AWS::ApiGateway::RestApi",
    "Properties": {
        "Body": {
            "OpenAPI specification": null
        },
        "Description": "A test API",
        "Name": "MyRestAPI"
    }
}

Updated template

Related