如何启用“ ApiKeyRequired” SAM中没有显式的招摇定义的属性? [英] How to enable "ApiKeyRequired" property in SAM without explicit swagger definition?

查看:269
本文介绍了如何启用“ ApiKeyRequired” SAM中没有显式的招摇定义的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在cloudformation中, AWS :: ApiGateway :: Method 具有布尔属性 ApiKeyRequired 。我如何在SAM中实现相同的目标?

In cloudformation, AWS::ApiGateway::Method has a boolean property ApiKeyRequired . How can i achieve the same in SAM ?

我知道我们可以使用显式swagger Configuration来启用。像这样

I know that we can enable using explicit swagger Configuration. which is like this

    {
    "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 吗?因为摇摇欲坠的代码适用于较少的端点,并且一旦端点增加就变得复杂。是否有像我们在 Cloudformation 中一样的 APIkeyRequired 这样的标志?

Cant it possible with implicit API call in SAM rather than explicitly passing the AWS::Serverless::Api ? Because the swagger code is okay for less endpoints and becomes complex once endpoints got increased. Is there any flag like APIkeyRequired like we have in Cloudformation ?

感谢任何帮助
谢谢

Any help is appreciated Thanks

推荐答案

现在 ApiKeyRequired AWS :: Serverless :: Api AWS :: Serverless :: Function 级别都支持c> SAM。

Now ApiKeyRequired is supported at both the AWS::Serverless::Api and AWS::Serverless::Function level in SAM.

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

Here is an example from the AWS Documentation:

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

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

You can also learn about this from the following resources:

  • AWS Official Documentation here.
  • This walkthrough blog post by Sarthak Jain.

这篇关于如何启用“ ApiKeyRequired” SAM中没有显式的招摇定义的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆