如何在没有显式定义的情况下在SAM中启用“ ApiKeyRequired”属性?

时间:2018-10-22 19:02:42

标签: aws-api-gateway aws-sam-cli aws-serverless aws-sam

在cloudformation中,public interface ApiRepository extends CrudRepository<Api, Long>{ } 具有布尔属性AWS::ApiGateway::Method。我如何在SAM中实现相同的目标?

我知道我们可以启用显式swagger配置。就是这样

ApiKeyRequired

是否可以通过SAM中的隐式API调用而不是显式传递 { "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" } } } 来实现?因为摇摇欲坠的代码适用于较少的端点,并且一旦端点增加就变得复杂。是否像我们在AWS::Serverless::Api中一样有APIkeyRequired这样的标志?

感谢任何帮助 谢谢

1 个答案:

答案 0 :(得分:0)

现在SAM的ApiKeyRequiredAWS::Serverless::Api级别都支持AWS::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

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