简体   繁体   English

AWS Serverless ApiKey UsagePlan 找不到网关 api 参考

[英]AWS Serverless ApiKey UsagePlan cannot find gateway api reference

I'm trying to build a serverless API that calls a lambda function with an ApiKey.我正在尝试构建一个使用 ApiKey 调用 lambda function 的无服务器 API。 AWS SDK in Visual Studio gives me an error stating that the reference is an invalid type. Visual Studio 中的 AWS SDK 给我一个错误,指出引用是无效类型。

I have compared my serverless template to several other working examples, but I must be overlooking some specific detail.我已经将我的无服务器模板与其他几个工作示例进行了比较,但我必须忽略一些特定的细节。

Any ideas?有任何想法吗?

来自 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/"
      }
    }
  }
}

ApiId is a string, not an object. ApiId 是一个字符串,而不是 object。

You can fix by just putting "ApiGatewayApi" there您只需将“ApiGatewayApi”放在那里即可修复

{
"ApiId": "ApiGatewayApi"
}

Ref: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-usageplan-apistage.html#cfn-apigateway-usageplan-apistage-apiid参考: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-usageplan-apistage.html#cfn-apigateway-usageplan-apistage-apiid

You are referring an AWS::Serverless::Api resource for your ApiId and the template is expecting a AWS::ApiGateway::RestApi resource.您正在为您的 ApiId 引用AWS::Serverless::Api资源,并且模板需要AWS::ApiGateway::RestApi资源。 That's reason behind that error.这就是该错误背后的原因。

The error disappears once you update the type of your ApiGatewayApi更新ApiGatewayApi的类型后,错误就会消失

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

更新的模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM