问题描述
在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 appreciatedThanks
推荐答案
现在 ApiKeyRequired $ c $
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官方文档。
- 本演练由萨尔塔克·贾恩(Sarthak Jain)
- AWS Official Documentation here.
- This walkthrough blog post by Sarthak Jain.
这篇关于如何启用“ ApiKeyRequired” SAM中没有显式的招摇定义的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!