edg*_*rch 15 amazon-web-services aws-api-gateway serverless-framework
我想用无服务器创建一个安全的APIG,在我目前的" s-fuction.json "我已经拥有:
"apiKeyRequired": true,
Run Code Online (Sandbox Code Playgroud)
在我的" s-resources-cf.json "中我已经拥有:
"AWSApiKey": {
"Type": "AWS::ApiGateway::ApiKey",
"Properties" : {
"Description" : "ApiKey for secure the connections to the xxx API",
"Enabled" : true
}
}
Run Code Online (Sandbox Code Playgroud)
它正确地为lambda(包括CORS)和API Key创建了一个Lambda,一个APIG,但是我需要手动"分配"生成的APIG-Stage的密钥,你对我怎么能这样做有什么想法自动使用无服务器?
我从这里阅读了有关我想要的功能的AWS文档(似乎有可能):AWS CloudFormation API密钥
文档显示它可以通过以下方式完成:
"ApiKey": {
"Type": "AWS::ApiGateway::ApiKey",
"DependsOn": ["TestAPIDeployment", "Test"],
"Properties": {
"Name": "TestApiKey",
"Description": "CloudFormation API Key V1",
"Enabled": "true",
"StageKeys": [{
"RestApiId": { "Ref": "RestApi" },
"StageName": "Test"
}]
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何添加对无服务器自动创建的APIG的引用以及如何等待创建APIG.
您可以通过将 apiKeys 数组属性添加到 serverless.yml 中的提供程序对象来指定服务 Rest API 要使用的 API 密钥列表。您还需要显式指定哪些端点是私有的,并要求通过向要设置为私有的 http 事件对象添加私有布尔属性来将其中一个 api 密钥包含在请求中。API 密钥是全局创建的,因此如果您想将服务部署到不同的阶段,请确保您的 API 密钥包含如下定义的阶段变量。使用API密钥时,您可以选择使用usagePlan对象定义使用计划配额和限制。
以下是为服务 Rest API 设置 API 密钥的示例配置:
service: my-service
provider:
name: aws
apiKeys:
- myFirstKey
- ${opt:stage}-myFirstKey
- ${env:MY_API_KEY} # you can hide it in a serverless variable
usagePlan:
quota:
limit: 5000
offset: 2
period: MONTH
throttle:
burstLimit: 200
rateLimit: 100
functions:
hello:
events:
- http:
path: user/create
method: get
private: true
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请阅读以下文档: https://serverless.com/framework/docs/providers/aws/events/apigateway
| 归档时间: |
|
| 查看次数: |
1411 次 |
| 最近记录: |