首页 > 解决方案 > AWS APIGateway CloudFormation 指定方法所需的 API 密钥?

问题描述

我有以下 CloudFormation 模板,它创建了我的 API 网关(由 Lambda 支持)。我想启用 API 密钥作为其中一种或多种方法的要求。我已成功创建 API 密钥、使用计划和两者之间的关联,但无法弄清楚如何为某些方法实际启用“需要 API 密钥”属性。AWS 的文档将“ ApiKeyRequired ”属性指定为AWS::ApiGateway::Method组件的一部分,但我的 CF 模板没有或没有使用此组件?考虑到我以前从未需要它,我不确定如何使用它?

我的模板如下:

   "ServerlessRestApi": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
            "Description":"This is a placeholder for the description of this web api",
            "ApiKeySourceType":"HEADER",
            "Body": {
                "info": {
                    "version": "1.0",
                    "title": {
                        "Ref": "AWS::StackName"
                    }
                },
                "paths": {
                    "/list/tables": {
                        "get": {
                            "x-amazon-apigateway-integration": {
                                "httpMethod": "POST",
                                "type": "aws_proxy",
                                "uri": {
                                    "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableList.Arn}/invocations"
                                }
                            },
                            "security": [
                                {
                                   "api_key": []
                                }
                             ],
                            "responses": {}
                        }
                    },
                    "/list/columns/{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/${GetColumnList.Arn}/invocations"
                                }
                            },
                            "responses": {}
                        }
                    },
                    "datagw/general/table/get/{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": {}
                        }
                    },
                    "/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": {}
                        }
                    },
                    "securityDefinitions": {
                        "type": "api_key",
                        "name": "x-api-key",
                        "in": "header"
                      }
                },
                "swagger": "2.0"
            }
        }
    },

标签: amazon-web-servicesamazon-cloudformationaws-api-gateway

解决方案


我认为在每条路径下添加security然后在下添加会起作用。securityDefinitionspaths

"paths": {
  "/list/tables": {
     "get": {
        "x-amazon-apigateway-integration": {
           "httpMethod": "POST",
           "type": "aws_proxy",
           "uri": {
              "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015- 
               03-31/functions/${GetTableList.Arn}/invocations"
           }
        },
        "security": [
           {
              "api_key": []
           }
        ]
     }
  }
},
"securityDefinitions": {
  "type": "api_key",
  "name": "x-api-key",
  "in": "header"
}

推荐阅读