Amazon cloudformation c#aws api gateway serverless.template结构未通过CloudFormation处理

Amazon cloudformation c#aws api gateway serverless.template结构未通过CloudFormation处理,amazon-cloudformation,aws-api-gateway,serverless-application-model,Amazon Cloudformation,Aws Api Gateway,Serverless Application Model,我已经构建了一个.NET serverless应用程序,并具有以下serverless.template { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Description": "API Gateway to access InSite data-store.", "Resources": { "Get": {

我已经构建了一个.NET serverless应用程序,并具有以下serverless.template

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Description": "API Gateway to access InSite data-store.",
    "Resources": {
        "Get": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::Get",
                "Runtime": "dotnetcore2.1",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Environment": {
                    "Variables":{
                        "LOG_LEVEL":"2",
                        "CONNECTION_STRING":"insite-api-connectionstring"
                    }
                },
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/",
                            "Method": "GET"
                        }
                    }
                }
            }
        },
        "GetTableBasic": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::GetTableBasic",
                "Runtime": "dotnetcore2.1",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Environment": {
                    "Variables":{
                        "LOG_LEVEL":"2",
                        "CONNECTION_STRING":"insite-api-connectionstring"
                    }
                },
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/tables/{tableid}/{columnid}",
                            "Method": "GET"
                        }
                    }
                }
            }
        },
        "PostClickCollectNotification": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::PostClickCollectNotification",
                "Runtime": "dotnetcore2.1",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Environment": {
                    "Variables":{
                        "LOG_LEVEL":"2",
                        "CONNECTION_STRING":"insite-api-connectionstring"
                    }
                },
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/order/notify",
                            "Method": "POST"
                        }
                    }
                }
            }
        },
        "PostClickCollectStockUpdate": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::PostClickCollectStockUpdate",
                "Runtime": "dotnetcore2.1",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Environment": {
                    "Variables":{
                        "LOG_LEVEL":"2",
                        "TARGET_TABLE":"InSiteClickCollect",
                        "CONNECTION_STRING":"insite-api-connectionstring"
                    }
                },
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "/order/stock/update/",
                            "Method": "POST"
                        }
                    }
                }
            }
        },
        "GetTableResponse": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "VpcConfig": {
                    "SecurityGroupIds": [
                        "sg-111a1476"
                    ],
                    "SubnetIds": [
                        "subnet-3029a769",
                        "subnet-5ec0b928"
                    ]
                },
                "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::GetTableResponse",
                "Runtime": "dotnetcore2.1",
                "CodeUri": "",
                "MemorySize": 256,
                "Timeout": 30,
                "Role": null,
                "Policies": [
                    "AWSLambdaBasicExecutionRole",
                    "AWSLambdaVPCAccessExecutionRole",
                    "AmazonSSMFullAccess"
                ],
                "Environment": {
                    "Variables":{
                        "LOG_LEVEL":"2",
                        "CONNECTION_STRING":"insite-api-connectionstring"
                    }
                },
                "Events": {
                    "PutResource": {
                        "Type": "Api",
                        "Properties": {
                            "Path": "tables/query/{tableid}",
                            "Method": "GET"
                        }
                    }
                }
            }
        },
        "ServerlessRestApi": {
            "Type": "AWS::ApiGateway::RestApi",
            "Properties": {
                "Description":"InSite Web API Version 2.0.0.0",
                "Body": {
                    "swagger": "2.0",
                    "info": {
                        "version": "1.0",
                        "title": {
                            "Ref": "AWS::StackName"
                        }
                    },
                    "x-amazon-apigateway-api-key-source" : "HEADER",
                    "schemes":["https"],
                    "paths": {
                        "tables/query/{tableid}": {
                            "get": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "GET",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableResponse.Arn}/invocations"
                                    }
                                },
                                "responses": {},
                                    "security": [
                                    {
                                        "api_key": []
                                    }
                                ]
                            }
                        },
                        "/products/update/": {
                            "post": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "POST",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PostClickCollectStockUpdate.Arn}/invocations"
                                    }
                                },
                                "responses": {}
                            }
                        },
                        "/": {
                            "get": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "GET",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Get.Arn}/invocations"
                                    }
                                },
                                "responses": {}
                            }
                        },
                        "/tables/{tableid}/{columnid}": {
                            "get": {
                                "x-amazon-apigateway-integration": {
                                    "httpMethod": "GET",
                                    "type": "aws_proxy",
                                    "uri": {
                                        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableBasic.Arn}/invocations"
                                    }
                                },
                                "responses": {}
                            }
                        }
                    },
                    "securityDefinitions": {
                        "api_key": {
                            "type": "apiKey",
                            "name": "x-api-key",
                            "in": "header"
                        }
                    }
                }
            }
        },
        "InternalUsagePlan": {
            "Type": "AWS::ApiGateway::UsagePlan",
            "Properties": {
                "ApiStages": [
                    {
                        "ApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "Stage": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ],
                "Description": "Internal Apps Usage Plan",
                "UsagePlanName": "Insite-datagw-InternalAppPlan-new"
            }
        },
        "ExternalUsagePlan": {
            "Type": "AWS::ApiGateway::UsagePlan",
            "Properties": {
                "ApiStages": [
                    {
                        "ApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "Stage": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ],
                "Description": "External Apps Usage Plan",
                "UsagePlanName": "InSite-datagw-ExternalAppPlan-new"
            }
        },
        "KeyHeartInHand": {
            "Type": "AWS::ApiGateway::ApiKey",
            "DependsOn": [
                "ServerlessRestApi",
                "ServerlessRestApiProdStage"
            ],
            "Properties": {
                "Name": "apikeyHeartInHand-new",
                "Description": "Api Key for Heart In Hand",
                "Enabled": true,
                "GenerateDistinctId": true,
                "StageKeys": [
                    {
                        "RestApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "StageName": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ]
            }
        },
        "LinkHeartInHandKey": { 
            "Type": "AWS::ApiGateway::UsagePlanKey",
            "Properties": {
                "KeyId": {
                    "Ref": "KeyHeartInHand"
                },
                "KeyType": "API_KEY",
                "UsagePlanId": {
                    "Ref": "InternalUsagePlan"
                }
            }
        },
        "KeyUmbraco": {
            "Type": "AWS::ApiGateway::ApiKey",
            "DependsOn": [
                "ServerlessRestApi",
                "ServerlessRestApiProdStage"
            ],
            "Properties": {
                "Name": "KeyUmbraco-new",
                "Description": "Api Key for Umbraco Website",
                "Enabled": true,
                "GenerateDistinctId": true,
                "StageKeys": [
                    {
                        "RestApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "StageName": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ]
            }
        },
        "LinkPricelineSiteKey": {
            "Type": "AWS::ApiGateway::UsagePlanKey",
            "Properties": {
                "KeyId": {
                    "Ref": "KeyUmbraco"
                },
                "KeyType": "API_KEY",
                "UsagePlanId": {
                    "Ref": "InternalUsagePlan"
                }
            }
        },
        "KeyPenTest": {
            "Type": "AWS::ApiGateway::ApiKey",
            "DependsOn": [
                "ServerlessRestApi",
                "ServerlessRestApiProdStage"
            ],
            "Properties": {
                "Name": "apiKeyPenTesting-new",
                "Description": "Api Key for pen testing",
                "Enabled": true,
                "GenerateDistinctId": true,
                "StageKeys": [
                    {
                        "RestApiId": {
                            "Ref": "ServerlessRestApi"
                        },
                        "StageName": {
                            "Ref": "ServerlessRestApiProdStage"
                        }
                    }
                ]
            }
        },
        "LinkPenTestKey": {
            "Type": "AWS::ApiGateway::UsagePlanKey",
            "Properties": {
                "KeyId": {
                    "Ref": "KeyPenTest"
                },
                "KeyType": "API_KEY",
                "UsagePlanId": {
                    "Ref": "InternalUsagePlan"
                }
            }
        }
    },
    "Outputs": {
        "ApiURL": {
            "Description": "API endpoint URL for Prod environment",
            "Value": {
                "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
            }
        }
    }
}
该应用程序通过AWS Toolkit for Visual Studio部署OK,但如果不是我对AWS::ApiGateway::RestApi部分所做的所有更新的话,处理后的cloudformation似乎缺少了大部分更新。当我在AWS控制台中检查时,已处理的CF模板如下所示:

   "ServerlessRestApi": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Body": {
          "info": {
            "version": "1.0",
            "title": {
              "Ref": "AWS::StackName"
            }
          },
          "paths": {
            "/order/stock/update/": {
              "post": {
                "x-amazon-apigateway-integration": {
                  "httpMethod": "POST",
                  "type": "aws_proxy",
                  "uri": {
                    "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PostClickCollectStockUpdate.Arn}/invocations"
                  }
                },
                "responses": {}
              }
            },
            "/order/notify": {
              "post": {
                "x-amazon-apigateway-integration": {
                  "httpMethod": "POST",
                  "type": "aws_proxy",
                  "uri": {
                    "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PostClickCollectNotification.Arn}/invocations"
                  }
                },
                "responses": {}
              }
            },
            "/tables/{tableid}/{columnid}": {
              "get": {
                "x-amazon-apigateway-integration": {
                  "httpMethod": "POST",
                  "type": "aws_proxy",
                  "uri": {
                    "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableBasic.Arn}/invocations"
                  }
                },
                "responses": {}
              }
            },
            "tables/query/{tableid}": {
              "get": {
                "x-amazon-apigateway-integration": {
                  "httpMethod": "POST",
                  "type": "aws_proxy",
                  "uri": {
                    "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableResponse.Arn}/invocations"
                  }
                },
                "responses": {}
              }
            },
            "/": {
              "get": {
                "x-amazon-apigateway-integration": {
                  "httpMethod": "POST",
                  "type": "aws_proxy",
                  "uri": {
                    "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Get.Arn}/invocations"
                  }
                },
                "responses": {}
              }
            }
          },
          "swagger": "2.0"
        }
      }
    },

由于上述问题,我的API网关中没有设置apiKey required=true。其他一切都创建得很好,创建了api密钥,创建了使用计划,创建了密钥和计划之间的链接,但是设置为required=true的api密钥不会发生。我在这里不知所措。我尝试用一个新名称部署堆栈来创建一个新的API,但同样的情况也发生了

目前看来这是SAM框架的一个限制。根据,目前只能通过使用外部swagger文件来使用所需的API密钥,这反过来限制了
AWS::Serverless::Function
事件配置的使用


据介绍,现在有一个RFC,以优先考虑向SAM添加对API键的支持。

这似乎是SAM框架目前的一个限制。根据,目前只能通过使用外部swagger文件来使用所需的API密钥,这反过来限制了
AWS::Serverless::Function
事件配置的使用


据了解,现在有一个RFC,以优先考虑向SAM添加API密钥支持。

您的模板似乎存在一些问题

首先,您使用的是
AWS::ApiGateway::RestApi
资源来定义API,而不是AWS SAM
AWS::Serverless::API
资源。我建议尽可能多地使用AWS SAM资源,除非有特定的理由不使用它们

其次,您似乎没有在无服务器函数上使用可用的
Auth
属性。通过向函数()添加
“Auth”:{“ApiKeyRequired”:true}
,您应该能够获得所需的功能

最后,我看到了对
ServerlessRestApiProdStage
资源的引用,但没有对其进行定义,这可能是因为您使用了
AWS::ApiGateway::RestApi
并将其与AWS SAM混合使用。如果更改为使用
AWS::Serverless::Api
,则可以使用
{Fn::GetAtt:[“ServerlessRestApi”,“Stage”]}


希望这有帮助!

您的模板似乎有一些问题

首先,您使用的是
AWS::ApiGateway::RestApi
资源来定义API,而不是AWS SAM
AWS::Serverless::API
资源。我建议您尽可能多地使用AWS SAM资源,除非有特定的理由不使用它们

其次,您似乎没有在无服务器函数上使用可用的
Auth
属性。您应该能够通过向函数()添加
“Auth”:{“ApiKeyRequired”:true}
来获得所需的功能

最后,我看到了对
ServerlessRestApiProdStage
资源的引用,但没有对其进行定义,这可能是因为您使用了
AWS::ApiGateway::RestApi
,并将其与AWS SAM混合使用。如果更改为使用
AWS::Serverless::Api
,则可以使用
{Fn::GetAtt:[]ServerlessRestApi”,“Stage”]}

希望这有帮助