3

在 cloudformation 中,AWS::ApiGateway::Method具有布尔属性ApiKeyRequired。我怎样才能在 SAM 中达到同样的效果?

我知道我们可以使用显式招摇配置启用。就像这样

    {
    "swagger": "2.0",
    "info": {
        "version": "1.0",
        "title": {
              "Ref": "AWS::StackName"
            }
    },
    "x-amazon-apigateway-api-key-source": "HEADER",
    "paths": {
        "/": {
            "get": {
                "x-amazon-apigateway-integration": {
                    "httpMethod": "POST",
                    "type": "aws_proxy",
                    "uri": {
                    "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetHelloWorld.Arn}/invocations"
                  }
                },
                "responses": {},
                "security": [
                    {
                        "api_key": []
                    }
                ]
            }
        }
    },
    "securityDefinitions": {
        "api_key": {
            "type": "apiKey",
            "name": "x-api-key",
            "in": "header"
        }
    }
}

不能在 SAM 中使用隐式 API 调用而不是显式传递AWS::Serverless::Api?因为 swagger 代码对于较少的端点是可以的,并且一旦端点增加就会变得复杂。有没有APIkeyRequired像我们这样的旗帜Cloudformation

感谢任何帮助谢谢

4

1 回答 1

0

现在在 SAM的和级别ApiKeyRequired都受支持。AWS::Serverless::ApiAWS::Serverless::Function

以下是 AWS 文档中的一个示例:

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        ApiKeyRequired: true # sets for all methods

  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: index.handler
      Runtime: nodejs8.10
      Events:
        ApiKey:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /
            Method: get
            Auth:
              ApiKeyRequired: true

您还可以从以下资源中了解这一点:

于 2020-05-27T17:51:58.803 回答