簡體   English   中英

AWS Serverless ApiKey UsagePlan 找不到網關 api 參考

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

我正在嘗試構建一個使用 ApiKey 調用 lambda function 的無服務器 API。 Visual Studio 中的 AWS SDK 給我一個錯誤,指出引用是無效類型。

我已經將我的無服務器模板與其他幾個工作示例進行了比較,但我必須忽略一些特定的細節。

有任何想法嗎?

來自 VS 2019 的錯誤消息

這是我的無服務器模板......我錯過了什么?

{
  "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 是一個字符串,而不是 object。

您只需將“ApiGatewayApi”放在那里即可修復

{
"ApiId": "ApiGatewayApi"
}

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

您正在為您的 ApiId 引用AWS::Serverless::Api資源,並且模板需要AWS::ApiGateway::RestApi資源。 這就是該錯誤背后的原因。

更新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